Switch and whiles in C++

Switch and whiles in C++

Table of Contant

  • C++ Switch Statements

  • The break Keyword

  • The default Keyword

  • Example for Switch

  • While loop

  • Loops in C++

  • C++ While Loop

  • Example for While loop

  • do while loop

  • The Do/While Loop

  • Example for do While loop

  • And Examples Outputs

C++ Switch Statements

Use theswitchstatement to select one of many code blocks to be executed.The switch statement in C++ is the best alternative to the lengthy if statements that are used to compare a variable to different integral values. It is a multi-way branch statement. The switch statement is the control statement that allows any value to change the control of the execution.

This is how it works:

  • The switch expression is evaluated once

  • The value of the expression is compared with the values of each case

  • If there is a match, the associated block of code is executed

  • The break and default keywords are optional, and will be described later in this chapter

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

Thedefaultkeyword specifies some code to run if there is no case match.Thespecifies the default block of code in a switch statement, which is the code to run if there is no case match in the switch. Note: if the default keyword is used as the last statement in a switch block, it does not need a break.

Example for Switch

1)Check the Months in Switch

#include <iostream>
using namespace std;

int main() {
    int month;
    cout << "Enter Month Number: ";
    cin >> month;

    switch(month){
    case 1:
        cout<<"January \n";
        break;
    case 2:
        cout<<"February \n";
        break;
    case 3:
        cout<<"March \n";
        break;
    case 4:
        cout<<"April \n";
        break;
    case 5:
        cout<<"May \n";
        break;
    case 6:
        cout<<"June \n";
        break;
    case 7:
        cout<<"July \n";
        break;
    case 8:
        cout<<" August \n";
        break;
    case 9:
        cout<<"September\n";
        break;
    case 10:
        cout<<"October \n";
        break;
    case 11:
        cout<<" November\n";
        break;
    case 12:
        cout<<" December \n";
        break;
    default:
        cout<<"This not month \n";


        }

    return 0;
}

Ouput of program:

input (9)

2)Check vowel or not vowel

Ouput of program:

input "I"

3)Choice numbers and print welcome words

Ouput of program:

input "3"

While loop

Loops in C++

Loops can execute a block of code as long as a specified condition is reached.Loops are handy because they save time, reduce errors, and they make code more readable.

C++ While Loop

Thewhileloops through a block of code as long as a specified condition istrue.Thea type of loop that will first evaluate a condition. If the condition is true, the program will run the code inside of the while loop. It will then go back and re-evaluate the condition. Every time the condition is true, the program will perform the code inside the loop.

while (condition) {
  // code block to be executed
}

Example for While loop

4)draw a star diagram

Ouput of program:

5)Positive from while

Ouput of program:

input "10"

do while loop

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

do {
  // code block to be executed
}
while (condition);

Example

Ouput of program:

input "4,8,0,7"

  • Variable Declaration:

    • int number; declares an integer variable number.
  • do-while Loop:

    • The do block contains the code that will execute at least once, regardless of the condition.

    • std::cout << "Enter a number greater than 10: "; prompts the user to enter a number.

    • std::cin >> number; reads the number entered by the user.

    • The while condition number <= 10 checks if the entered number is less than or equal to 10. If it is, the loop will repeat, prompting the user to enter a number again.

Ouput of program:

Output 5,24

Once the user enters a number greater than 10, the loop terminates, and the program prints a message indicating the number entered and that the program ends.