Multiple conditions for if statement in C / C++
When working with if
statement in C / C++, we often wonder that "Can if
statement have 2 conditions?" or "Can you have 3 conditions in an if
statement?". So in this tutorial, we will learn if an if
statement can have multiple conditions.
For example: Check if a number N is between 1 and 100?
Explain: Here, we have two conditions, N > 1
and N < 100
. So in the if
statement, we need to use these two conditions.
The best way: Use the "AND" operator to combine two conditions.
if(N > 1 && N < 100){ cout<<N<<" > 1 and "<<N<<" < 100."; }
In some cases, we need to use 2 - 3 or more conditions in the if
statement. So what are the ways to do that? Let's get started!!
Multiple conditions for if statement in C / C++
In the if
statement, we can use many different conditions at the same time. However, we need to use additional operators like: And (&&), Or (||). Now, let's find out each way with us.
Use the "AND" operator for multiple conditions
Let's say we have 2 conditions in an if
statement and all of these conditions need to be checked. Then using the "AND" operator is the best solution. Let's see the example below:
Example: assigning grades (A, B, C) based on marks obtained by a student.
- If the percentage is above 90, assign grade A.
- If the percentage is above 75 and below 90, assign grade B.
- If the percentage is above 65 and below 75, assign grade C.
- if the percentage is below 65, assign grade D.
C++ programming:
#include <iostream> using namespace std; int main() { int score; do{ cout<<"Enter your percentage: "; cin >> score; if(score < 1 || score > 100) cout<<"\nPlease enter numbers from 1 to 100!!\n\n"; }while(score < 1 || score > 100); if(score >= 90) cout<<"Your score is A"; else if(score < 90 && score >= 75) cout<<"Your score is B"; else if(score < 75 && score >= 65) cout<<"Your score is C"; else cout<<"Your score is D"; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int score; do{ printf("Enter your percentage: "); scanf("%d", &score); if(score < 1 || score > 100) printf("\nPlease enter numbers from 1 to 100!!\n\n"); }while(score < 1 || score > 100); if(score >= 90) printf("Your score is A"); else if(score < 90 && score >= 75) printf("Your score is B"); else if(score < 75 && score >= 65) printf("Your score is C"); else printf("Your score is D"); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Use the "OR" operator for multiple conditions
In this case, we have two conditions. However, as long as one of them occurs, the block of code inside the if
statement body must execute. Then we will use the "OR" operator.
Example: Ask the user to enter the natural number N. If N < 1
or N > 100
, the message "You entered wrong!!!"
C++ programming:
#include <iostream> using namespace std; int main() { int score; cout<<"Enter your percentage: "; cin >> score; if(score < 1 || score > 100) cout<<"\nYou entered wrong!!!\n\n"; else cout<<"\nYou entered right!!!\n\n"; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int score; printf("Enter your percentage: "); scanf("%d", &score); if(score < 1 || score > 100) printf("\nYou entered wrong!!!\n\n"); else printf("\nYou entered right!!!\n\n"); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Use the nested if statement for multiple conditions
In addition to using the "And" operator, we can use nested if
statements with multiple conditions.
Example: Check if a number is between 1 and 100?
C++ programming:
#include <iostream> using namespace std; int main() { int N; cout<<"Enter number N: "; cin >> N; if(N > 1){ if(N < 100){ cout<<N<<" > 1 and "<<N<<" < 100."; } else cout<<"\nYou entered wrong!!!\n\n"; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int N; printf("Enter number N: "); scanf("%d", &N); if(N > 1){ if(N < 100){ printf("%d > 1 and %d < 100.",N,N); } else printf("\nYou entered wrong!!!\n\n"); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output: