Conditional Statements in C

Conditional Statements in C programming are used to make decisions based on the conditions. These conditions are specified by a set of conditional statements having boolean expressions which are evaluated to a boolean value true or false. This process is called decision making in ‘C.’

Types of conditional statements in C

  1. If statement
  2. If-Else statement
  3. Nested If-else statement
  4. If-Else If ladder
  5. Switch statement

Conditional Statements in C

“if” statement

It is one of the powerful conditional statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Syntax –

if(expression)

{

//code to be executed

}

If the condition is evaluated as true, then the block statement is executed, but if the condition is evaluated as false, then control is passed to the next Statement following it.

NE_ifstatementExample

#include<stdio.h> 
int main() 
{ 
 int num1=1; 
 int num2=2; 
 if(num1<num2)    //test-condition 
 { 
   printf("num1 is smaller than num2");
 } 
 return 0; 
}

Output

num1 is smaller than num2

“if-else” Conditional Statements in C

if-else Statement in C allows two-way selection. If the given condition is true, then program control goes inside the if block and executes the Statement.

The condition is false, then program control goes inside the else block and executes the corresponding Statement.

Syntax

if (test-expression)
{
    True block of statements
}
else
{
    False block of statements
}
Statements;

 

if-else Conditional Statements in CLets take an example to understand this –

#include<stdio.h>
int main()
{
    int num=19;
    if(num<10)
    {
        printf("The value is less than 10");
    }
    else
    {
        printf("The value is greater than 10");
    }
    return 0;
}

Output

The value is greater than 10

Explanation :

  1. We have initialized a variable with value 19. We have to find out whether the number is bigger or smaller than 10 using a ‘C’ program. To do this, we have used the if-else construct.
  2. Here we have provided a condition num<10 because we have to compare our value with 10.
  3. As you can see the first block is always a true block which means, if the value of test-expression is true then the first block which is If, will be executed.
  4. The second block is an else block. This block contains the statements which will be executed if the value of the test-expression becomes false. In our program, the value of num is greater than ten hence the test-condition becomes false and else block is executed. Thus, our output will be from an else block which is “The value is greater than 10”. After the if-else, the program will terminate with a successful result.

In ‘C’ programming we can use multiple if-else constructs within each other which are referred to as nesting of if-else statements.

Nested if-else Conditional Statements in C

The nested if…else statement is used when a program requires more than one test expression. It is also called a multi-way selection statement. When a series of the decision are involved in a statement, we use if else statement in nested form.

Syntax

if( expression )
{ 
  if( expression1 )
  {
    statement-block1;
  }
  else 
  {
    statement-block 2;
  }
}
else
{
   statement-block 3;
}

The C if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions are true, then the final else statement will be executed.

nested if-else Conditional Statements in C

Lets take an example –

#include<stdio.h>
int main()
{
    int num=1;
    if(num<10)
    {
        if(num==1)
        {
            printf("The value is:%d\n",num);
        }
        else
        {
            printf("The value is greater than 1");
        }
    }
    else
    {
        printf("The value is greater than 10");
    }
    return 0;
}

Output

The value is:1

Explanation

  1. Firstly, we have declared a variable num with value as 1. Then we have used if-else construct.
  2. In the outer if-else, the condition provided checks if a number is less than 10. If the condition is true then and only then it will execute the inner loop. In this case, the condition is true hence the inner block is processed.
  3. In the inner block, we again have a condition that checks if our variable contains the value 1 or not. When a condition is true, then it will process the If block otherwise it will process an else block. In this case, the condition is true hence the If a block is executed and the value is printed on the output screen.
  4. The above program will print the value of a variable and exit with success.

Try changing the value of variable see how the program behaves.

NOTE: In nested if-else, we have to be careful with the indentation because multiple if-else constructs are involved in this process, so it becomes difficult to figure out individual constructs. Proper indentation makes it easy to read the program.

“if-else if-else” ladder

The if-else-if conditional Statement in C is used to execute one code from multiple conditions. It is also known as a multi-path decision statement. It is a sequence of if-else statements where every if  statement is associated with else if Statement and last would be an else statement.

NE_if-else-if ladder

The general syntax of how else-if ladders are constructed in ‘C’ programming is as follows:

if (test - expression 1)
{
   statement1;
}
else if (test - expression 2)
{
   Statement2;
}
else if (test - expression 3)
{
   Statement3;
}
else if (test - expression n)
{
   Statement n;
}
else
{
   default;
}
Statement x;

 

Lets take an example

#include<stdio.h>
int main()
{
    int marks=83;
    if(marks>75)
    {
        printf("First class");
    }
    else if(marks>65)
    {
        printf("Second class");
    }
    else if(marks>55)
    {
        printf("Third class");
    }
    else
    {
        printf("Fourth class");
    }
    return 0;
}

Output

First class

Explanation

  1. We have initialized a variable with marks. In the else-if ladder structure, we have provided various conditions.
  2. The value from the variable marks will be compared with the first condition since it is true the statement associated with it will be printed on the output screen.
  3. If the first test condition turns out false, then it is compared with the second condition.
  4. This process will go on until the all expression is evaluated otherwise control will go out of the else-if ladder, and default statement will be printed.

Try modifying the value and notice the change in the output.

“switch” Conditional Statements in C

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed.

Each case in a block of a switch has a different choice which is referred to as an identifier. When the user enters a value, then the value provided by the user is compared with all the cases inside the switch block until the exact match is found.

If a case match is not found, then the default statement is executed, and the control goes out of the switch block.

switch Conditional Statements in C -flowchartSyntax

Switch (expression)
{
 case value1:
     //Statements 
     break;
 case value 2:
     //Statements
     break; 
 case value 3:
     //Statements 
     break;
 case value n:
     //Statements
     break;
 Default:
     //Statements
}

break statement

You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.

Syntax

break;

Lets take an example with break statement –

#include <stdio.h>
    int main() {
        int num = 8;
        switch (num) {
            case 7:
                printf("Value is 7");
                break;
            case 8:
                printf("Value is 8");
                break;
            case 9:
                printf("Value is 9");
                break;
            default:
                printf("Out of range");
                break;
        }
        return 0;
    }

Output :

Value is 8

Explanation

  1. In the given program we have explain initialized a variable num with value 8.
  2. A switch construct is used to compare the value stored in variable num and execute the block of statements associated with the matched case.
  3. In this program, since the value stored in variable num is eight, a switch will execute the case whose case-label is 8. After executing the case, the control will fall out of the switch and program will be terminated with the successful result by printing the value on the output screen.

Now lets write the same code without the break statement.

#include <stdio.h>
    int main() {
        int num = 8;
        switch (num) {
            case 7:
                printf("Value is 7");
            case 8:
                printf("Value is 8");
            case 9:
                printf("Value is 9");
            default:
                printf("Out of range");
        }
        return 0;
    }

Can you guess what would be the output?

Output

Value is 8

Value is 9

Out of range

 

Explanation

If there is no break statement then the cases following the matched case will also get executed.

Note :

    • Case labels must have constants or constant expressions.must be of integral Type (Integer, Character).
    • Case label should not be ‘float type. ‘
    • Switch case should have at most one default label
    • The default label is Optional
    • The default can be placed anywhere in the switch
    • Break Statement takes control out of the switch
    • Two or more cases may share one break statement.

Conclusion

Try using different conditional statements in the same example for better understanding.

Keep Practicing.

Sharing is Caring!!

Leave a Reply

Your email address will not be published.