If else in C language


What is a condition?

In this article, we cover if else in C language, but before that, let us explain a condition. If life were a straight line, it would be very boring. But this is not the case, and every day we face different events that destroy the monotony of life. Because we have to decide to do a specific task in different situations. The same applies to programming. In programs, we often have to decide to perform a certain action based on the occurrence of a certain condition.
In fact, the important task of a programmer is to direct the computer (smartphone, laptop, microcontroller, etc.) to certain parts of the program when faced with special conditions. 
If we want some commands to be executed under certain conditions or some others not to be executed, we must use conditional structures. Conditional statements are the main components in all programming languages. A conditional command checks the condition specified by the user and executes one set of codes if it is true and another set of codes if it is false. In the C language, there are two important conditional commands or structures called if and switch, which we will learn.

If else in C language

In this section, we examine the if command in C language. In the C, the if statement is the most popular conditional statement. With this conditional command, you can evaluate variables, arrays, strings, etc. In the picture below, you can see the flowchart of the if-conditional structure:

flowchart of if else in c language

 

The format of the conditional if statement is as follows:

if(condition) {
    //statement
}
else{
    //statement
}

If the condition is met, the code inside { } will be executed. If the desired condition is not met, the codes inside { } will be executed under else. Consider the following example:

#include <stdio.h>

int a = 10;

int main(){
  if(a == 7) {
      printf("a is equal to 7.");
  else
      printf("a is not equal to 7.");

  return 0;
}
  1. The if checks if variable “a” is equal to 7.
  2. Because the value of a is not 7, the second “printf()” is executed.
  3. Printf() is a function to print text onto the screen. We learn about it in functions.
  4. There are no { } signs in the above lines. Because there is only one statement under the if and else statements.
  5. If the number of commands is more than one command, { } must be used.
  6. The existence of the else command in the if is not necessary and is placed depending on the user’s needs.

Else if statement in C

In the example above, only one condition was checked. If more conditions need to be evaluated after the first if statement, then else if should be used for subsequent conditions. In the figure below, you can see the else if flowchart:

flowchart of else if in c

In this flowchart, the conditions are checked in order, and for the first true condition, the related statements are executed, and other statements are ignored. But if none of the conditions are correct, the statements related to the last else will be executed. Note that the last else is optional and can be omitted. Take a look at the example below:

#include <stdio.h>

int a = 10;

int main(){
  if(a == 7) {
      printf("a is equal to 7.");
  else if(a == 10)
      printf("a is equal to 10.");

  return 0;
}

In the example above, two conditions are examined. The first one checks whether a is equal to 7, and the second one checks if a is equal to 10. The second one is true.
In the following example, we use more conditions:

#include <stdio.h>

int a = 10;

int main(){
  if(a == 7) {
      printf("a is equal to 7.");
  else if(a == 10)
      printf("a is equal to 10.");
  else{
      printf("a is neither 7 nor 10.\n");
      printf("a is %d.", a);
  }

  return 0;
}
  1. The last else’s statements are executed.
  2. Since there are two statements, a pair of curly brackets are used in the last else.
  3. In the second “printf(),” the value of a is printed in place of %d.
Was this helpful?
[0]