Sometimes using only
'if'
statement is not enough. Suppose we have a program to print whether the input number (say variable 'i') is odd or even. The decision part of the program is as follows
- if ((i % 2) == 1)
- {
- printf("Odd");
- }
This way we can decide whether the number is odd or not. But to decide whether it is even, we have to again write almost same expression with changing the value 1 to 0. But C provides us a different solution to reduce the number of lines of codes that you need to write. It is called
'if ... else'
statement, which is used to handle the false case of the 'if'
expression.
Syntax of
'if ... else'
statement is as given below:
- if (expression)
- {
- statement block1
- } else {
- statement block2
- }
In the above syntax, if the
'expression'
evaluates to true, 'statement block1'
will be executed. If the 'expression'
evaluates to false, 'statement block2'
will be executed.
Example of 'if-else'
Statement
Using
'if ... else'
statement we can write code to check a number is even or odd as given below.
- #include <stdio.h>
- int main(void) {
- int i = 10;
- if ((i % 2) == 1)
- {
- printf("%d is odd number", i);
- }
- else
- {
- printf("%d is even number", i);
- }
- return 0;
- }
- Output:
- 10 is even number
Multi-way decision
We learnt in the previous section of this tutorial that,
if-else
statement is good for situations when you want to execute two different set of statements based on the result of a test expression. But what if we need to execute different sets of statements based on evaluation of more than one test expression? Will it be possible to express this situation with if-else
only ? C has a great solution for this situation, called multi-way decision. Control structures with multi-way decision allow us to cover more than two possibilities. There are two options in which we can implement multi-way control structures in C. They are- Nested
'if'
statements or'if...else if...else'
statements 'switch'
statements
Nested 'if'
Statements or 'if...else if...else'
Statements
Nested
'if'
statement is similar to 'if-else'
statement with the addition of more than one'if-else'
block. The syntax is given below.
- if (expression 1)
- {
- statement block 1
- }
- else if (expression 2)
- {
- statement block 2
- }
- ...
- ...
- else if (expression n)
- {
- statement block n
- }
- else
- {
- statement block n+1
- }
While executing nested
'if'
statement, first 'expression 1'
will be checked and if it evaluates to true 'statement block1'
will be executed. If it is false, then 'expression 2'
will be tested and it proceeds further in the same fashion.
Example of Nested 'if'
Statement
Below program checks if a given number is positive, negative or zero using using nested
'if'
statement.
- #include <stdio.h>
- int main(void) {
- int i = -1;
- if (i == 0)
- {
- printf("%d is zero", i);
- }
- else if (i > 0)
- {
- printf("%d is positive", i);
- }
- else if (i < 0)
- {
- printf("%d is negative", i);
- }
- return 0;
- }
- Output:
- -1 is negative
No comments:
Post a Comment