In a C program, a decision causes a one-time jump to a different part of the program, depending on the value of an expression. Decisions in C can be made in several ways. The most important is with the if...else statement, which chooses between two alternatives. This statement can be used without the else, as a simple if statement. Another decision control statement, switch, creates branches for multiple alternative sections of code, depending on the value of a single variable.
The if Statement
It is used to execute an instruction or sequence/block of instructions only if a condition is fulfilled. In if statements, expression is evaluated first and then, depending on whether the value of the expression (relation or condition) is “true” or “false”, it transfers the control to a particular statement or a group of statements.
Different forms of implementation if-statement are:
>> Simple if statement
It is used to execute an instruction or block of instructions only if a condition is fulfilled. The syntax is as follows:
if (condition)
statement;
where condition is the expression that is to be evaluated. If this condition is true,
statement is executed. If it is false, statement is ignored (not executed) and the
program continues on the next instruction after the conditional statement.
>> If … else statement
If…else statement is used when a different sequence of instructions is to be executed depending on the logical value (True / False) of the condition evaluated. Its form used in conjunction with if and the syntax is as follows:
if (condition)
Statement _1;
else
Statement_ 2;
statement_3;
If the condition is true, then the sequence of statements (Statements_1_Block) executes; otherwise the Statements_2_Block following the else part of if-else statement will get executed. In both the cases, the control is then transferred to Statements_3 to follow sequential execution of the program.
>> Nested if…else statement
In nested if… else statement, an entire if…else construct 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) {
Statements_1_Block;
} else {
Statements_2_Block;
}
} else {
Statements_3_Block;
}
Statement_4_Block;
Here, condition_1 is evaluated. If it is false then Statements_3_Block is executed and is followed by the execution of Statements_4_Block, otherwise if condition_1 is true, then condition_2 is evaluated. Statements_1_Block is executed when condition_2 is true otherwise Statements_2_Block is executed and then the control is transferred to Statements_4_Block.
>> Else if statement
To show a multi-way decision based on several conditions, we use the else if
statement. This works by cascading of several comparisons. As soon as one of the
conditions is true, the statement or block of statements following them is executed and
no further comparisons are performed. The syntax is as follows:
if (condition_1) {
Here, the conditions are evaluated in order from top to bottom. As soon as any condition evaluates to true, then the statement associated with the given condition is executed and control is transferred to Statements_x skipping the rest of the conditions following it. But if all conditions evaluate false, then the statement following final else is executed followed by the execution of Statements_x.
The Switch Statement
Its objective is to check several possible constant values for an expression, something similar to what we had studied in the earlier sections, with the linking of several if and else if statements. When the actions to be taken depending on the value of control variable, are large in number, then the use of control structure Nested if…else makes the program complex. There switch statement can be used. Its form is the following:
switch (expression){
case expression 1:
block of instructions 1
break;
case expression 2:
block of instructions 2
break;
.
.
default:
default block of instructions
}
It works in the following way: switch evaluates expression and checks if it is equivalent to expression1. If it is, it executes block of instructions 1 until it finds the break keyword, moment at finds the control will go to the end of the switch. If expression was not equal to expression 1 it will check whether expression is equivalent to expression 2. If it is, it will execute block of instructions 2 until it finds the break keyword.
Finally, if the value of expression has not matched any of the previously specified
constants (you may specify as many case statements as values you want to check), the
program will execute the instructions included in the default: section, if it exists, as it
is an optional statement.