473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

break in switch statement

Expand|Select|Wrap|Line Numbers
  1.                   int ch;
  2.                   cin>>ch;
  3.                   switch(ch)
  4.                   {
  5.                    case 1:
  6.                    cout<<"one";
  7.                    break;
  8.                    case 2:
  9.                    cout<<"two";
  10.                    break;
  11.                    case 3:
  12.                    cout<<"three";
  13.                    break;
  14.                   }
  15.  
When we trace the above code, I choose value 3 for the variable ch, then the compiler switches to case 3 and print statement "three". After printing this the compiler goes to case 1's break instead of going case 3's break.why?
Sep 14 '07 #1
4 1245
pbmods
5,821 Expert 4TB
Heya, srshamla. Welcome to TSDN!

I'm going to go ahead and move this thread to the C forum, where our resident Experts will be better able to help you out.
Sep 14 '07 #2
Ganon11
3,652 Expert 2GB
What do you mean, 'it goes to case 1's break'?
Sep 14 '07 #3
sicarie
4,677 Expert Mod 4TB
What do you mean, 'it goes to case 1's break'?
The debugger in step-through mode. I'm guessing that it's some compiler-specific "optimization" or the closing of the switch statement (when you step through, a lot of the time it bounces back up to the check to close the loop). However, the case 3 break is definitely not ignored - you can manipulate its place (put things before and after), and see that it is definitely being caught there.
Sep 14 '07 #4
JosAH
11,448 Expert 8TB
The debugger in step-through mode. I'm guessing that it's some compiler-specific "optimization" or the closing of the switch statement (when you step through, a lot of the time it bounces back up to the check to close the loop). However, the case 3 break is definitely not ignored - you can manipulate its place (put things before and after), and see that it is definitely being caught there.
It's the code generator to blame and the short term memory of it: once the jump
out of the switch statement has been found (possibly it was a 'short' and 'relative'
jump, jump to that jump instruction and all will be fine. Possibly a later code
generating/optimizing pass over the code can remove cascaded jumps as well.

kind regards,

Jos
Sep 14 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Martin Lucas-Smith | last post by:
Is there any need to keep the final break in a switch which uses a default at the end? I.e: switch ($data) { case 'foo': # Action break;
25
by: chunhui_true | last post by:
In <<expert c>>I know the break in if wich is scoped in switch is break the switch,like: switch c case 1: if(b){ break; } ...... But like this: while(a){
7
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and...
55
by: Ennixo | last post by:
hi, do you know where i can find some ebooks or websites talking about C# optimisation ? for exemple, i just learned that ++i is faster than i++. i would like to know more about the things...
14
by: serrand | last post by:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did... int res;...
6
by: David | last post by:
I know that by some reasons... the use of "break;" in java language is not correct, is there any similar problems with c#????
26
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
26
by: a.mil | last post by:
I am programming for code-speed, not for ansi or other nice-guy stuff and I encountered the following problem: When I have a for loop like this: b=b0; for (a=0,i=0;i<100;i++,b--) { if (b%i)...
3
by: Yansky | last post by:
Hi, I've looked through the tutorial on w3cschools.com, but I'm still uncertain as to the difference between using break and using return. If I have a simple "for" loop that I want to stop if a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.