Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Conditions & Statements

Conditions & Statements

You already know that C++ supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

In C++, you can employ various conditional statements to control the flow of your program based on different conditions. These statements include:

  1. if Statement: It allows you to execute a block of code if a specified condition is true. If the condition evaluates to false, the code inside the if block is skipped.
  2. else Statement: This statement is used in conjunction with if. It specifies a block of code to be executed if the condition in the corresponding if statement is false.
  3. else if Statement: It provides a way to test multiple conditions. If the condition in the preceding if statement is false, the program evaluates the condition in the else if statement. If it is true, the code inside the corresponding block is executed.
  4. switch Statement: It allows you to specify multiple alternative blocks of code to be executed based on the value of a variable or an expression. Each block corresponds to a particular value or range of values.

These conditional statements help you write more dynamic and flexible programs by allowing different code paths to be taken based on various conditions. They are crucial tools for implementing decision-making logic in your C++ programs.

The if Statement

Use the if statement to specify a block of C++ code to be executed if a condition is true.

Syntax:

if (condition) {
  // block of code to be executed if the condition is true
}

Example:

#include <iostream>
using namespace std;

int main() {
  if (20 > 18) {
    cout << "20 is greater than 18";
  }  
  return 0;
}

Output:

20 is greater than 18

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax:

if (condition) {
  // block of code to be executed if the condition is true
} else {
 // block of code to be executed if the condition is false

Example:

  #include <iostream>
  using namespace std;

  int main() {
    int time = 20;
    if (time < 18) {
      cout << "Good day.";
    } else {
      cout << "Good evening.";
    }
    return 0;
  }

Output:

Good evening.

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax:

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
 // block of code to be executed if the condition1 is false and condition2 is true
} else {
 // block of code to be executed if the condition1 is false and condition2 is false
}

Example:

#include <iostream>
using namespace std;

int main() {
  int time = 22;
  if (time < 10) {
    cout << "Good morning.";
  } else if (time < 20) {
    cout << "Good day.";
  } else {
    cout << "Good evening.";
  }
  return 0;
}

Output:

Good evening.

Short Hand If…Else (Ternary Operator)

The shorthand if…else, also known as the ternary operator in C++, is a concise way to write conditional expressions. It’s particularly useful when you want to assign a value to a variable based on a condition.

Here’s the basic syntax:

variable = (condition) ? expressionTrue : expressionFalse;

Instead of writing:

Example:

#include <iostream>
using namespace std;

int main() {
  int time = 20;
  if (time < 18) {
    cout << "Good day.";
  } else {
    cout << "Good evening.";
  }
  return 0;
}

Output:

Good evening.

You can simply write:

Example:

#include <iostream>
#include <string>
using namespace std;

int main() {
  int time = 20;
  string result = (time < 18) ? "Good day." : "Good evening.";
  cout << result;
  return 0;
}

Output:

Good evening.

C++ Switch Statements

Use the switch statement to select one of many code blocks to be executed.

Syntax:

switch(expression) {
  case x:
    // code block
 break;
 case y:
 // code block
 break;
 default:
 // code block
}

This is how its work:

  1. The switch expression is evaluated once.
  2. The value of the expression is compared with the values of each case.
  3. If there is a match between the value of the expression and a case, the associated block of code is executed.
  4. The break keyword, when present, terminates the switch statement, preventing execution from “falling through” to subsequent cases. It is optional, meaning you can omit it if you want execution to continue to the next case.
  5. The default case, when provided, acts as a catch-all for values that do not match any of the specified cases. If no cases match and there is a default case, its associated block of code will be executed. The default case is also optional.

Example:

#include <iostream>
using namespace std;

int main() {
  int day = 4;
  switch (day) {
  case 1:
    cout << "Monday";
    break;
  case 2:
    cout << "Tuesday";
    break;
  case 3:
    cout << "Wednesday";
    break;
  case 4:
    cout << "Thursday";
    break;
  case 5:
    cout << "Friday";
    break;
  case 6:
    cout << "Saturday";
    break;
  case 7:
    cout << "Sunday";
    break;
  }
  return 0;
}

Output:

Thursday

The break Keyword

When C++ reaches a break keyword, it breaks out of the switch block.

This will stop the execution of more code and case testing inside the block.

When a match is found, and the job is done, it’s time for a break. There is no need for more testing.

The default Keyword

The default keyword specifies some code to run if there is no case match:

Example:

#include <iostream>
using namespace std;

int main() {
  int day = 4;
  switch (day) {
    case 6:
      cout <<"Saturday";
      break;
    case 7:
      cout << "Sunday";
      break;
    default:
      cout << "Looking forward to the Weekend";
  }
  return 0;
}

Output:

Looking forward to the Weekend

    Leave a Reply

    Your email address will not be published.

    Need Help?