if else statement and flowchart
Decision Control Statements and Flowchart
The if Statement
It is used to execute an instruction or sequence/block of instruction only if a condition is fulfilled.
Difference forms of implements if-statement are:
- Simple if statement
- if-else statement
- Nested if-else statement
- else if statement
Figure: Simple if statement syntax and flowchart |
Figure: if-else statement syntax and flowchart |
Nested if-else statement
In nested if...else statement, an entire if...elseconstruct is written within either the body of the if statement or the body of an else statement.
The syntax is as follows:
if(condition_1)
{
if(condition_2)
{
block statement_1;
}
else
{
block statement_2;
}
}
else
{
block statement_3;
}
block statement_4;
Figure: nested if-else statement flowchart |
Else if statement
It is used in multi-way decision on several conditions. This works by cascading comparisons. As soon as one of the conditions is true, the statement or block of statements following them is executed and no further comparison are performed.
The else...if syntax is as follows:
if(condition_1)
block statement_1;
else if(condition_2)
block statement_2;
else if(condition_n)
block statement_n;
else
default statement;
Figure: flowchart for else...if statement |
No comments:
Post a Comment