PSPC Unit II A Decision Control Structure
PSPC Unit II A Decision Control Structure
UNIT –II
• if Statements if (Condition) {
statements;
}
• switch…case Statements
switch (n)
{
case 1: // will execute if n = 1;
break;
case 2: // will execute if n = 2;
break;
default: // will execute if n doesn't match with any cases
}
• while loop
true
condition statement
false
• for loop
Initialize variable
Condition
true statement
Test the variable
Increment/Decrement
variable
false
• do…while loop
do {
statement
statement
} while ( condition );
true
condition
false
• Nested Loops??
• Break
Ex:
– Causes immediate exit from int next = 0;
a while, for, do/while while (true){
or switch statements Scanf(“%d”,&next);
if (next < 0)
• Continue
break;
– Skips the remaining if (next % 2) //odd number, don’t print
statements in the body of a continue;
while, for or do/while printf(“%d\n”,next);
structure and proceeds with }
the next iteration of the printf( “Out of the Loop!”);
loop
Thanks