C plus plus programming
June 3, 2026 3 min read

Guide to Conditional Statements and Switch Statements in C++

HS
HEMANT SINGH
Technical Writer & Educator

When programs contain conditions statements are used.

if statement

If the statement is used when the program has one condition and related output.

#include<iostream.h>
#include<conio.h>
using namespace std;
int main(){
	int n;
	cout<<"Enter a value:";
	cin>>n;
	if(n%2==0){
		cout<<"Even.";
	}
	getch();
	return 0;
}

if-else statement

This statement is used when the program has one condition and related two outputs.

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
	int n;
	cout<<"Enter a value:";
	cin>>n;
	if(n%2==0){
		cout<<"Even.";
	}
	else{
		cout<<"Odd.";
	}
	getch();
	return 0;
}

if-else if statement

This statement is used when a program has multiple conditions and related output.

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
	int n;
	cout<<"Enter 10th class percentage:";
	cin>>n;
	if(n>=60){
		cout<<"First division.";
	}
	else if(n>=45){
		cout<<"Second division.";
	}
	else if(n>=33){
		cout<<"Third division.";
	}
	else{
		cout<<"Fail.";
	}
	getch();
	return 0;
}

Nested if statement

This statement is used when a program has a condition within a condition.

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
	int a,b,c;
	cout<<"Enter three different numbers:";
	cin>>a>>b>>c;
	if(a>b){
		if(a>c){
			cout<<"A is greater.";
		}
		else{
			cout<<"C is greater.";
		}
	}
	else if(b>a){
		if(b>c){
			cout<<"B is greater.";
		}
		else{
			cout<<"C is greater.";
		}
	}
	getch();
	return 0;
}

Conditional statements

It is an alternative to the if-else statement, or it is also called the single-line statement

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
	int n;
	cout<<"Enter a value:";
	cin>>n;
	n%2 == 0 ? cout<<"Even." : cout<<"Odd.";
	getch();
	return 0;
}

 

Switch statements

It is an alternative of an if-else if statement to check multiple conditions in a single program.

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
	int day;
	cout<<"Enter a number from 1-7:";
	cin>>day;
	switch(day){
		case 1:
		cout<<"Sunday.";
		break;

		case 2:
		cout<<"Monday.";
		break;

		case 3:
		cout<<"Tuesday.";
		break;

		case 4:
		cout<<"Wednesday.";
		break;

		case 5:
		cout<<"Thursday.";
		break;

		case 6:
		cout<<"Friday.";
		break;

		case 7:
		cout<<"Saturday.";
		break;

		default:
		cout<<"Invalid value.";
		break;
	}	
	getch();
	return 0;
}

In this category