Professional Documents
Culture Documents
DECISION Statements
DECISION Statements
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.
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
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.
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.
THE if–else STATEMENT
The if–else statement takes care of the true and false conditions.
It has two blocks.
One block is for if and it is executed when the condition is true.
The other block is of else and it is executed when the condition is false.
The else statement cannot be used without if.
No multiple else statements are allowed with one if (see Figures 3 and 4).
The flow chart for the if–else statement is given in Figure 4.
Program to find the roots of a quadratic equation by using if-else condition.
Explanation:
The user can enter the values of a, b and
c in the above program.
The terms b 2 and 4 a c are evaluated.
The if condition checks whether b 2 is
greater than 4 a c.
If true, x1 and x2 are evaluated and
printed; otherwise message displayed
will be ‘Roots are Imaginary’.
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.
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.
Syntax of if-else-if statement can be given as follows:
Explanation:
In the above program, a number is entered.
The number is checked for even or odd with modules
division operator.
When the number is even, the goto statement transfers
the control to the label even.
Similarly, when the number is odd the goto statement
transfers the control to the label odd and respective
message will be displayed.
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:
Explanation: