C - Decision Making


Decision making in C


It is about deciding the order of the statement based on certain conditions or a group of statements are repeated until certain specified condition are met. C language handles decision making by supporting the following statements: -
·         If statement
·         switch statement
·         conditional operator statement (? : operator)
·         goto statement
Decision making with ‘if’ statement

Depending on the complexity of conditions to be tested, the ‘if’ statement may be implemented in different forms. Following are the different forms: -
1.   Simple if statement
2.   If-else statement
3.   Nested if-else statement
4.   Using else-if statement


1.  Simple ‘if’ statement

The general form of a simple ‘if’ statement is,
if(expression)
{
    statement inside;
}
    statement outside;
The statement-inside will be executed only when the expression returns true, otherwise only the statement-outside is executed and statement-inside is skipped.
Ex:
#include <stdio.h>
 
void main( )
{
    int x, y;
    x = 15;
    y = 13;
    if (x > y )
    {
        printf("x is greater than y");
    }
}

Output:
x is greater than y

2. if...else statement

The general form of a simple if-else statement is,
if(expression)
{
    statement block1;
}
else
{
    statement block2;
}
Statement-block1 is executed if the expression is true, else statement-blocck2 is executed and Statement-block1 is skipped.
Ex:
#include <stdio.h>
 
void main( )
{
    int x, y;
    x = 15;
    y = 18;
    if (x > y )
    {
        printf("x is greater than y");
    }
    else
    {
        printf("y is greater than x");
    }
}

Output:
y is greater than x

3. Nested if-else statement

The general form of a nested if-else statement is,
if( expression )
{
    if( expression1 )
    {
        statement block1;
    }
    else 
    {
        statement block2;
    }
}
else
{
    statement block3;
}
Statement-block3 will be executed if expression is false, otherwise execution continues and enters into the first if to perform the check for next if block, where the statement-block1 is executed if expression 1 is true otherwise statement-block2 is executed.

Ex:
#include <stdio.h>
 
void main( )
{
    int a, b, c;
    printf("Enter 3 numbers...");
    scanf("%d%d%d",&a, &b, &c);
    if(a > b)
    { 
        if(a > c)
        {
            printf("a is the greatest");
        }
        else 
        {
            printf("c is the greatest");
        }
    }
    else
    {
        if(b > c)
        {
            printf("b is the greatest");
        }
        else
        {
            printf("c is the greatest");
        }
    }
} 


4. else-if ladder
The general form of else-if ladder is,
if(expression1)
{
    statement block1;
}
else if(expression2) 
{
    statement block2;
}
else if(expression3 ) 
{
    statement block3;
}
else 
    default statement;
After expression is tested from top (of the ladder) downwards and is true condition is found, the statement associated with it is executed.

Ex:
 
#include <stdio.h>
 
void main( )
{
    int a;
    printf("Enter a number...");
    scanf("%d", &a);
    if(a%5 == 0 && a%8 == 0)
    {
        printf("Divisible by both 5 and 8");
    }  
    else if(a%8 == 0)
    {
        printf("Divisible by 8");
    }
    else if(a%5 == 0)
    {
        printf("Divisible by 5");
    }
    else 
    {
        printf("Divisible by none");
    }
}

Important Points:

1.   In ‘if’ statement, if there is only one statement, it can be included without enclosing it into curly braces { ... }
2.    int a = 5;
3.    if(a > 4)
    printf("success");
No curly braces are required in the above given case, but if we have more than one statement inside ‘if’ condition, then we have to enclose them inside curly braces.
4.   ‘==’ must be used for comparison in the expression of ‘if’ condition, if we use ‘=’ then it performs assignment not comparison therefore expression will always return true.
5.   Other than 0(zero), all other values are considered as true.
6.    if(27)
    printf("hello");
hello will be printed in above example.


No comments:

Post a Comment