Conditional Statements

Till now we have dealt with sequential execution. This happens if there is no conditions upon the statements. Conditional statements helps us to execute statements based on conditions. This make program more flexible. It adds the extra functionality by helping to make decisions on certain conditions. These are also known as ‘decision making statements’.

Conditional statements are possible with the help of following constructs

  • if statement
  • if-else statement
  • switch statement

The if Statement

The if statement allows conditional execution. It is responsible for modifying the flow of the program. The if statement is executed, if the condition is true. The syntax for if statement is:


The statement gets executed if the condition is true. We can have multiple statements in between { and }. The following program shows the use of if statement

using namespace std;
int main(){
	int num = 10;
		cout<<"Number is less than 50";
	return 0;
Output of the program
Output of above program

In the above program, we have initialized the variable num to 10. In the second statement, we execute the condition if the number is less than 50. And as the number is less than 50, the statement in the brackets gets executed.

The condition evaluates to either true or false. If the value is non-zero, then the value is true and if the value is 0, then it evaluates to false.

The if-else Statement

The if-else statement causes the other statement to execute if the condition is not true in the if block. The syntax for if-else statement is given as

    statement 1;
    statement 2;

If the condition is true then statement 1 gets executed, else statement 2 gets executed. The following can be seen in the following flow chart. In the program, we read two numbers a and b. If a is greater than b, then we print a else we print b.

flow chart for if-else statement
Flow chart for if-else statement

We can write the above flow chart in form of program as

using namespace std;
int main(){
	int a,b;
	cout<<"Enter two numbers : "<<endl;
	cout<<"a : ";
	cout<<"b : ";
		cout<<a<<" is greater than "<<b;
		cout<<b<<" is greater than "<<a;
	return 0;
Output of the above program.
Output of the above program

Relational Operators

Relational operators are used to formulate the conditions, which returns true or false. We have 6 relational operators as:

  1. x < y x is less than y
  2. x > y x is greater than y
  3. x <= y x is less than or equal to y
  4. x >= y x is greater than or equal to y
  5. x == y x is equal to y
  6. x != y x is not equal to y

These operators can be used to compare values x and y of ordinal data type. And the result is either true or false, according to the condition.

Leave a Reply