C++ Tutorial – If Else Statement in C++

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 

2: C++ Getting User Input 

 

Syntax for if else

 

 

 

 

So now let’s create some real examples.

 

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.

 

 

 

If the condition became false you will receive this output.

 

 

 

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

 

 

 

 

Run the code and this will be the output.

 

 

 

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.

 

 

 

 

So if you run the code this will be the result.

 

 

 

 

 

Also you can watch the complete video for this article.

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×