C++ CONDITIONAL STATEMENTS
Engr. Monette Loy-a
C++ CONDITIONAL STATEMENTS
IF/Else Statement
Switch Statement
C++ CONDITIONAL STATEMENTS
If/E ls e s tatement
S chematic Repres entation of an if
s tatement
C++ CONDITIONAL STATEMENTS
Syntax:
A.
if (condition)
s ingle s tatement;
B.
if (condition)
{
s tatement1;
s tatement2;
//..... a n d s o o n a s lo n g a s y o u w a n t
s tatement_las t;
}
C++ CONDITIONAL STATEMENTS
C.
if (c o nd itio n )
s tate m e nt1 ;
els e
s tate m e nt2;
C++ CONDITIONAL STATEMENTS
D. Ladderized if/else
if (condition1)
s tatements 1 ;
els e if (condition2)
s tatements 2
els e if (condition3)
s tatements 3 ;
…
els e
s tatements 4 ;
C++ CONDITIONAL STATEMENTS
S chematic Repres entation of an ladderized els e if s tatement
C++ CONDITIONAL STATEMENTS
E. Nested If/else
if (c o nd itio n 1)
if(condition2)
s tate m e nt1 ;
els e
s tate m e nt2;
els e
s tate m e nt3;
C++ CONDITIONAL STATEMENTS
Sample Problems
1. Write a program that determines if the input age is
qualified to vote or not. The qualifying age is 18 years old
and above.
2. Write a program that determines if the input number is
ODD or EVEN number.
C++ CONDITIONAL STATEMENTS
3. Write a program to display the high school level of a student,
based on its year-entry. For example, the year-entry 1 means
the student is a freshman, 2 for sophomore, and so on. Here
are the given criteria:
Year-entry number High school level
1 Freshman
2 Sophomore
3 Junior
4 Senior
other entry number “Out of School”
C++ CONDITIONAL STATEMENTS
4. Write a program to assist a teacher in calculating student
grades at the end of the semester. It accepts a numerical
grade as input, then it will display the character grade as
output, based on the given scale:
Range Grade
90 and above A
80-89 B
70-79 C
60-69 D
below 60 F
C++ CONDITIONAL STATEMENTS
5. Write a program that will displays an equivalent color once
an input letter match its first character. For example b for
Blue, r for Red, and so on. Here are the given criteria:
Letters Colors
‘B’ or ‘b’ Blue
‘R’ or ‘r’ Red
‘G’ or ‘g’ Green
‘Y’ or ‘y’ Yellow
other letters “Unknown Color”
C++ CONDITIONAL STATEMENTS
S witch s tatement
The switch statement is a multi-way decision that tests
whether an expression matches one of a number of
constant integer values. If it matches, then the statements
which follow are all executed.
C++ CONDITIONAL STATEMENTS
Syntax:
s witch (expres s io n ) {
cas e co n s ta n t1:
blo ck o f in s tru ctio n s 1
break;
cas e co n s ta n t2:
blo ck o f in s tru ctio n s 2
break;
.
.
.
default:
d efa u lt blo ck o f in s tru ctio n s
}
C++ CONDITIONAL STATEMENTS
Several points need watching:
You must not use floats in switch statements.
Expression evaluated by switch must evaluate to an integer.
The expression evaluated by switch should match a case. This
means that the matching case must also be an integer or a constant
expression which evaluates to an integer. By `constant expression'
I mean that variables are not allowed.
A default case should be included to catch the integers which don't
match any of the cases.You will see examples of these shortly.
Letters (not words) are seen as integers by C and C++. So letters
(not words) can be used in case statements.
C++ CONDITIONAL STATEMENTS
Once the case statement has been matched then a ll the
following statements in the switch statement are executed
unless you specifically stop them. So if the first case
statement happens to be matched, then every single one of
the statements in the entire switch statement will be run
unless you put a stop to it. This behavior is very different
from the if ... else if statements.
You stop execution of any further statements by using a break
statement. This breaks out of the switch statement and
continues execution with whatever code follows the closing
brace of the switch.
C++ CONDITIONAL STATEMENTS
sw itch example if-else equivalent
s witch (x) { if (x == 1)
cas e 1: {
cout << "x is 1";
cout << "x is 1";
}
break; els e if (x == 2)
cas e 2: {
cout << "x is 2"; cout << "x is 2";
break; }
default: els e
{
cout << "value of x
unknown"; cout << "value of x
unknown";
} }