Professional Documents
Culture Documents
5 ICT S112 Conditional Statements PDF
5 ICT S112 Conditional Statements PDF
if(condition)
{
statement;
}
if(condition)
{
statement 1;
}
else
{
statement 2;
}
ICT S112: FUNDAMENTALS OF COMPUTING 7
IF…ELSE STATEMENT
if(Grade>=75)
{
cout<<“ Congratulations, You Passed!”;
}
else
{
cout<<“ You failed, better luck next time!”;
}
if(condition) {
if(condition 1){
statement 1;
} else {
statement 2;
}
} else {
ICT S112: FUNDAMENTALS OF COMPUTING 9
statement 3;
}
NESTED IF STATEMENT
case 1:
cout<<“You selected 1.”;
break;
case 2:
cout<<“You selected 2”;
break;
default:
cout<<“Pls select a number from 1-2”;
}
ICT S112: FUNDAMENTALS OF COMPUTING 12
THE FOLLOWING RULES APPLY TO A SWITCH
STATEMENT
• The expression used in a switch statement must have an integral or enumerated
type or a class type in which the class has a single conversion function to an integral
or enumerated type.
• You can have any number of case statements within a switch. Each case is followed
by the value to be compared to and a colon.
• When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
• A switch statement can have an optional default case, which must appear at the end
of the switch. The default case can be used for performing a task when none of the
cases is true. No break is needed in the default case.