CSC 183 Chap-6
CSC 183 Chap-6
Programming C
Chapter - 6
1) if statement
2) switch statement
4) goto statement
if ( test condition )
statements;
• Example:
scanf( “ %f “ , &marks );
if( marks >= 80 )
printf( "Yes, the student get A+“ );
if ( test condition )
statements;
• Example:
Entry
Test True
Expression
?
Statement-block
False
Statement - x
Next Statement
• Example:
if( num % 2 == 0 )
printf( “Even number’’ );
else
printf( “Odd number” );
Entry
Statement - x
if(a>b)
if ( condition ) {
if ( condition ) if(a>c)
printf("a is the largest:%d",a);
statement1; else
else printf("c is the largest:%d",c) ;
statement2; }
else
else {
statement3 ; if(c>b)
printf("c is the largest:%d",c);
else
printf("b is the largest:%d",b) ;
}
31 January 2023 CSC-183 14
nested if_else Statement (Example)
int main()
{
float salary, bonus;
if(gender == FEMALE)
{
if(salary > 10000)
bonus = salary * 0.5;
else
bonus = salary *0.2;
}
else
{
bonus = salary * 0.3;
}
salary = salary + bonus;
return 0;
}
Format: index=marks/10;
switch (index)
{
switch ( expression ) case 10:
case 9:
{
case 8:
case value-1: printf(" Honours ");
statements; break;
case 7:
break; case 6:
case value-2: printf(“ First Division ");
statements; break;
case 5:
break; printf(“ Second Division ");
............... break;
case 4:
...............
printf(“ Third Division ");
default: break;
statements; default:
printf( “ Fail ");
break; break;
} }
31 January 2023 CSC-183 22
switch Statement (cont.)
• The expression is an integer expression or characters.
• Each case value should be unique within a switch statement.
• case labels end with a colon (:).
• When switch is executed, the value of the expression is
compared against the value value-1, value-2 …. If a case
matched then the block of statements are executed.
• value in case should be constant, error if variable use.
• The break statement at the end of each block indicates end of
particular case and exit from switch statement.
• The switch also permitted to use nested switch statement.
• The default is an optional case.
– If present: executed if other cases value are not matched.
– If not present: no action if all cases are failed.
31 January 2023 CSC-183 23
switch Statement (flowchart)