Home C Programming Tutorial Control Statements in C

Control Statements in C

by anupmaurya
4 minutes read

Control Statements in C are used to execute/transfer the control from one part of the program to another depending on a condition. 

To control the flow of program execution in c programming languages. C provides two styles of flow control:

  • Branching (or) Selectional statements
  • Looping ( or ) Iterative statements

Branching is deciding what actions to take and looping is deciding how many times to take a certain
action.
Selectional statement are used to make one-time decisions in C Programming, that is, to execute
some code/s and ignore some code/s depending upon the test expression.

Types of Control Statement in C

Types of Control Statements in C .Also support an unconditional set of branching statements that transfer the control to another location in the program.

Selection declarations in C.

  1. If statements
  2. Switch Statement
  3. Conditional Operator Statement
  4. Goto Statement
  5. Loop Statements

C If Statements

If statement enables the programmer to choose a set of instructions, based on a condition. When the condition is evaluated to true, a set of instructions will be executed and a different set of instructions will be executed when the condition is evaluated to false.

if (test expression) 
{
   // code
}

Example : if statement

// Program to display a number if it is negative

#include <stdio.h>
int main() {
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    // true if number is less than 0
    if (number < 0) {
        printf("You entered %d.\n", number);
    }

    printf("The if statement is easy.");

    return 0;
}

Output 1

Enter an integer: -2
You entered -2.
The if statement is easy.

When the user enters -2, the test expression number<0 is evaluated to true. Hence, You entered -2 is displayed on the screen.

Output 2

Enter an integer: 5
The if statement is easy.

When the user enters 5, the test expression number<0 is evaluated to false and the statement inside the body of if is not executed

Types of if Statement

  1. If..else
  2. Else if ladder
  3. Nested if

1.C If…else Statement

In this statement, there are two types of statements execute. First, if the condition is true first statement will execute if the condition is false second condition will be executed.

Syntax:

If(condition)
{
Statement(s);
}
else
{
Statement(s)
}
Statement

Example : if…else statement

// Check whether an integer is odd or even

#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);

    // True if the remainder is 0
    if  (number%2 == 0) {
        printf("%d is an even integer.",number);
    }
    else {
        printf("%d is an odd integer.",number);
    }

    return 0;
}

Output

Enter an integer: 7
7 is an odd integer.

2.C if…else Ladder

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.

The if…else ladder allows you to check between multiple test expressions and execute different statements.

Example : if…else Ladder

// Program to relate two integers using =, > or < symbol

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    //checks if the two integers are equal.
    if(number1 == number2) {
        printf("Result: %d = %d",number1,number2);
    }

    //checks if number1 is greater than number2.
    else if (number1 > number2) {
        printf("Result: %d > %d", number1, number2);
    }

    //checks if both test expressions are false
    else {
        printf("Result: %d < %d",number1, number2);
    }

    return 0;
}

Output

Enter two integers: 12
23
Result: 12 < 23

3.C Nested if

If the condition is evaluated to true in the first if statement, then the condition in the second if statement is evaluated and so on.

Syntax:

If(condition)
{
If(condition)
{
Statement(s);
}
Else
{
Statement(s)
}
}

Example : Nested if…else

This program given below relates two integers using either <> and = similar to the if...else ladder’s example. However, we will use a nested if...else statement to solve this problem.

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    if (number1 >= number2) {
      if (number1 == number2) {
        printf("Result: %d = %d",number1,number2);
      }
      else {
        printf("Result: %d > %d", number1, number2);
      }
    }
    else {
        printf("Result: %d < %d",number1, number2);
    }

    return 0;
}

Output

Enter two integers: 12 12
Result: 12 = 23

Check out other control statements

  • Switch Statement
  • Conditional Operator Statement
  • Goto Statement
  • Loop Statements

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.