
C++ Conditionals are a very important part of programming used to execute a conditional statement based on true or false condition. There are many conditional statements which are supported by C++ programming language.
In this article, let us get familiar with the c++ conditional statements used in the program to evaluate statements based on conditions.
|
if (condition) {
// code to execute
}
|
|
#include <iostream> using namespace std; int main() { int number; cout<< “Enter a Number of your choice: “; cin>> number; if (number > 5) { cout << "Number is greater than 5" << endl; } return 0; } |
|
#include <iostream> using namespace std; int main() { int number; cout<< "Enter a number of your choice: "; cin>> number; if (number > 5) { cout << "Number is greater than 5"; } else{ cout<<"Number is less than 5" <<endl; } return 0; } |
|
if (condition1) { // code for condition1 } else if (condition2) { // code for condition2 } else { // code if none are true } |
|
#include <iostream> using namespace std; int main() { int number; cout<< "Enter a number of your choice: "; cin>> number; if (number > 5) { cout << "Number is greater than 5"; } else if (number=5){ cout<<"Number is equal to 5"; } else{ cout<<"Number is less than 5" <<endl; } return 0; } |
|
switch (variable) { case value1: // code for value1 break; case value2: // code for value2 break; default: // code if no match } |
|
#include <iostream> using namespace std; int main() { int number; cout<<"Enter a number of your choice (less than 10): "; cin>>number; // number = 2 switch (number){ case 1: cout<<"The value of Number is 1"; break; case 2: cout<<"The value of Number is 2"; break; default: cout<<"The number does not match any value"; } return 0; } |
|
variable = (condition) ? value_if_true : value_if_false; |
|
#include <iostream> using namespace std; int main() { int number = 10; // Using ternary operator to check if the number is positive or negative string result = (number > 0) ? "Positive" : "Negative"; cout << "The number is " << result << endl; return 0; } |
|
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; // Check if the number is even or odd if (num % 2 == 0) { cout << num << " is even." << endl; } else { cout << num << " is odd." << endl; } return 0; } |

|
#include <iostream> using namespace std; int main() { int num; // Input number from user cout << "Enter a number: "; cin >> num; // Check if the number is positive, negative, or zero if (num > 0) { cout << num << " is positive." << endl; } else if (num < 0) { cout << num << " is negative." << endl; } else { cout << "The number is zero." << endl; } return 0; } |
3. Write a C++ program to find the largest of three numbers entered by the user.
If-else statement in C++ is used to check which of the three numbers is the largest by using a series of nested if-else C++ conditionals.
|
#include <iostream>
using namespace std;
int main() {
int a, b, c;
// Input three numbers
cout << "Enter three numbers: ";
cin >> a >> b >> c;
// Find the largest number
if (a >= b && a >= c) {
cout << a << " is the largest number." << endl;
} else if (b >= a && b >= c) {
cout << b << " is the largest number." << endl;
} else {
cout << c << " is the largest number." << endl;
}
return 0;
}
|

|
#include <iostream> using namespace std; int main() { int marks; // Input marks from user cout << "Enter marks: "; cin >> marks; //marks =30 // Determine grade based on marks if (marks >= 90) { cout << "Grade A" << endl; } else if (marks >= 70) { cout << "Grade B" << endl; } else if (marks >= 50) { cout << "Grade C" << endl; } else { cout << "Grade F" << endl; } return 0; } |
|
#include <iostream> using namespace std; int main() { int year; // Input year from user cout << "Enter a year: "; cin >> year; // Check if the year is a leap year if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { cout << year << " is a leap year." << endl; } else { cout << year << " is not a leap year." << endl; } return 0; } |
