What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?

If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

The operators && and || guarantee that the left-hand side expression will be fully evaluated (and all side effects applied) before the right-hand side is evaluated. In other words, the operators introduce a sequence point.

Additionally, if the value of the expression can be determined from the lhs, the rhs is not evaluated. In other words, if you have an expression like x && y, and x evaluates to 0 (false), then the value of the expression is false regardless of y, so y is not evaluated.

This means that expressions like x++ && x++ are well-defined, since && introduces a sequence point.

What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?
  Bipul Tiwari

 Print   7 min read  

21 Aug 2022

Intermediate

261K Views

Conditional statements help you to make a decision based on certain conditions. These conditions are specified by a set of conditional statements having boolean expressions which are evaluated to a boolean value of true or false.

In our life, we frequently encounter certain situations where we have to make a decision be it your favorite food, movie, hobby, or the color of our shoes. In C programming also, you may encounter similar kinds of situations where you need to make a decision based on the two possibilities that are yes/no or true/false acceptance, we will learn all about the conditional statements in this article.

There are the following 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

If statement

The single if statement in C language is used to execute the code if a condition is true. It is also called a one-way selection statement. When we use the if condition, we pass the argument and if the argument will be satisfied then the respective code will be executed otherwise nothing can happen.

Below is the if statement followed by the conditional expression.

Syntax

if(expression) { //code to be executed }

What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?

How the "if" statement works

  • If the expression is evaluated to nonzero (true) then if block statement(s) are executed.

  • If the expression is evaluated to zero (false) then Control passes to the next statement following it.

Note

"Expression must be scalar type" i.e evaluated to a single value.

if Statement Example

#include<stdio.h> #include<conio.h> void main() { int num=0; printf("enter the number"); scanf("%d",&num); if(n%2==0) { printf("%d number in even",num); } getch(); }

If-else statement

The if-else statement in C language is used to execute the code if the condition is true or false. It is also called a two-way selection statement.

The single if statement may work pretty well, but if you want to work with multiple variables or the extended conditional parameters, then the if-else statement is the optimum choice. By using the if statement, only one block of the code executes after the condition is true but by using the if-else statement, there are two possible blocks of code where the first block is used for handling the success part and the other one for the failure condition.

Syntax

if(expression) { //Statements } else { //Statements }

What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?

How the "if..else" statement works

  • If the expression is evaluated to nonzero (true) then if block statement(s) are executed.

  • If the expression is evaluated to zero (false) then else block statement(s) are executed.

if..else Statement Example

#include<stdio.h> #include<conio.h> void main() { int num=0; printf("enter the number"); scanf("%d",&num); if(n%2==0) { printf("%d number in even", num); } else { printf("%d number in odd",num); } getch(); }

Nested If-else statement

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 the if-else statement in nested form.

Nested if-else statements can be useful when we can have multiple sources of expression and the values and based on the specific value, we need to check nested conditions.

Its recommended for the best practice to avoid using nested if-else statement as it may turn into a conditional bubbling situation, better to use if-else-if or the switch case for the better conditional handling.

Syntax

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

Example

#include<stdio.h> #include<conio.h> void main( ) { int a,b,c; clrscr(); printf("Please Enter 3 number"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) { printf("a is greatest"); } else { printf("c is greatest"); } } else { if(b>c) { printf("b is greatest"); } else { printf("c is greatest"); } } getch(); }

If..else If ladder

The if-else-if statement is used to execute one code from multiple conditions. It is also called a multipath decision statement. It is a chain of if..else statements in which each if statement is associated with an else if statement and the last would be an else statement.

Syntax

if(condition1) { //statements } else if(condition2) { //statements } else if(condition3) { //statements } else { //statements }

What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?

If..else If ladder Example

#include<stdio.h> #include<conio.h> void main( ) { int a; printf("enter a number"); scanf("%d",&a); if( a%5==0 && a%8==0) { printf("divisible by both 5 and 8"); } else if( a%8==0 ) { printf("divisible by 8"); } else if(a%5==0) { printf("divisible by 5"); } else { printf("divisible by none"); } getch(); }

Switch Statement

switch statement acts as a substitute for a long if-else-if ladder that is used to test a list of cases. A switch statement contains one or more case labels that are tested against the switch expression. When the expression match to a case then the associated statements with that case would be executed.

We have seen the way of using conditional statements such as if, if-else. if-else ladder, but the need for an additional way of dealing with conditional statements may seem unnecessary but based on the certain usage, switch case was defined to check for the single condition, and based on the multiple cases, code can be executed.

Below is the basic syntax that shows how to use and implement a switch statement.

Syntax

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

What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?

switch statement Example

#include<stdio.h> #include<conio.h> void main( ) { char grade = 'B'; if (grade == 'A') { printf("Excellent!"); } else if (grade == 'B') { printf("Well done"); } else if (grade == 'D') { printf("You passed"); } else if (grade == 'F') { printf("Better try again"); } else { printf("You Failed!"); } } getch(); } Read More Articles Related to C Programming Language

Note

  • The switch statement must be an integral type.

  • Case labels must be constants.

  • Case labels must be unique.

  • Case labels must end with a colon.

  • The break statement transfers the control out of the switch statement.

  • The break statement is optional.

Take our free skill tests to evaluate your skill!

What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?
  • =IF(D2>89,"A",IF(D2>79,"B",IF(D2>69,"C",IF(D2>59,"D","F"))))

    This complex nested IF statement follows a straightforward logic:

  1. If the Test Score (in cell D2) is greater than 89, then the student gets an A

  2. If the Test Score is greater than 79, then the student gets a B

  3. If the Test Score is greater than 69, then the student gets a C

  4. If the Test Score is greater than 59, then the student gets a D

  5. Otherwise the student gets an F

This particular example is relatively safe because it’s not likely that the correlation between test scores and letter grades will change, so it won’t require much maintenance. But here’s a thought – what if you need to segment the grades between A+, A and A- (and so on)? Now your four condition IF statement needs to be rewritten to have 12 conditions! Here’s what your formula would look like now:

  • =IF(B2>97,"A+",IF(B2>93,"A",IF(B2>89,"A-",IF(B2>87,"B+",IF(B2>83,"B",IF(B2>79,"B-", IF(B2>77,"C+",IF(B2>73,"C",IF(B2>69,"C-",IF(B2>57,"D+",IF(B2>53,"D",IF(B2>49,"D-","F"))))))))))))

It’s still functionally accurate and will work as expected, but it takes a long time to write and longer to test to make sure it does what you want. Another glaring issue is that you’ve had to enter the scores and equivalent letter grades by hand. What are the odds that you’ll accidentally have a typo? Now imagine trying to do this 64 times with more complex conditions! Sure, it’s possible, but do you really want to subject yourself to this kind of effort and probable errors that will be really hard to spot?

Tip: Every function in Excel requires an opening and closing parenthesis (). Excel will try to help you figure out what goes where by coloring different parts of your formula when you’re editing it. For instance, if you were to edit the above formula, as you move the cursor past each of the ending parentheses “)”, its corresponding opening parenthesis will turn the same color. This can be especially useful in complex nested formulas when you’re trying to figure out if you have enough matching parentheses.

Following is a very common example of calculating Sales Commission based on levels of Revenue achievement.

What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?
  • =IF(C9>15000,20%,IF(C9>12500,17.5%,IF(C9>10000,15%,IF(C9>7500,12.5%,IF(C9>5000,10%,0)))))

This formula says IF(C9 is Greater Than 15,000 then return 20%, IF(C9 is Greater Than 12,500 then return 17.5%, and so on...

While it’s remarkably similar to the earlier Grades example, this formula is a great example of how difficult it can be to maintain large IF statements – what would you need to do if your organization decided to add new compensation levels and possibly even change the existing dollar or percentage values? You’d have a lot of work on your hands!

Tip: You can insert line breaks in the formula bar to make long formulas easier to read. Just press ALT+ENTER before the text you want to wrap to a new line.

Here is an example of the commission scenario with the logic out of order:

What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?

Can you see what’s wrong? Compare the order of the Revenue comparisons to the previous example. Which way is this one going? That’s right, it’s going from bottom up ($5,000 to $15,000), not the other way around. But why should that be such a big deal? It’s a big deal because the formula can’t pass the first evaluation for any value over $5,000. Let’s say you’ve got $12,500 in revenue – the IF statement will return 10% because it is greater than $5,000, and it will stop there. This can be incredibly problematic because in a lot of situations these types of errors go unnoticed until they’ve had a negative impact. So knowing that there are some serious pitfalls with complex nested IF statements, what can you do? In most cases, you can use the VLOOKUP function instead of building a complex formula with the IF function. Using VLOOKUP, you first need to create a reference table:

What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?
  • =VLOOKUP(C2,C5:D17,2,TRUE)

This formula says to look for the value in C2 in the range C5:C17. If the value is found, then return the corresponding value from the same row in column D.

What do you call the type of if-else statement where there are multiple conditions to be evaluated in order?
  • =VLOOKUP(B9,B2:C6,2,TRUE)

Similarly, this formula looks for the value in cell B9 in the range B2:B22. If the value is found, then return the corresponding value from the same row in column C.

Note: Both of these VLOOKUPs use the TRUE argument at the end of the formulas, meaning we want them to look for an approxiate match. In other words, it will match the exact values in the lookup table, as well as any values that fall between them. In this case the lookup tables need to be sorted in Ascending order, from smallest to largest.

VLOOKUP is covered in much more detail here, but this is sure a lot simpler than a 12-level, complex nested IF statement! There are other less obvious benefits as well:

  • VLOOKUP reference tables are right out in the open and easy to see.

  • Table values can be easily updated and you never have to touch the formula if your conditions change.

  • If you don’t want people to see or interfere with your reference table, just put it on another worksheet.

There is now an IFS function that can replace multiple, nested IF statements with a single function. So instead of our initial grades example, which has 4 nested IF functions:

  • =IF(D2>89,"A",IF(D2>79,"B",IF(D2>69,"C",IF(D2>59,"D","F"))))

It can be made much simpler with a single IFS function:

  • =IFS(D2>89,"A",D2>79,"B",D2>69,"C",D2>59,"D",TRUE,"F")

The IFS function is great because you don’t need to worry about all of those IF statements and parentheses.

You can always ask an expert in the Excel Tech Community or get support in the Answers community.

Video: Advanced IF functions
IFS function (Microsoft 365, Excel 2016 and later)
The COUNTIF function will count values based on a single criteria
The COUNTIFS function will count values based on multiple criteria
The SUMIF function will sum values based on a single criteria
The SUMIFS function will sum values based on multiple criteria
AND function
OR function
VLOOKUP function
Overview of formulas in Excel
How to avoid broken formulas
Detect errors in formulas
Logical functions
Excel functions (alphabetical)
Excel functions (by category)