How if else-if else ladder statement works in C program.

 How if else-if else ladder statement works in C program.

In this blog post we will cover the concept of conditional statements which also referred to as control structure statements. We have already discussed about the if statement in C programming language. But in this blog post we will cover the concept of if else if ladder statement in C program. Before diving deep into this concept let us look at the basic syntax and basic working of if else if ladder statement.

By now we know that decision making statements are very essential part part of any programming language so it is better to understand this concept first. The standard if condition simply checks the condition and if that condition is true it will simply execute the piece of code written with in its curly brackets.

And if we talk about if else-if ladder statement in C programming language, when the if block condition becomes false it will terminate the if part and directly jumps to else part and execute the code written between else curly brackets. 



Before starting if else-if ladder statement in C programming language let us look at the basic syntax of if else-if ladder statement.


Basic syntax of if else-if ladder statement in C programming language


if
(condition1) { // write coding part here. } else if (condition2) { // write coding part here. } else if (condition3) { // write coding part here. } else if (condition4) { // write coding part here. } else { // write coding part here. }


Just keep one thing in mind that if else-if ladder statement is extension of if else statement in C programming language. The main difference in both of these statements is that in simple if condition there is only one condition statement where as in if else-if ladder statement there are multiple conditions among which we can chose a specfic condition that matches with our program logic.

Now let us look at the syntax of if else-if ladder statement in C programming language.

In the above syntax code which is starting with if condition and its specific code of block, now if the starting if condition is true that code block will run. But in case if the first if condition is false then our program will ignore this if condition and move to the else-if condition part. And similarly if the else-if condition is true it will execute the piece of code written inside the else if condition block of code. Suppose if the else if conditon is false it will move or check the next else if condition to run.

Thus, it means that we can write as many else if conditions as per our program logic or requirement, and if none of the else if condition is true then automatically our program will move to the else part and execute the code of else block.

So this is the basic syntax and working of if else if ladder statement in C programming language.


Simple coding example of if else if ladder statement in C programming language


#include<stdio.h>
void main()
{
	int number = 0;
	printf("Enter Number = ");
	scanf_s("%d", &number);
	if (number == 10)
	{
		printf("Entered Number Is 10.");
	}
	else if (number == 20)
	{
		printf("Entered Number Is 20.");
	}
	else if (number == 30)
	{
		printf("Entered Number Is 30.");
	}
	else if (number == 40)
	{
		printf("Entered Number Is 40.");
	}
	else
	{
		printf("Entered Number Is Not 10, 20, 30, or 40.");
	}
	_getch();
}


Now if we look at the above coding example we can see that we have declared and initialized a variable of type int which is named as number. Now after that we have simply printed the message on the console screen to enter the number like this,

        int number = 0;
printf("Enter Number = ");

And in this C program coding example we have used the if else if ladder statement with one if statement and three else if statements and at last we have used the else part.

Now in this C programming coding example if the if condition is true it will simply print the message that entered number is equal to 10 like this,

        if (number == 10)
	{
		printf("Entered Number Is 10.");
	}

If the first if condition becomes false it will check for the else if condition part and check whether the entered number is  equal to 20 or not and simply if the entered number is 20 then the first else if condition will run and the rest of the else if conditions will be ignored even without checking them.

Now there is an important point to mention that if the first else if condition is true then our program will not check the remaining else if conditions and the same is true for other else if statements which means that, suppose if first else if condition is false then it will check the second or next else if condition and if that else if condition becomes true our program will simply ignore the rest of the else if conditions.

And at last if all the conditions become false i.e, if, else if then the else part will run. So this is how the if else if ladder statement works in C programming language.

Congratulations! like always we have successfully implemented the if else if ladder statement in C programming language.

Comments

Popular posts from this blog

How to Download and Install Microsoft visual studio community 2022 for free

Hello World Program In C Programming Language

How to add two numbers in C programming language