C++ if statement
- if statement is very powerful decision making statement.
- C++ uses if statement to execute a specific set of statements when the given logical condition is true. If the condition is not true then this part will not execute.
- Here is a syntax of if statement as follows:
- Statements present inside if block are executed only when the condition is true. If the condition is false then compiler will skip the statements within if block means control will not enter into if block. Here is a diagram that represents the concept of if statement more clearly:
- Here is an example of if statement:
cout<<"\n Enter the value of a and b:";
cout<<"\nvalue of a and b are equal";
The output of the above program is as follows:
Value of a and b are equal
In the above example we have declared two variables a and b. here we have used cin function to read the input from the console. When we give the input then first value is assigned to a, and 2nd value is assigned to b. after that we have used if statement. Inside this we have checked that if the both variable values are same then if block will be executed otherwise it will not execute. In this example both variable values are same. So if part will be executed.