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
3: C++ Getting User Input

Syntax for if else

if ( True ) {
  // Execute this code 
}
else {
  // Execute this code
}

So now let’s create some real examples.

#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.

Condition is true
Press any key to continue . . .

If the condition became false you will receive this output.

condition is false
Press any key to continue . . .

This is another example, in this example we are checking two numbers

#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.

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.

#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.

Please enter first number :
10
Please enter second number :
12
a value is smaller than b
Press any key to continue . . .

#c++ #cplusplus #web-development

If Else Statement In C++
1.25 GEEK