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 or Conditional Execution 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

The “if” statement 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=15; 
 int num2=29; 
 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=57;
    if(num<20)
    {
        printf("The value is less than 20");
    }
    else
    {
        printf("The value is greater than 20");
    }
    return 0;
}

Output

The value is greater than 20

Explanation :

int num=57;

We created a value named “num” and assigned the value 57 to it. We want to check “if” the number is bigger or smaller than 20 using a ‘C’. To do this, we are going to use the if-else statements.

if(num<20)

Here we have provided a condition num<20 because we have to compare our value with 20. We are checking if num is less than 20. If the condition is true, execution will enter the block. Since the condition is not true, it jumps to the else part.

else  {      printf("The value is greater than 20");  }

Since our test condition num<20 was false, the execution jumps to else part.

In ‘C’ programming we can use multiple if-else statements 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

You can experiment with different values to see how the execution changes.

“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 temperature = 35;
    if(marks>40)
    {
        printf("Extremely hot");
    }
    else if(marks>30)
    {
        printf("Mildly hot");
    }
    else if(marks>20)
    {
        printf("Comfortable");
    }
    else
    {
        printf("Cold");
    }
    return 0;
}

Output

Mildly hot

Just like with the previous example, you can experiment with different values here too.

“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.

1 thought on “Conditional Statements in C”

Leave a Reply

Your email address will not be published. Required fields are marked *