conditional statements in c

Conditional statements in programming help us run certain code only if certain conditions are met. Almost all programming languages, including C, use them. In C, there are different types of these statements, each with its own unique syntax, chosen based on use case. Here are the conditional statements in C that are available.

“if” statement

The “if” statement in programming enables actions to occur only when a specific condition is true.

syntax:
if (condition)
{
      // statement to execute
}
flow diagram:
if statement in C

Here is a sample program which explains the use of if statement.

/* Write a program which takes a number from stdin,
    that prints "The given number is divisible by 15" if
    the given number is divisible by 15 */

#include <stdio.h>
int main()
{
      int a;
      // take input from user
      printf("Enter a number\n");
      scanf("%d", &a);

      if (a % 10 == 15)
            printf("The given number is divisible by 15\n");
}

Note: When executing a single-line statement, curly braces { } are not neccessary.

“if-else” statement

The “if-else” statement in programming helps you decide between two actions: one if a condition is true and another if it’s false. It’s like having a plan B in case the first plan doesn’t work out.

syntax:
if (condition)
{
      // statement 1
} else {
      // statement 2
}
flow diagram:
if else  conditional statements in C

here is a sample program which explains the use of “if else” statement:

/* write a program which takes a number from stdin,
    prints "the given number is even" if the given number is even"
    and prints "the given number is odd" if the given number is odd. */

#include <stdio.h>
int main()
{
      int a;
      // take input from user
      printf("Enter a value:\n");
      scanf("%d", &a);

      if (a % 2 == 0)
            printf("The given number is even\n");
      else
            printf("The given number is odd\n");
}

nested if

nested if is like making choices. You have a list of options. You check the first option – if it’s true, you pick that. If not, you move to the next option. If any option is true, you choose it. and If none are true, you have a backup plan, like a default choice.

syntax:
if (condition 1)
{
      // statement 1
} else if (condition 2)
{
      // statement 2
} else if (condition 3)
{
      // statement 3
} else {
      // default statement
}
flow diagram for nested if:
nested if in c

as we discussed about if, if else and nested if. these conditions are fine upto some extent, but what if there are more conditions to check and you need to execute only one out of them, if we use nested if for lets say 10 conditions, and 10th condition is true in this case CPU executes all other 9 and reaches 10th statement. The CPU experiences increased execution time, consequently reducing performance, due to the program. Switch statements serve to mitigate this issue.

“switch” statement

The “switch” statement in programming is a helpful tool when you have multiple conditions to check and you need to execute only one specific block of code based on the condition that matches. It’s like having multiple options, and you pick just one based on the given condition.

syntax for switch case
#include <stdio.h>
int main()
{
      int option = 3;

            switch (option) {
                   case 1:
                   // do something for option 1
                   break;
                   case 2:
                   // do something for option 2
                   break;
                   case 3:
                   // do something for option 3
                   break;
                   default:
                 // do something if none of the options match
           }
}
flow diagram for switch case
switch case in C

Note: Switch case statements exclusively support value checking and do not permit conditions like (a > 10), (i == 14), (b != 10), etc.

Sample Programs for conditional statements in C are available here.

Prev << Typecasting
Next >> Loops in C