Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

DECISION MAKING -

BRANCHING

1/77
SELECTION STRUCTURE:
NESTED IF-ELSE STATEMENT

• We can write an entire if else construct within


the body of an if statement or else statement.

• This known as nested if statements.


SELECTION STRUCTURE:
NESTED IF-ELSE STATEMENT

This kind of representation can end up with dangling else


problem.
SELECTION STRUCTURE:
NESTED IF-ELSE STATEMENT
In this nested form, condition_1 is evaluated. If it is zero (FALSE),
statement_1 is executed and the entire nested if statement is terminated.
If non-zero (TRUE), control goes to the second if (within the first if) and
condition_2 is evaluated.
If it is zero (FALSE), statement_2 is executed; if not, control goes to the third if
(within the second if) and condition_3 is evaluated.
If it is zero (FALSE), statement_3 is executed; if not, statement_4 is executed.
The statement_4 (inner most) will only be executed if all the if statement are
TRUE.
Again, only one of the statements is executed other will be skipped.
If the else is used together with if, always match an else with the nearest if
before the else.
statements_x can be a block of codes and must be put in curly braces.
SELECTION STRUCTURE:
NESTED IF-ELSE STATEMENT
SELECTION STRUCTURE:
NESTED IF-ELSE STATEMENT
FLOWCHART
EXAMPLE 1
EXAMPLE 2

 Given an integer, n, perform the following conditional


actions:

If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print Not
Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird
MCQS
}

Answer:A
Answer: else loop executed;j=1
Answer:B
Answer:A
Answer:B
Answer:C
Answer:B
Which of the following statements are correct about the
below program?
#include <stdio.h>
int main()
{
int i = 10, j = 15; if (i % 2 = j % 3)
printf("REC");
return 0;
}
A. Error: Expression syntax
B. Error: Lvalue required
C. Error: Rvalue required
D. The Code runs successfully

You might also like