• About WordPress
    • WordPress.org
    • Documentation
    • Support
    • Feedback
  • Log In
  • Register
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment
Cuitutorial
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment

Programming Fundamental

Home » Blog » Switch in c++

Switch in c++

  • Posted by saqib
  • Categories Programming Fundamental
  • Date March 29, 2023
  • Comments 0 comment

Switch statement in c++

Multiple- selection construct that is used in  multi decision  making in an efficient way and is easy to read and understand.





Structure of Switch Statement

switch (expression)

{

   case constant1:

  group of statements 1;

  break;

  case constant2:

  group of statements 2;

  break;

  .

  .

  .

  default:

  default group of statements

}




How switch works?

switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes group of statements 1 until it finds the break statement. When it finds this break statement the program jumps to the end of the switch selective structure

If expression was not equal to constant1 it will be checked against constant2. If it is equal to this, it will execute group of statements 2 until a break keyword is found, and then will jump to the end of the switch selective structure

Finally, if the value of expression did not match any of the previously specified constants (you can include as many case labels as values you want to check), the program will execute the statements included after the default: label, if it exists (since it is optional)

Switch Selection Structure

// counting grades assigned

int aCount, bCount, cCount, dCount, fCount;

switch (grade)

{

case ‘A’: case ‘a’:

++aCount;

case ‘B’: case ‘b’:

++bCount;

case ‘C’: case ‘c’:

++cCount;

case ‘D’: case ‘d’:

++dCount;

default:

++fCount;

}

…………

// counting grades assigned

int aCount, bCount, cCount, dCount, fCount;

switch (grade)

{

case ‘A’: case ‘a’:

++aCount;

break;

case ‘B’: case ‘b’:

++bCount;

break;

case ‘C’: case ‘c’:

++cCount;

break;

case ‘D’: case ‘d’:

++dCount;

break;

default:

++fCount;

}

Break Statement

  • break statement causes program control to move to the first statement after the selection/ repetition structure
  • Switch cases would otherwise run together

Same Behavior

Switch and if else
Switch and if else same behavior





Switch Selection Structure

Switch Selection Structure
Switch Selection Structure

 

  • Share:
author avatar
saqib

Previous post

If Else statement in c++
March 29, 2023

Next post

Software project management
May 19, 2023

You may also like

If Else statement in c++
29 March, 2023

If Else statement /conditional structure —In everyday life, we are often making decision. —“if the age of the person is above 60, then discount the ticket price to 40%” —The above statement have element of decision making. E.g only if …

Data Types in C++
28 February, 2023

Data Types in C++ Unsigned Integers Signed Integers Floating point numbers Characters Logical values Standard Data Types in C++ Following are examples of predefined data types used in C++ int    an integer number (e.g. 10, -5). float    a …

Introduction to Programming Language
20 February, 2023

Introduction to Programming Language  Problem Solving Problem solving is a process of identifying a problem and finding the best solution for it. Problem solving is a skill that can be developed by following a well organized approach. We solve different …

Leave A Reply Cancel reply

You must be logged in to post a comment.

admin@cuitutorial.com
Facebook-f Twitter Youtube Linkedin Instagram Stack-overflow Pinterest Github Quora Whatsapp
Courses
  • All Courses
  • Past Paper
  • Final year projects
  • Interview Questions
  • Contact
Important Pages
  • Privacy Policy
  • Terms of Service
  • Cookie Policy
Links
  • University Events
  • Team
Education & learning platform for All Computer science subjects
Final year projects
Past Paper
Interview questions
Programming, C/C++, Asp.net/MVC. Android, MySql, Jquery, Ajax, javascript, Php, Html5, Bootstrap4.
NTS, GAT, PPSC, FPSC

Copyright © 2021 | Cuitutorial