2. •In selection structure, the program is executed
based upon the given condition.
•Only instructions that satisfy the given
condition are executed.
• There are 4 types of selection structure:
• Simple if
• if…else
•nested if..else
•Else if ladder
3. A nested if…else statement is an if…else statement with another
if…else statements inside it.
Example :
if (score >= 90)
printf(“An”);
else if (score >= 80)
printf(“Bn”);
else if (score >= 70)
printf(“Cn”);
else if (score >= 60)
printf(“Dn”)
else
printf(“Fn”);
The else if statement means that if the above condition is not satisfied,
then try checking this condition.If any one of the condition is already
satisfied, then ignore the rest of the available conditions
4. if (test expression)
statements to be executed if
test expression is true;
else
if(test expression 1)
statements to be executed
if test expressions 1 is true;
else
if (test expression 2)
.
.
.
else
statements to be
executed if all test
expressions are false;
5. Write a C program to relate two integers entered by user using = or > or < sign.
#include <stdio.h>
int main(){
int numb1, numb2;
printf("Enter two integers to check".n);
scanf("%d %d",&numb1,&numb2);
if(numb1==numb2) //checking whether two integers are equal.
printf("Result: %d=%d",numb1,numb2);
else
if(numb1>numb2) //checking whether numb1 is greater than numb2.
printf("Result: %d>%d",numb1,numb2);
else
printf("Result: %d>%d",numb2,numb1);
return 0;
}
Output
Enter 2 integers to check
5
3
Result: 5>3
6. If the test expression is true, it will execute the
code before else part but, if it is false, the
control of the program jumps to the else part
and check test expression 1 and the process
continues. If all the test expression are false
then, the last statement is executed.
The ANSI standard specifies that 15 levels of
nesting may be continued.
7. Else if ladder means, an simple if statement occurs with in
the else part of simple if else statement.
if statement evaluates the condition inside the parenthesis
and if it is true, then it executes one line followed by if
statement or the sequence of steps bound of { }.
This construct is known as the else if ladder. The conditions
are evaluated from the top of the ladder to downwards. As
soon as a true condition is found, the statement associated
with it is executed and the control is transferred to the
statement-x (skipping the rest of the ladder). When all the n
conditions become false, then the final else containing the
default statement will be executed