In this C++ Tutorial we are going to talk about If Else Statement in C++ . using conditional statement we can execute some section of the code according to a condition. and particularly in this article we make some examples of if else condition. using if statement you can control if a program enters a section of code or not based on whether a given condition is true or false. also using if statement you can take actions according to the user input, for example according to the user input we can check two numbers that which one is big and smaller, we will see this example in our article. OK Sometimes when the condition in an if statement became false, it is good to execute some code instead of the code executed when the statement became to true, using else we can evaluate if the condition became false what we should do. also we can use else if when there are multiple conditional statements that may all evaluate to true, yet you want only one if statement’s body to execute.
Also you can check more article on c++ programming language
1: C++ Introduction & Program Structure
2: C++ Variables And Data Types
Syntax for if else
1 2 3 4 5 6 |
if ( True ) { // Execute this code } else { // Execute this code } |
So now let’s create some real examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include<iostream> using namespace std; int main() { bool condition = true; if (condition) { //the code in the body will be execute cout << "Condition is true" << endl; } else { //in the false case this code will be executed cout << "condition is false" << endl; } return 0; } |
OK in the above code first we have a created a Boolean variable, and by default the value is true, after that we are checking that if the condition is true we are executing the code in block and if the condition became false we are executing another code in the block. if you run the code this will be the result. because the condition is true.
1 2 |
Condition is true Press any key to continue . . . |
If the condition became false you will receive this output.
1 2 |
condition is false Press any key to continue . . . |
This is another example, in this example we are checking two numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include<iostream> using namespace std; int main() { int number = 12; if (number < 12) { cout << "Number is less than 12" << endl; } else if (number == 12) { cout << " Number is equal to 12 " << endl; } else { cout << "Number is greater than 12" << endl; } return 0; } |
Run the code and this will be the output.
1 2 |
Number is equal to 12 Press any key to continue . . . |
Let’s create little complex example, and this example will be according to the user input, basically we are going to get two numbers from the user and after that we are checking these numbers and give the output for the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include<iostream> using namespace std; int main() { int a; int b; cout << "Please enter first number : " << endl; cin >> a; cout << "Please enter second number : " << endl; cin >> b; if (a > b) { cout << "a value is greater than b" << endl; } else if (a == b) { cout << "a and b values are equal " << endl; } else { cout << "a value is smaller than b" << endl; } return 0; } |
So if you run the code this will be the result.
1 2 3 4 5 6 |
Please enter first number : 10 Please enter second number : 12 a value is smaller than b Press any key to continue . . . |
Also you can watch the complete video for this article.
Subscribe and Get Free Video Courses & Articles in your Email