techmore.in

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:

  1. if statement
  2. if-else statement
  3. if-else if-else statement
  4. switch statement
  5. 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:

cpp
if (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:

csharp
x 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:

cpp
if (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:

vbnet
x 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:

cpp
if (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:

vbnet
x 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:

cpp
switch (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 break statement is used to exit the switch block after a case is executed, preventing it from "falling through" to subsequent cases.
  • The default case 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:

mathematica
Wednesday

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:

cpp
condition ? 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:

makefile
y: 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:

css
a is less than b and b is less than c

Summary

  • The if, if-else, and if-else if-else statements allow decision-making based on conditions.
  • The switch statement 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.