CO110 Computer Programming6
CO110 Computer Programming6
if(conditional-expression1)
{
if (conditional-expression2)
{
statement-block-1 ;
}
else
{
statement-block-2 ;
}
}
else
{
statement-block-3 ;
}
statement-block-4;
Nested if … else statement cont…
In case of above Nested if….else statement
if ‘conditional-expression 1’ of outermost if statement
results false then control goes to statement-block3
with statement-block1 and 2 skipped out .
If true the control goes to check ‘conditional-
expression2’ of inner if statement .
If ‘conditional-expression2’ results true then ‘statement-
block1’ will be executed
Else if false control will jump to ‘statement-block2’ will be
executed.
Flowchart for nested if else statement
C program to demonstrate nested if else statement
#include<stdio.h>
main()
{ Note the double
int num1,num2,num3; forward slashes used
printf("Enter three numbers: "); below
scanf("%d %d %d", &num1, &num2, &num3);
Comment lines to improve
if (num1>=num2) readibility of code
{
if(num1>=num3) //Nested if statement within if
printf("Largest number = %d",num1);
else
printf("Largest number = %d“,num3); //Nested else statement within if
}
else
{
if(num2>=num3) //Nested if statement within else
printf("Largest number = %d“,num2);
else //Nested else statement within else
printf("Largest number = %d“,num3);
}
}
C programs for nested if else statement
Program to check if a given year is a leap year or not.
Hint: a leap year is a year divisible by 4. Also if it is divisible by 400 but
not 100. E.g 1900 not a leap year but 2000 is a leap year.
switch(expression)
{
case value-1: block-1;
break;
case value-2: block-2;
break;
case value-3: block-3;
break;
default: block-4;
}
switch statement cont…..
In switch statement the value of a given variable or
expression is compared against a list of case values.
When a match is found , the block of statement inside that
particular case is executed.
When a match is not found, the default case statement block
is executed.
It is not mandatory to have a default case.
We use a break statement at the end of each case block to
prevent execution of subsequent cases.
start:
printf(“ enter the number”);
scanf(“%d”, &num); Now if we enter a number 7 for e.g
if (num <=0) 7 % 3 == 1
goto start;
It will skip the goto check label and
if( num%3==0) execute the printf(“ Not divisible by 3”);
goto check;
printf(“ Not divisible by 3”); It will then execute the check label which
goto end; is not required.
check: if ( num%2==0 ) To skip this we have a new label end
{
printf(“ divisible by both 2 and 3”);
}
end: printf(“ Skipped check label”);
}
Which are the unconditional branching statements in
C? Explain with an example.
Explain the different forms of “if” statement.
Why is switch called a multi-decision making
statement?
Mention the differences between Nested if-else and
switch statement.