C has three major decision making instructions—the if
statement, the if-else
statement, and the switch
statement.
1. The if Statement
C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this:
//for single statement
if(condition)
statement;
//for multiple statement
if(condition)
{
block of statement;
}
The more general form is as follows:
//for single statement
if(expression)
statement;
//for multiple statement
if(expression)
{
block of statement;
}
Here the expression can be any valid expression including a relational expression. We can even use arithmetic expressions in the ifstatement. For example all the following if
statements are valid
if (3 + 2 % 5)
printf("This works");
The expression (3 + 2 % 5)
evaluates to 5 and since 5 is non-zero it is considered to be true. Hence the printf("This works");
gets executed.
2. The if-else Statement
The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements ifthe expression evaluates to false? Of course! This is what is the purpose of the else statement that is demonstrated as
if (expression)
{
block of statement;
}
else
statement;
Note
- The group of statements after the
if
upto and not including theelse
is called anif block
. Similarly,the statements after theelse
form theelse block
. - Notice that the
else
is written exactly below theif
. The statements in theif block
and those in theelse block
have been indented to the right. - Had there been only one statement to be executed in the
if block
and only one statement in theelse block
we could have dropped the pair of braces. - As with the
if
statement, the default scope ofelse
is also the statement immediately after theelse
. To override this default scope a pair of braces as shown in the above "Multiple Statements within if" must be used.
3. Nested if-elses
If we write an entire if-else
construct within either the body of the if
statement or the body of an else
statement. This is called "nesting" of ifs
. This is demonstrated as -
if (expression1)
statement;
else
{
if (expression2)
statement;
else
{
block of statement;
}
}
4. The if-else Ladder/else-if Clause
The else-if
is the most general way of writing a multi-way decision.
if(expression1)
statement;
else if(expression2)
statement;
else if(expression3)
statement;
else if(expression4)
{
block of statement;
}
else
statement;
The expressions are evaluated in order; if an expressionis true, the "statement" or "block of statement" associated with it is executed, and this terminates the whole chain. As always, the code for each statement is either a single statement, or a group of them in braces. The last else
part handles the "none of the above" or default case where none of the other conditions is satisfied.
5. Switch Statements or Control Statements
The switch
statement is a multi-way decision that tests whether an expression matches one of a number of constant
integer values, and branches accordingly. The switch
statement that allows us to make a decision from the number of choices is called a switch
, or more correctlya switch-case-default
, since these three keywords go together to make up the switch
statement.
switch (expression)
{
case constant-expression:
statement1;
statement2;
break;
case constant-expression:
statement;
break;
...
default:
statement;
}
- In
switch…case
command, eachcase
acts like a simple label. A label determines a point in program which execution must continue from there. Switch statement will choose one ofcase
sections or labels from where the execution of the program will continue. The program will continue execution until it reachesbreak
command. break
statements have vital rule inswitch
structure. If you remove these statements, program execution will continue to next case sections and all remaining case sections until the end ofswitch
block will be executed (while most of the time we just want onecase
section to be run).default
section will be executed if none of the case sections match switch comparison.
6. switch Versus if-else Ladder
There are some things that you simply cannot do with a switch
.
These are:
- A float expression cannot be tested using a switch
- Cases can never have variable expressions (for example it is
wrong to say case a +3 :) - Multiple cases cannot use same expressions.
7 . Quiz
Test what you learn from this tutorial. Click here to give a quiz based on this tutorial.
8. Examples
Example Statement for If Statement and Switch Case in C Language |
---|
1. Find Perfect Leap Year |
2. ATM money dispatch count while currencies are 1000,500 and 100 |
3. Display day for the entered date by user |
4. Switch case (Resolve Error) |
Next - Loops (for, while, do-while)