Monday, February 15, 2016

'if else' Statement

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
  1. if ((i % 2) == 1)
  2. {
  3. printf("Odd");
  4. }
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:
  1. if (expression)
  2. {
  3. statement block1
  4. } else {
  5. statement block2
  6. }
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.
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int i = 10;
  5. if ((i % 2) == 1)
  6. {
  7. printf("%d is odd number", i);
  8. }
  9. else
  10. {
  11. printf("%d is even number", i);
  12. }
  13. return 0;
  14. }
  15.  
  16. Output:
  17. 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-elseonly ? 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
  1. Nested 'if' statements or 'if...else if...else' statements
  2. '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.
  1. if (expression 1)
  2. {
  3. statement block 1
  4. }
  5. else if (expression 2)
  6. {
  7. statement block 2
  8. }
  9. ...
  10. ...
  11. else if (expression n)
  12. {
  13. statement block n
  14. }
  15. else
  16. {
  17. statement block n+1
  18. }
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.
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int i = -1;
  5. if (i == 0)
  6. {
  7. printf("%d is zero", i);
  8. }
  9. else if (i > 0)
  10. {
  11. printf("%d is positive", i);
  12. }
  13. else if (i < 0)
  14. {
  15. printf("%d is negative", i);
  16. }
  17. return 0;
  18. }
  19.  
  20. Output:
  21. -1 is negative

No comments: