- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Conditions
C++ provides several ways to implement conditions in a program, allowing decisions to be made based on certain criteria. The main conditional statements in C++ are:
- if statement
- if-else statement
- if-else if-else statement
- switch statement
- Conditional (Ternary) Operator
1. if statement
The if statement executes a block of code only if a specified condition is true. If
the condition evaluates to false, the code inside the if block is skipped.
Syntax:
cppif (condition) {
// Code to be executed if the condition is true
}
Example:
cpp#include
using namespace std;
int main() {
int x = 10;
if (x > 5) {
cout << "x is greater than 5" << endl;
}
return 0;
}
Output:
csharpx is greater than 5
2. if-else statement
The if-else statement adds an alternative block of code to execute if the condition
is false.
Syntax:
cppif (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example:
cpp#include
using namespace std;
int main() {
int x = 3;
if (x > 5) {
cout << "x is greater than 5" << endl;
} else {
cout << "x is less than or equal to 5" << endl;
}
return 0;
}
Output:
vbnetx is less than or equal to 5
3. if-else if-else statement
The if-else if-else statement allows you to test multiple conditions. The first
true condition will have its corresponding code block executed, and the rest will be ignored.
Syntax:
cppif (condition1) {
// Code to be executed if condition1 is true
} else if (condition2) {
// Code to be executed if condition1 is false and condition2 is true
} else {
// Code to be executed if both condition1 and condition2 are false
}
Example:
cpp#include
using namespace std;
int main() {
int x = 10;
if (x > 15) {
cout << "x is greater than 15" << endl;
} else if (x == 10) {
cout << "x is equal to 10" << endl;
} else {
cout << "x is less than 10" << endl;
}
return 0;
}
Output:
vbnetx is equal to 10
4. switch statement
The switch statement is used to evaluate a variable against multiple possible
values. It is an alternative to a series of if-else if statements when you are checking a single
variable for different constant values.
Syntax:
cppswitch (expression) {
case value1:
// Code to be executed if expression == value1
break;
case value2:
// Code to be executed if expression == value2
break;
// Add more cases as needed
default:
// Code to be executed if none of the cases match
}
- The
breakstatement is used to exit the switch block after a case is executed, preventing it from "falling through" to subsequent cases. - The
defaultcase is optional and is executed if none of the cases match.
Example:
cpp#include
using namespace std;
int main() {
int day = 3;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
default:
cout << "Invalid day" << endl;
}
return 0;
}
Output:
mathematicaWednesday
5. Conditional (Ternary) Operator
The conditional (ternary) operator is a shorthand way to write an if-else
statement. It evaluates a condition and returns one value if the condition is true and another value if the
condition is false.
Syntax:
cppcondition ? value_if_true : value_if_false;
Example:
cpp#include
using namespace std;
int main() {
int x = 10;
int y = (x > 5) ? 100 : 0; // If x > 5, y is assigned 100; otherwise, y is 0
cout << "y: " << y << endl;
return 0;
}
Output:
makefiley: 100
Relational and Logical Operators in Conditions
In conditions, relational and logical operators are used to evaluate expressions.
Relational Operators:
==: Equal to!=: Not equal to<: Less than>: Greater than<=: Less than or equal to>=: Greater than or equal to
Logical Operators:
&&: Logical AND (true if both operands are true)||: Logical OR (true if at least one operand is true)!: Logical NOT (true if the operand is false)
Example Using Relational and Logical Operators:
cpp#include
using namespace std;
int main() {
int a = 5, b = 10, c = 15;
if (a < b && b < c) {
cout << "a is less than b and b is less than c" << endl;
}
return 0;
}
Output:
cssa is less than b and b is less than c
Summary
- The
if,if-else, andif-else if-elsestatements allow decision-making based on conditions. - The
switchstatement is useful for multiple value checks of a single variable. - The ternary operator provides a shorthand form of
if-else. - Relational and logical operators are used to form conditions within these statements.