Decision Making in Cpp

Decision-making is vital to programming, allowing programs to make choices based on specific conditions. It involves evaluating conditions and executing different code paths accordingly. This concept empowers programmers to create dynamic and responsive applications that adapt to user inputs and scenarios. Effective decision-making enhances program logic, efficiency, and problem-solving capabilities. It enables developers to write code that solves complex tasks and makes informed choices. By understanding decision-making concepts, programmers can create intelligent and versatile applications that provide a seamless user experience.

Conditional Statements

A. If statement

Syntax and usage:

The "if" statement in C++ allows for conditional execution of code based on a specific condition.

Syntax :

if (condition) {
     // code to execute 
}

Single and multiple conditions:

The "if" statement can evaluate a single condition using relational or logical operators.

Multiple conditions can be evaluated using logical operators like "&&" (AND) or "||" (OR).

Nested if statements:

"If" statements can be nested within each other to handle complex conditions.

This allows for different code blocks to be executed based on various combinations of conditions.

Example:

#include <iostream>
int main() {
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;
    if (number > 0) {
        std::cout << "The number positive." << std::endl;
    } else if (number < 0) {
        std::cout << "The number is negative." << std::endl;
    } else {
        std::cout << "The number is zero." << std::endl;
    }
    return 0;
}

Explanation:

  1. The program starts by declaring an integer variable called number.
  2. The user is prompted to enter a number using std::cout and std::cin.
  3. The if statement is used to check the value of number.
  4. If number is greater than 0, the code block inside the first if statement is executed, which displays "The number is positive."
  5. If number is not greater than 0, the else if statement is evaluated. If number is less than 0, the code block inside the else if statement is executed, displaying "The number is negative."
  6. If both the conditions of the if and else if statements are false, the code block inside the else statement is executed, displaying "The number is zero."

Here's an example run of the program:

Enter a number: 7
The number is positive.

In this case, the user entered the number 7, which is greater than 0, so the output states that the number is positive.

Enter a number: -5
The number is negative.

In this case, the user entered the number -5, which is less than 0, so the output states that the number is negative.

Enter a number: 0
The number is zero

In this case, the user entered the number 0, so the output states that the number is zero.

The if statement allows you to make decisions based on conditions, making it a fundamental control flow construct in C++.

B. If-else statement

Syntax and usage:

The "if-else" statement provides an alternative execution path when the condition in the "if" statement is not met.

Syntax:

if (condition) {
    // code to execute if the condition is true
}
else {
    // code to execute if the condition is false
}

Handling alternative conditions:

The "if-else" statement allows different code blocks to be executed based on whether the condition is true or false.

It provides a way to handle alternative scenarios and perform different actions accordingly.

Example:

#include <iostream>
int main() {
int number;
    std::cout << "Enter a number: ";
    std::cin >> number;
    if (number % 2 == 0) {
        std::cout << "The number is even." << std::endl;
    } else {
        std::cout << "The number is odd." << std::endl;
    }
    return 0;
}

Explanation:

  1. The program starts by declaring an integer variable called number.
  2. The user is prompted to enter a number using std::cout and std::cin.
  3. The if statement is used to check the remainder of number divided by 2 (number % 2).
  4. If the remainder is equal to 0, it means the number is divisible by 2 and therefore even. The code block inside the if statement is executed, which displays "The number is even."
  5. If the remainder is not equal to 0, it means the number is not divisible by 2 and therefore odd. The code block inside the else statement is executed, displaying "The number is odd."

Here's an example run of the program:

Enter a number: 4
The number is even.

In this case, the user entered the number 4, which is divisible by 2, so the output states that the number is even.

Enter a number: 7
The number is odd.

In this case, the user entered the number 7, which is not divisible by 2, so the output states that the number is odd.

The if-else statement allows you to perform different actions based on a condition. If the condition is true, the code block inside the if statement is executed; otherwise, the code block inside the else statement (if present) is executed.

C. Switch statement

Syntax and usage:

The "switch" statement allows for multi-way branching based on the value of a variable or an expression.

Syntax:

switch (expression) {.
    case value1:
        // code to execute if expression equals value1
        break;
    case value2:
        // code to execute if expression equals value2
        break;
    // more cases...
    default:
        // code to execute if expression doesn't match any case
        break;
}

Handling multiple cases:

The "switch" statement checks the value of the expression against different cases and executes the corresponding code block.

It provides an efficient way to handle multiple cases without using various "if" statements.

Understanding the syntax and usage of if statements, if-else statements, and switch statements in C++ allow programmers to control the flow of execution based on specific conditions, enabling dynamic and versatile program behavior.

Example:

int main() (
    char grade;
    std::cout << "Enter your grade (A-F): ";
    std::cin>> grade;
    switch (grade) {
    case 'A':
        std::cout << "Excellent!" << std::endl;
        break;
    case 'B':
    case 'C':
        std::cout << "Well done!" << std::endl;
        break;
    case 'D':
        std::cout << "You passed." << std::endl;
        break;
    case 'F':
        std::cout << "You failed." << std::endl;
        break;
    default:
        std::cout << "Invalid grade." << std::endl;    
        break;
    }
    return 0;
}

Explanation:

  1. The program starts by declaring a character variable called grade.
  2. The user is prompted to enter their grade (a single character from 'A' to 'F') using std::cout and std::cin.
  3. The switch statement is used to perform different actions based on the value of grade.
  4. In this example, each case represents a possible value of grade. If the value matches the corresponding case, the code block following that case is executed.
  5. If grade is 'A', it displays "Excellent!". If grade is 'B' or 'C', it displays "Well done!". If grade is 'D', it displays "You passed.". If grade is 'F', it displays "You failed.".
  6. If none of the case values match the value of grade, the code block inside default is executed, displaying "Invalid grade."

Here's an example run of the program:

Enter your grade (A-F): B
Well done!

In this case, the user entered 'B', and the program displays "Well done!" as the corresponding message.

Enter your grade (A-F): F
You failed.

In this case, the user entered 'F', and the program displays "You failed." as the corresponding message.

Enter your grade (A-F): X
Invalid grade.

In this case, the user entered 'X', which doesn't match any of the cases, so the program executes the code block inside default and displays "Invalid grade."

This smaller example demonstrates how a switch statement can be used to handle different cases based on the value of a variable.


Learn via Video Course

C++ Programming (English) Logo

C++ Programming (English)

2056

18 hrs