Switch Statement in C++ for Multiway Branching

lecture 2 switch statement second course n.w
1 / 10
Embed
Share

Learn about the switch statement in C++, a powerful control flow mechanism used as a substitute for long if-else statements. Understand its syntax, usage, and examples to efficiently handle multiple possible outcomes based on expression values in your C++ programs.

  • C++
  • control flow
  • branching
  • switch statement
  • programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Lecture 2 (switch statement) Second course 1st year classes, Mathematics Department, Science College, Mustansiriyah University Dr. Akeel Omairi

  2. . 1 - . .) ( ) ( 2 - 0 ( ) . ( ) ( ) . 20 ) 20 0 ) ( 30 15 ( 3 - C++ IDE . 4 - 5 - akeelomairi@gmail.com

  3. These notes are partially taken from the website ( geeksforgeeks.org )

  4. Switch case statements are a substitute for long if statements that compare a variable to several integral values. The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Switch is a control statement that allows a value to change control of execution. Syntax: switch (expression) { case constant 1: // code to be executed if expression is constant 1; break; case constant 2: // code to be executed if expression is constant 2; break; ... default: // code to be executed if n doesn't match any cases }

  5. Notes: 1) The value of the expression is compared with the values of each case ( compared with constant 1, constant 2, ). 2) If there is a match, the associated block of code is executed. 3) The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. 4) The break statement is optional. If omitted, execution will continue into the next case. The flow of control will fall through to subsequent cases until a break is reached. 5) The default statement is optional. Even if the switch case statement does not have a default statement, it would run without any problem. However, it is good to put it because it is going to execute a code if there is no case match.

  6. Example: Write a program that asks to enter a number from 1 to 4 and prints the translation of that number in Spanish. # include <iostream> using namespace std; int main() { int x; cout << "Please Enter a number from 1 to 4: "; cin>> x; switch (x) { case 1: cout << "One in Spanish is Uno"; break; case 2: cout << "Two in Spanish is Dos"; break; case 3: cout << "Three in Spanish is Tres"; break; case 4: cout << "Four in Spanish is Cuatro"; break; default: cout << "Sorry, Choose a number from 1 to 4"; break; } return 0; }

  7. Output: After run the program, Im going to enter the number 4. Please Enter a number from 1 to 4: 4 Four in Spanish is Cuatro Suppose I run the program again, in this time I m going to enter the number 5. Please Enter a number from 1 to 4: 5 Sorry, Choose a number from 1 to 4 Notes about the program: As you notice we put numbers (1 to 4) after each case ( instead of constant 1, constant 2, ) because the expression inside switch ( x) will takes the numbers from 1 to 4.

  8. Example: write a program that reads a letter (A, B, C, or D) and prints a name of fruit starts with this letter. # include <iostream> using namespace std; int main() { char x; cout << "Please Enter a letter from A to D: "; cin>> x; switch (x) { case 'A': cout << "A for Apples"; break; case 'B': cout << "B for Bananas"; break; case 'C': cout << "C for Carrots"; break; case 'D': cout << "D for Dates"; break; default: cout << "Sorry, Choose a letter from A to D."; break; } return 0; }

  9. Homework: Solve the two previous examples using nested if statement instead of switch statement.

Related


More Related Content