Programmers have to understand the concept of choice. Given a set value or set of values, we need to design programs that offer choices. At an ATM we are presented with a menu of transaction options. The choice of a single option determines an entire sequence of operations. The programmers of the ATM have also programmed all the options presented in the ATM. Whatever the option, there is a direction of code.
Decision structures make branching happen. Also, this is one of the most uniform concept in all of programming. It must be understood that decision structures are a programming ideology. As you plan your program solution, rest assured that what you consider in one language works in many other languages as well. The keyword for decision structures in if
. We will explore the variety of if
statements that we can encounter in programming.
true
or false
. In programming there are 6 relational operators. When relational operators are used in expressions, the expression is called a relational expression. The results of a relational expression are always true or false. Here is a list of the relational operators.
x > y
true
if x
has a numeric equivalent value that is higher than y
and false
otherwise.x >= y
true
if x
is at least numerically equivalent to or higher than y
and false
otherwise. x < y
true
if x
has a numeric equivalent value that is lower than y
and false
otherwise.x <= y
true
if x
is at least numerically equivalent to or lower than y
and false
otherwise. x == y
true
if x
has a numeric equivalent value that is the same as y
and false
otherwise.x != y
true
if x
has a numeric equivalent value that is not the same as y
and false
otherwise. if
statementsThere are times where we want our program to respond only when a condition exists. Consider that we want to write a programming statement that asks the user for a grade and will respond with a message if the learner passed. Here are examples in the various languages.
Language | Code | *-not considering input validation at this stage
---|---|
C++ |
#include<iostream>
|
C# |
using System;
|
Java |
import java.util.Scanner;
|
Python |
grade = int(input("Enter final score for the semester: "))
|
For this program, there will be no output unless the user inputs an integer value 70 or greater. If the program example had more code above the if statement and below it, if the user entered a grade below 70, then the code would execute everything above and below the decision structure.
Single-alternative decision structures are called if-statments as well as "do this or do nothing statements."
if
-else
statmentsThere are some times where a decision has to be made that is mutually exclusive. Consider a course where you either pass or you fail. There are no additional options but those two. When we arrive at this branch, one option must be chosen. This is the dual alternative decision structure. Let's look at coding examples of a pass-fail program.
Language | Code | *-not considering input validation at this stage
---|---|
C++ |
#include<iostream>
|
C# |
using System;
|
Java |
import java.util.Scanner;
|
Python |
grade = int(input("Enter final score for the semester: "))
|
This code is written so there will only be a message that the student passed the class or failed the class. There is no way to know what the output will be at run time until the end user inputs the grade value. Also, notice that else
does not require an explicit condition because it is only activated if the if
condition is false
.
When we considered a menu-driven screen at the ATM, we should realize that the user will select only one of the options at any given time. If the user selects "Deposit" then the user did not select the other options on the screen. We would need to write a structure that would define a branch for each particular item. To play with the idea presented in single-alternative and dual-alternative descriptions, let's consider that the user (a student) wants to know his or her final grade. We can accomplish this using a multiple alernative statement (called if-else-if
statements)
if-else-if
statementsI know that I am staying fairly high-level in this introduction of coding. The if-else-if
is a special construct known as a nested statement. What we are actually saying is this: if the first condition is true, execute the body of that if block, else, if the second condition is true, execute the second body, else, if the third condition is true... and so on.
There could be a possibility of all the conditions not being true for an execution and then the block will produce no output (just like an if
statement). To prevent this from happening, programmers often use a "trailing else
" to output a statement if none of the conditions are true.
Let's look at a letter grade output program where A ranges from 90 - 100, B ranges from 80 - 89, C, 70 - 79, D, 60 - 69, and F, 0 - 59.
Language | Code | *not accounting for input validation |
---|---|
C++ |
#include <iostream>
|
C# |
using System;
|
Java |
import java.util.Scanner;
|
Python |
grade = int(input("Enter final score for the class: "))
|
Notice that the logic of this construct is built so that only one condition is true for the entire block. While 92
is greater than or equal to 80, 70, and 60, 92
would trigger the first block. If a condition is true, then there is no more decision to be made in the block. Also notice that a variable letter
was used to capture the letter grade. This allows me to not have to write repetitive output statements just for each grade range.
Notice that in Python, the keyword of elif
is used where else if
is used by the others.
There are way more details that could have been addressed but these are the very high points of decision structures. We need them to allow our programs to provide real interactivity by giving the user his or her own experience with your program. As the programmer, it is important to define all the alternatives for decision structures. Knowing the right type of decision structure is beneficial for code design. It is also important to know that programmers have the abiliity to design conditions as loosely or as stringent as the algorithm requires.