decision making statements

decision making statements

decision making statements

In C++, decision making statements are used to execute different code blocks based on whether a condition is true or false. The main decision making statements in C++ are if, else, else if, switch, and the ternary operator.

  1. if statement: The if statement is used to execute a block of code only if a certain condition is true. The general syntax for an if statement is as follows:
				
					if(condition) {

    // code to be executed if condition is true

}


				
			

For example, the following code uses an if statement to print a message if the variable x is greater than 10:

				
					int x =12;

if (x> 10) {

    std::cout << "x is greater than10" << std::endl;

}
				
			

2. else statement: The else statement is used to execute a block of code if the condition in the preceding if statement is false. The general syntax for an if-else statement is as follows:

				
					if(condition) {

    // code to be executed if condition is true

} 

else {

    // code to be executed if condition is false

}

				
			

For example, the following code uses an if-else statement to print a message if the variable x is less than or equal to 10:

				
					int x =8;

if (x> 10) {

                  std::cout << "x is greater than 10" << std::endl;

}

 else {

    std::cout << "x is less than or equal to 10" << std::endl;

}


				
			

4. else if statement: The else if statement is used to test multiple conditions in sequence, and execute different blocks of code depending on which condition is true. The general syntax for an if-else if statement is as follows:

				
					if(condition1) {

    // code to be executed if condition1 is true

} 

else  if (condition2) {

    // code to be executed if condition2 is true

}

 else {

    // code to be executed if all conditions are false

} 
				
			

For example, the following code uses an if-else if statement to print a message based on the value of the variable x:

				
					int x = 5;

if (x> 10) {

  std::cout << "x is greater than 10" << std::endl;

} 

else if (x < 0) {

    std::cout << "x is negative" << std::endl;

}

 else {

    std::cout << "x is between 0 and 10" << std::endl;

}
				
			

5 Nested if else:

In C++, you can use nested if-else statements to create more complex conditional logic. Here’s an example:

				
					#include<iostream>

int main() {

    int age;

    std::cout << "Enter your age:";

    std::cin >> age;

    if (age >= 18) {

        std::cout << "You are an adult.\n";

if (age >= 65) {

            std::cout << "You are a senior citizen.\n";

        }
else {

            std::cout << "You are not a senior citizen.\n";

        }

   } 

else {

        std::cout << "You are not an adult.\n";

    }

    return 0;

}
				
			

In this example, we prompt the user to enter their age, and read the input into an int variable called age. We then use an if-else statement to check whether the user is an adult, and nest another if-else statement inside the first one to check whether the user is also a senior citizen.

4. switch statement: 

The switch statement is used to test the value of a variable or expression against a series of possible values, and execute different blocks of code depending on which value matches. The general syntax for a switch statement is as follows:

If the user is not an adult, we skip the inner if-else statement and print a message indicating that the user is not an adult.

Note that the indentation of the code is important to make it clear which statements belong to which conditional blocks.

 

				
					switch(expression) {

    case value1:

        // code to be executed if expression equals value1

        break;

    case value2:

       // code to be executed if expression equals value2

        break;

    // more cases can be added here

    default:

        // code to be executed if none of the cases match

        break;

}
				
			

For example, the following code uses a switch statement to print a message depending on the value of the variable day:

				
					int day= 3;

switch(day) {

    case 1:

        std::cout << "Monday"<< std::endl;

        break;

    case 2:

         std::cout << "Tuesday" << std::endl;

        break;

    case 3:

       std::cout <<"Wednesday" << std::endl;

        break;

    default:

        std::cout << "Invalid day" << std::endl;

        break;

}
				
			

These decision-making statements are essential for making complex programs that require branching and making decisions based on different conditions.

 

Nested switch:

In C++, you can use nested switch statements to create more complex branching logic. Here’s an example:

				
					#include<iostream>

int main() {

    int x = 1;

    int y = 2;

    switch (x) {

        case 1:

            std::cout << "x is 1\n";

            switch (y) {

                case 1:

                    std::cout << "y is 1\n";

                    break;

                case 2:

                    std::cout << "y is 2\n";

                    break;

                default:

                    std::cout << "y is not 1 or 2\n";

                    break;

            }

            break;

        case 2:

            std::cout << "x is 2\n";

            break;

        default:

            std::cout << "x is not 1 or 2\n";

            break;

    }

    return 0;

}
				
			

In this example, we define two int variables called x and y. We then use a switch statement to check the value of x. If x is 1, we nest another switch statement inside the first one to check the value of y.

If y is 1, we print a message indicating that y is 1. If y is 2, we print a message indicating that y is 2. Otherwise, we print a message indicating that y is not 1 or 2.

If x is not 1, we check whether x is 2. If x is 2, we print a message indicating that x is 2. Otherwise, we print a message indicating that x is not 1 or 2.

Note that the break statements are used to prevent the code from “falling through” to the next case block. Without the break statements, the code would execute all subsequent case blocks until the end of the switch statement or until a break statement is encountered.

Join To Get Our Newsletter
Spread the love