In this post, we will learn about C++ if...else conditional statement which is used for decision-making purposes in programming. In a decision-making process, we have to choose one out of many available options and our decision is based upon certain conditions.
In programming, let's say you have two blocks of code and you want to execute the first block of code only if a certain condition is true else you will execute the second block of code. In these kinds of cases, you require a condition statement like if...else.
There are three kinds of if...else statement available in C++.
- if statement.
- if...else statement.
- if...else if...else statement.
if statement in C++.
if(condition){ //statement(s) to execute if condition is true }
//C++ if statement Example #include<iostream> using namespace std; int main(){ int num; cout<<"Enter number:"; cin>>num; if(num % 2 == 0){ cout<<"The Entered number is an even number."; } cout<<"\nThe value of num: "<<num; return 0; }
Enter number:24
The entered number is an even number.
The value of num: 24
Enter number:21
The value of num: 21
if...else statement in C++.
if(condition){ //statement(s) to execute if condition is true } else{ //statement(s) to execute if condition is false }
![]() |
Flowchart of if...else statement |
//C++ if..else statement Example #include<iostream> using namespace std; int main(){ int num; cout<<"Enter number:"; cin>>num; if(num % 2 == 0){ cout<<"The Entered number is even number."; } else{ cout<<"The Entered number is an odd number."; } cout<<"\nThe value of num: "<<num; return 0; }
Enter number:33
The entered number is an odd number.
The value of num: 33
Enter number:36
The entered number is even number.
The value of num: 36
if...else if...else statement in C++.
if(condition 1){ //statement(s) to execute if condition 1 is true } else if(condition 2){ //statement(s) to execute if condition 2 is true } else if(condition 3){ //statement(s) to execute if condition 3 is true } else{ //statement(s) to execute if all conditions false }
![]() |
Flowchart of if...else if...else statement |
//C++ if..else if else statement Example #include<iostream> using namespace std; int main(){ int mark; cout<<"Enter the mark of the student: "; cin>>mark; if(mark >= 90){ cout<<"Student got Grade A"; } else if(mark >= 70 && mark < 90){ cout<<"Student got Grade B"; } else if(mark > 50 && mark < 70){ cout<<"Student got Grade C"; } else{ cout<<"Student got Grade D"; } cout<<"\nTotal mark of the student: "<<mark; return 0; }
Enter the mark of the student: 70
Student got Grade B
Total mark of the student: 70
Enter the mark of the student: 40
Student got Grade D
Total mark of the student: 40
Note: There can be more than one else if statement in our code but only one if and else statement is allowed. (alert-success)
Nested if...else statement.
if(condition 1){ //statement(s) to execute if condition 1 is true if(condition 1A){ //statement(s) to execute if condition 1A is true } else{ //statement(s) to execute if condition 1A is false } } else if(condition 2){ //statement(s) to execute if condition 2 is true } else{ //statement(s) to execute if all conditions false }
//C++ Nested if..else if else statement Example #include<iostream> using namespace std; int main(){ int num; cout<<"Enter a number: "; cin>>num; if(num % 2 == 0){ if(num % 5 == 0){ cout<<"Number is Even and Divisible by 5"; } else{ cout<<"Number is Even and not Divisible by 5"; } } else{ if(num % 5 == 0){ cout<<"Number is Odd and Divisible by 5"; } else{ cout<<"Number is Odd and not Divisible by 5"; } } return 0; }
Enter a number: 25
Number is Odd and Divisible by 5
No comments:
Post a Comment