Professional Documents
Culture Documents
Lecture - 06 CSC 183 Chap-5
Lecture - 06 CSC 183 Chap-5
Lecture - 06 CSC 183 Chap-5
Programming C
Chapter - 5
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) ;
}
Monday, December 27, 2021 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;
}
• Sequential
In a sequential approach, all the statements are
executed in the same order as it is written
• Selectional
In a selectional approach, based on some conditions,
different set of statements are executed
• Iterational (Repetition)
In an iterational approach certain statements are
executed repeat
Selectional Control Structures
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;
} }
Monday, December 27, 2021 CSC-183 25
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.
Monday, December 27, 2021 CSC-183 26
switch Statement (flowchart)
int i = 2;
switch(i){
case 1:
printf(“ONE”);
break;
case 2: Output:
printf(“TWO”);
break; TWO
case 3:
printf(“THREE”);
break;
default:
printf(“INVALID”);
break;
}
What is the output of the following code snippet?
switch (departmentCode){
case 01 :
printf(“CE”);
break;
case 02 :
printf(“EEE”);
break;
case 03 :
printf(“ME”);
break;
case 04 :
printf(“CSE”);
}
What is the output of the following code snippet?
int iNum = 2;
switch(iNum) {
case 1.5:
printf(“ONE AND HALF”); Case 1.5: this is invalid
break; because the values in
case statements must be
case 2: integers
printf(“TWO”);
break;
case ‘A’ :
printf(“A character”);
}
An Example – switch case
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
printf("Enter the vowel:");
scanf("%c",&ch);
switch(ch) {
case 'a' : printf("Vowel");
break;
case 'e' : printf("Vowel");
break;
case 'i' : printf("Vowel");
break;
case 'o' : printf("Vowel");
break;
case 'u' : printf ("Vowel");
break;
default : printf("Not a vowel");
}
getch();
return 0;
}
The ?: Operator (Conditional Operator)
• The C has an unusual operator, useful for making two-way
decisions. This operator is a combination of ? and :
• It has three parts and takes three operands.
• Format:
conditional_expression ? expression-1 : expression-2
max = (a>b?a:b)
• The conditional expression is evaluated first.
• If the result is nonzero, expression-1 is evaluated.
• Otherwise expression-2 is evaluated.