| Conditional
Statements
If Statements
Let’s have the program perform different tasks under
different conditions. A person may think, "Oh gee, here comes
the hard part." Digital Reference says, "DON’T SWEAT
IT!" By using the if statement, you can have a fine
and dandy program that runs errands under certain
"requirements."
Here is how to set up your if statement:
if(type here under what condition this program should perform
the task)
type your assignment statement here;
The "requirement" of the if statement is also known
as a Boolean expression. It can be true or false. If the
Boolean expression is true, meaning if such a condition exists,
then the program will run the task it is assigned. However, if the
expression is false, the program will skip the task.
Relational operators can be used in a Boolean expression. <
means less than. > means greater than. <= means less than or
equal to. >= greater than or equal to. != means does not equal
to. = = means equal to.
Let’s say, if the variable x equals 1, we want the program
the program to display "False." We would write the
following if statement:
if (x == 1)
cout << "False";
if-else statements
We can also have the program run a task under a certain
condition. If the condition is not met, the program can run
another task otherwise. This is done so by writing if-else statements.
For example, if the variable x equals 1, the program will display
"False." Otherwise, if the variable x represents another
value, display "True." We would write the following
if-else statements:
if (x == 1)
cout << "False";
else
cout << "True";
Sometimes you want the program to perform more than one task
under a certain condition. It is possible! All you have to do is
use a brace to enclose the statements. These statements are called
compound statements. Here is an example:
If the variable x equals 1, display, "Hi" on one
line, and "How are you?" on the next line. If the
variable x represents another value other than 1, display
"See you!" on one line, and "Please come back
another time" on the next line." This is how you write
the if-else statements consisting of the compound statements:
if(x == 1){
cout << "Hi" << endl;
cout << "How are you?";
}else{
cout << "See you!" << endl;
cout << "Please come back another time"
<< endl;
}
Nested if statements
Let’s say you want the program to perform a certain task
under a certain condition, and this certain condition will run
only if another condition is met. Then, you will need to use nested
if statements.
Here is an example:
If the variable x equals 100, we want the program to display
"Perfect Score." Otherwise, if x does not equal 100, but
if x is greater than or equal to 60, display "Pass." If
x is not greater than or equal to 60, display, "Fail."
if (x = = 100)
cout << "Perfect Score";
else
if(x >=60)
cout << "Pass";
else
cout << "Fail";
Switch Statements
When you use if statements, the condition is in bool. Switch
statements are very similar to the if statements. However, they
are in a different format.
This is how switch statements look like:
switch (variable type)
{
case value_here:
assignment statement here;
break;
case value1_here:
assignment statement here;
break;
....
default:
assignment statement here;
}
The case is equivalent to the if(condition). The default is
equivalent to the else in an if-else statement. The break
statement ends a section of code. It is very important to use the break
statement after every case in the switch statement, because
the assignment statement for that case can be shared with another
case when there is no break statement.
(The break statement will be explained later in the Loops
section.)
NEXT |