Topic: Decision Control
Topic: Decision Control
* The Expression is evaluated first, if the value of Expression is true (or non zero) then Statement1 will be
executed; otherwise if it is false (or zero), then Statement1 will be skipped and the execution will jump to the
Statement2.
2)if-else statement:
The "if-else" statement allows you to provide an alternative block of code to be executed when the
condition is false
The if..else statement is an extension of simple if statement.
If the Expression is true (or non-zero) then Statement1 will be executed; otherwise if it is
false (or zero), then Statement2 will be executed.
.
Nested if .. else statement: When a series of decisions are involved, we have to use more than
one if..else statement in nested form as shown below in the general syntax.
If Expression1 is true, check for Expression2, if it is also true then Statement1 is executed.
* If Expression1 is true, check for Expression2, if it is false then Statement2 is executed.
* If Expression1 is false, then Statement3 is executed.
4. else-if ladder or cascaded if else: There is another way of putting ifs together when multipath
decisions are involved. A multi path decision is a chain of ifs in which the statement associated
with each else is an if. It takes the following form
The conditions are evaluated from the top (of the ladder), downwards. As soon as true condition is found, the
statement associated with it is executed and control transferred to the Next statement skipping the rest of the
ladder.
*When all conditions are false then the final else containing the default Statement4 will be executed.
. Switch Statement: C language provides a multi-way decision statement so that complex else-if statements
can be easily replaced by it.
* C languages multi-way decision statement is called switch.
If the choice matches to label1 then block1 will be executed else if it evaluates to label2 then block2 will be
executed and so on.
* If choice does not matches with any case labels, then default block will be executed.
QUIZ