C++

C++ Control Structures: If Else and Switch-Case Statements

771
0
C++ Control Structures: If Else and Switch-Case Statements

Welcome to this in-depth article on C++ Control Structures! This article will focus on two of the most commonly used control structures in C++, If Else and Switch-Case Statement.

Before diving into the nitty-gritty of these structures, let’s first define what a control structure is. In programming, control structures are used to dictate the flow of execution of a program. They allow us to make decisions and execute certain actions based on those decisions.

Now, let’s get to the good stuff!

If Else Statement

The If Else Statement is a fundamental control structure in C++. It allows us to execute different blocks of code based on whether a certain condition is true or false. Here is the basic syntax for an If Else Statement:

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

Let’s break down this syntax a bit. The if keyword is used to start the statement, followed by a condition that is enclosed in parentheses. If this condition evaluates to true, then the code block immediately following the if statement is executed. If the condition is false, the code block following the else statement is executed instead.

Examples

Here are some examples of If Else Statements in action:

int x = 10;

if (x > 5) {
  cout << "x is greater than 5" << endl;
} else {
  cout << "x is less than or equal to 5" << endl;
}
C++

In this example, the condition x > 5 is true, so the first code block is executed, resulting in the output “x is greater than 5”.

int x = 2;

if (x > 5) {
  cout << "x is greater than 5" << endl;
} else {
  cout << "x is less than or equal to 5" << endl;
}
C++

In this example, the condition x > 5 is false, so the second code block is executed, resulting in the output “x is less than or equal to 5”.

Nested If Else Statements

If Else Statements can also be nested within each other, allowing for more complex decision-making. Here is an example:

int x = 10;
int y = 5;

if (x > 5) {
  if (y > 3) {
    cout << "x is greater than 5 and y is greater than 3" << endl;
  } else {
    cout << "x is greater than 5 but y is less than or equal to 3" << endl;
  }
} else {
  cout << "x is less than or equal to 5" << endl;
}
C++

In this example, the first condition x > 5 is true, so we move on to the nested If Else Statement. The condition y > 3 is also true, so the first code block within the nested statement is executed, resulting in the output “x is greater than 5 and y is greater than 3”.

Switch-Case Statement

The Switch-Case Statement is another common control structure in C++. It allows us to execute different blocks of code based on the value of a variable. Here is the basic syntax for a Switch-Case Statement:

switch (variable)
{
case value1:
    // code block to be executed if variable equals value1
    break;
case value2:

    // code block to be executed if variable equals value2
    break;
// add as many cases as needed
default:
    // code block to be executed if variable does not equal any of the cases
    break;
}
C++

Let’s break down this syntax a bit. The switch keyword is used to start the statement, followed by the variable whose value we want to check. The case keyword is used to specify a possible value for the variable. If the variable equals the specified value, then the code block following that case is executed. The break keyword is used to exit the switch statement after a case is executed.

The default case is optional and is executed if the variable does not equal any of the cases specified.

Examples

Here are some examples of Switch-Case Statements in action:

int day = 4;

switch (day)
{
case 1:
    cout << "Monday" << endl;
    break;
case 2:
    cout << "Tuesday" << endl;
    break;
case 3:
    cout << "Wednesday" << endl;
    break;
case 4:
    cout << "Thursday" << endl;
    break;
case 5:
    cout << "Friday" << endl;
    break;
case 6:
    cout << "Saturday" << endl;
    break;
case 7:
    cout << "Sunday" << endl;
    break;
default:
    cout << "Invalid day" << endl;
    break;
}
C++

In this example, the variable day is equal to 4, so the code block following the case 4 statement is executed, resulting in the output “Thursday”.

char grade = 'B';

switch (grade) {
  case 'A':
  case 'a':
    cout << "Excellent" << endl;
    break;
  case 'B':
  case 'b':
    cout << "Good" << endl;
    break;
  case 'C':
  case 'c':
    cout << "Fair" << endl;
    break;
  case 'D':
  case 'd':
    cout << "Poor" << endl;
    break;
  default:
    cout << "Invalid grade" << endl;
    break;
}
C++

In this example, the variable grade is equal to ‘B’, so the code block following the case 'B' statement is executed, resulting in the output “Good”.

Nested Switch-Case Statements

Like If Else Statements, Switch-Case Statements can also be nested within each other. Here is an example:

int x = 10;
char grade = 'A';

switch (x) {
  case 10:
    switch (grade) {
      case 'A':
        cout << "x is 10 and grade is A" << endl;
        break;
      case 'B':
        cout << "x is 10 and grade is B" << endl;
        break;
      default:
        cout << "x is 10 and grade is neither A nor B" << endl;
        break;
    }
    break;
  default:
    cout << "x is not 10" << endl;
    break;
}
C++

In this example, the variable x is equal to 10, so we move on to the nested Switch-Case Statement. The variable grade is equal to ‘A’, so the first code block within the nested statement is executed, resulting in the output “x is 10 and grade is A”.

Conclusion

That’s a wrap on If Else and Switch-Case Statements in C++. These statements are crucial for controlling the flow of execution in your program and making decisions based on certain conditions. With If Else Statements, you can choose between two or more code blocks to execute depending on the outcome of a condition, while Switch-Case Statements allow you to choose between multiple code blocks based on the value of a variable.

As you become more comfortable with these control structures, you will find that they are incredibly versatile and can be used to solve a wide variety of problems. Whether you are building a simple calculator or a complex video game, If Else and Switch-Case Statements will be your go-to tools for making decisions and controlling the flow of your program.

So, keep practicing and experimenting with these control structures, and before you know it, you’ll be writing efficient, flexible, and powerful C++ code like a pro!

xalgord
WRITTEN BY

xalgord

Constantly learning & adapting to new technologies. Passionate about solving complex problems with code. #programming #softwareengineering

Leave a Reply