Previous Index Next

Sometimes the program needs to be executed depending upon a particular condition. C provides the following statements for implementing the selection control structure.

  • if statement
  • if else statement
  • nested if statement
  • switch statement

if statement

syntax of the if statement
if (condition)
{
   statement(s);
}

If the condition is true, statement is executed; otherwise it is skipped. The statement may either be a single or compound statement.

/* This program illustrates if statement. */

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);
    
    if (number < 0)
    {
        number =  - number;
    }
    
    printf("The absolute value of number is %d", number);

    return 0;
}

Output:

Enter a number: -15
The absolute value of number is 15

if else statement

syntax of the if - else statement

if (condition)
   statement1;
else
   statement2;

The given condition is evaluated first. If the condition is true, statement1 is executed. If the condition is false, statement2 is executed. It should be kept in mind that statement and statement2 can be single or compound statement.

/* This program illustrates if..else statements */

#include <stdio.h>

int main()
{
    int num1, num2;

    printf("Enter two integers :");
    scanf("%d%d", &num1, &num2);

    if (num1 > num2)
    {
        printf("Largest number is %d.", num1);
    }
    else
    {
        printf("Largest number is %d.", num2);
    }

    return 0;
}

Output:

Enter two integers :25 19
Largest number is 25.

Nested if statement

The if block may be nested in another if or else block. This is called nesting of if or else block.

syntax of the nested if statement

if(condition 1)
{
  if(condition 2)
  {
    statement(s);
  }
}

Sometimes you want to check for a number of related conditions and choose one of several actions. One way to do this is by chaining a series of ifs and elses. Look at the following code segment :

if(condition 1)
  statement 1;
else if (condition 2)
  statement2;
else
  statement3;

/* This program illustrates if elseif statement */
      
#include <stdio.h>

int main()
{
    int num1, num2, num3;

    printf("Enter three integers :");
    scanf("%d%d%d", &num1, &num2, &num3);

    if (num1 > num2 && num1 > num3)
    {
        printf("Largest number is %d.", num1);
    }
    else if (num2 > num3)
    {
        printf("Largest number is %d.", num2);
    }
    else
    {
        printf("Largest number is %d.", num3);
    }

    return 0;
}

Output

Enter three integers :8 18 11
Largest number is 18.

switch statement

The syntax of switch statement is:

switch (var / expression)
{
   case constant1 : statement 1;
   break;
   case constant2 : statement2;
   break;
   .
   .
   default: statement3;
   break;
}

The execution of switch statement begins with the evaluation of expression. If the value of expression matches with the constant then the statements following this statement execute sequentially till it executes break. The break statement transfers control to the end of the switch statement. If the value of expression does not match with any constant, the statement with default is executed.

Some important points about switch statement

  • The expression of switch statement must be of type integer or character type.
  • The default case need not to be used at last case. It can be placed at any place.
  • The case values need not to be in specific order.
/* This program illustrates switch.. case statement */

#include <stdio.h>

int main()
{
    int day;

    printf("Enter number 1-7 :");
    scanf("%d", &day);

    /* Determine the corresponding week's day */
    switch (day)
    {
        case 1:
            printf("Sunday");
            break;

        case 2:
            printf("Monday");
            break;

        case 3:
            printf("Tuesday");
            break;

        case 4:
            printf("Wednesday");
            break;

        case 5:
            printf("Thursday");
            break;

        case 6:
            printf("Friday");
            break;

        case 7:
            printf("Saturday");
            break;

        default:
            printf("Invalid input");
    }

    return 0;
}

Output:

Enter number 1-7 :5
Thursday

Previous Index Next

privacy policy