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.
What are C++ Conditionals?
The C++ conditionals refers to a block of statements executed on specific conditions. They are very useful in making decisions based on certain conditions which generally evaluates to two options in programming.
C++ programming language consists of if statement, if..else statement, nested if statements, switch statements, nested switch statements, and much more. It is important to be well aware of the C++ conditionals as they are frequently used in programming.
C++ Conditionals: Key Takeaways
- C++ Conditionals consist of if statement, if-else statements, else if statements, nested statements and more.
- In C++ conditionals, the conditionals either evaluate to true or false based on the values.
- The C++ conditionals must be enclosed within a parentheses () and evaluates to a true or false value.
- We use “==” for comparison. Do not use a “=” assignment operator for comparison.
- The break statement must be used to control the exit after a match condition and avoid execution of the default statement.
C++ Conditionals: If Statement
The if statement in C++ is used to execute a block of code based on a specific condition when evaluated to true. If the condition evaluates to false then the statement inside the if statement is not executed and the execution moves to the next statement.
if (condition) { // code to execute } |
If-Else Statement in C++ Example Program
Let us understand the if C++ conditionals based on a simple example given below.
#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; } |
Suppose we put the value of “number” 10 then the if statement will execute and return “Number is greater than 5”. A simple representation of if-statement used in C++ programming.
If-Else C++ Conditionals
The If else C++ conditionals are very similar to if statements in Conditional. However, the only difference lies here is when the statement evaluates to false then we use another statement to handle the false statement.
Let us extend the above example to understand the if-else c++ conditionals in a better way.
If-Else Statement in C++ Example Program
#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; } |
When we input the value of number as 4 then the output is executed via the else statement in C++ which gives “Number is less than 5”.
Else If C++ Conditionals
The Else if conditional statements in C++ consists of many else if conditions in between which evaluates for multiple conditions. The common syntax for else if c++ conditionals are mentioned below.
if (condition1) { // code for condition1 } else if (condition2) { // code for condition2 } else { // code if none are true } |
Let us again extend the above example code to understand the else if statement ladder below. In the following program we will also keep an else if statement before the else statement for numbers equal to 5. Let us check.
The else if condition also contains a condition which must be satisfied to execute the program or statement in the block.
#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; } |
Suppose we put the input value to 5 then let us see the result, the else if statement block will be executed in the following condition.
C++ Conditionals: Switch Statement
The C++ switch conditionals are used to test a variable against multiple values and execute the matching case. Let us understand the structure of switch statements in C++.
switch (variable) { case value1: // code for value1 break; case value2: // code for value2 break; default: // code if no match } |
We will understand the Switch conditional statement with an example given below.
#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; } |
Ternary Operators in C++ Conditionals
The ternary operator is used to simplify writing and executing the C++ conditionals statement. It is a one line statement of the conditional statement.
variable = (condition) ? value_if_true : value_if_false;
|
Let us check a simple C++ Conditional Example to understand how to use ternary operator in C++ conditional statements.
#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; } |
C++ If Else Statement Exercises With Answers
Let us check some popular C++ conditionals questions with solutions to understand why conditional statements are so important.
1. Write a C++ program to check if a number entered by the user is even or odd.
If the number evaluates to 0 with modulo expression, then it is an even number otherwise the statement evaluates to odd number.
#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; } |
2. Write a C++ program to check if a number is positive, negative, or zero.
This C++ program is used to check for any number which is greater than 0 and then check if it is less than 0. If neither then the output statement evaluates to zero.
#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; } |
4. Write a C++ program to determine the grade of a student based on their marks.
- Marks >= 90: Grade A
- Marks >= 70: Grade B
- Marks >= 50: Grade C
- Marks < 50: Grade F
This program uses an if-else conditional statement to determine the grade of a student based on their marks.
#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; } |
5. Write a C++ program to check if a year entered by the user is a leap year or not. A year is a leap year if:
- It is divisible by 4.
- However, if it is divisible by 100, it should also be divisible by 400.
#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; } |
Learn C++ Programming with PW Skills
Become a C++ programmer and learn Data Structures and Algorithms with PW Skills Self Paced program Decode DSA with C++. Learn with dedicated mentors and in-depth interactive tutorials. Practice real world questions based on all modules in this course. Become a certified developer with this self paced lifetime program on pwskills.com.
C++ Conditionals FAQs
Q1. What are C++ Conditionals?
Ans: The C++ conditionals refers to a block of statements executed on specific conditions. They are very useful in making decisions based on certain conditions which generally evaluates to two options in programming.
Q2. What are various types of C++ Conditionals?
Ans: If statement, If else statement, Else if statement, and Switch statements are some of the most frequently used C++ conditionals.
Q3. Can I use conditionals inside each other in C++?
Ans: Yes, you can nest if, else if, else, and even switch statements inside each other. This is useful when you have to check multiple conditions in a structured way.
Q4. What is the default condition in the Switch statement in C++?
Ans: The switch statement default condition evaluates when no condition matches in the conditional statement. We must use a break statement in every statement for matching conditions and avoid executing the default condition.