DECISION Statements
DECISION Statements
Decision-making statements in a programming language help the programmer to transfer the control from one part to
other parts of the program.
Thus, these decision-making statements facilitate the programmer in determining the flow of control.
This involves a decision-making condition to see whether a particular condition is satisfied or not.
On the basis of real time applications it is essential:
Besides, the C also supports other control statements such as continue, break.
The decision-making statement checks the given condition and then executes its sub-block.
The decision statement decides which statement to execute after the success or failure of a given
condition.
The conditional statements use relational operators.
The relational operators are useful for comparing the two values.
They can be used to check whether they are equal to each other, unequal or one is smaller/greater than the other.
The reader or the programmer is supposed to understand the concepts as cited above.
Following points are expected to be known to the programmer related to the decision-making statements.
Sequential execution:
The statements in the program are executed one after another in a sequential manner.
This is called the sequential execution.
Transfer of control:
The C statements such as if, goto, break, continue, switch allow a program to transfer the control at different places in
the program.
This is accomplished by skipping one or more statements appearing in a sequential flow.
This jumping of control is called the transfer of control.
THE if STATEMENT
C uses the keyword if to execute a set of command
lines or one command line when the logical condition
is true.
It has only one option.
The sets of command lines are executed only when the
logical condition is true (see Figure 1).
The if statement contains an expression which is evaluated. If the expression is true it returns 1,
otherwise 0.
The statement is executed when the condition is true. In case the condition is false, the compiler skips the lines within
the if block.
The condition is always enclosed within a pair of parentheses.
The conditional statements should not be terminated with semi-colons (;).
The statements following the if statement are normally enclosed within curly braces.
The curly braces indicate the scope of the if statement.
The default scope is one statement.
But it is good practice to use curly braces even if a single statement is used following the if condition.
Program to check whether the entered number is less than 10. If yes, display the same.
Fig(2): The if statement (flow of control)
Explanation:
In the above program, the user can enter the number.
The entered number is checked with the if statement.
If it is less than 10, a message ‘Number entered is less than 10’ is displayed.
For the sake of understanding, Figure 2 is given for the above program.
Explanation:
The user can enter the values of a, b and c in the
above program.
NESTED if–else STATEMENTS
In this kind of statement, a number of logical conditions are checked for executing various statements.
Here, if any logical condition is true the compiler executes the block followed by if condition, otherwise it skips and
executes the else block.
In the if–else statement, the else block is executed by default after failure of condition
In order to execute the else block depending upon certain condition we can add, repetitively, if statements in else block.
This kind of nesting will be unlimited.
Figure 5 describes the nested if–else–if blocks.
Syntax of nested if-else statement can be given as
follows:
From the above block, following rules can be described for applying nested if-else-if statements:
2. If the condition is true control passes to the block following first if.
In that case, we may have one more if statement whose condition is again checked.
This process continues till there is no if statement in the last if block.
In this kind of statement, a number of logical conditions are checked for executing various statements.
Here, if the first logical condition is true the compiler executes the block followed by first if condition,
otherwise it skips that block and checks for next logical condition followed by else-if, if the condition
is true the block of statements followed by that if condition is executed.
The process is continued until a true condition is occurred or an else block is occurred.
If all if conditions become false, it executes the else block.
In the if-else-if ladder statement, the else block may or may not have the else block.
In if-else-if ladder statement we do not have to pair if statements with the else statements that is we do not have to
remember the number of braces opened like nested if-else.
So it is simpler to code than nested if-else and having same effect as nested if-else.
The statement is named as if-else-if ladder because it forms a ladder like structure as shown in Figure 6.
Explanation:
In the above program, a number is entered.
The number is checked for even or odd with modules division
operator.
THE switch STATEMENT
The switch expression should not be terminated with a semi-colon and/or with any other
symbol.
The entire case structure following switch should be enclosed with curly braces.
The keyword case is followed by a constant.
Every constant terminates with a colon.
Each case statement must contain different constant values.
Any number of case statements can be provided.
If the case structure contains multiple statements, they need not be enclosed with curly braces.
Here, the keywords case and break perform the job of opening and
closing curly braces, respectively.
When one of the cases satisfies, the statements following it are executed.
In case, there is no match, the default case is executed.
The default can be put anywhere in the switch expression.
The switch statement can also be written without the default statement.
The break statement used in switch causes the control to go outside the switch block.
By mistake, if no break statements are given all the cases following it are executed
(see Figure).
Program to print lines by selecting the choice:
Explanation:
Explanation:
In the above-given program, the first switch statement is used for displaying
the message such as even or odd numbers when the entered numbers are 0
and 1, respectively.
THE switch CASE AND NESTED ifs
The distinction between the switch case and the nested ifs is narrated in Table.
Explanation: