Valhalla Legends Forums Archive | C/C++ Programming | Break/Continue in switch nested in while

AuthorMessageTime
Myndfyr
I'm working on converting LibMPQ to C# at the moment, and I'm running into a problem.  There's a situation like this:
[code]
while (condition) {
  switch (value) {
     case a:
        if (condition2) {
          break;
        }
        continue;
  }
}
[/code]
That's greatly simplified of course.  I want to know if the break breaks the switch or the while.  That's it!  Thanks!

[edit] The reason I ask is because the guy makes liberal use of continue in the code -- that's the only time break is used, though.  If break breaks the switch, then it is effectively identical to the use of continue in this case; if it breaks the loop, that's a BIG difference.
June 18, 2005, 8:39 AM
LoRd
break ends the most current loop or conditional statement, which in your case would be the switch statement.
June 18, 2005, 9:59 AM
OnlyMeat
[quote author=MyndFyre link=topic=11140.msg106811#msg106811 date=1112384409]
I know both C/++ AND Java, as well as Assembly.  I also know old-school BASIC.  I don't know what to pick!
[/quote]

LMAO.
June 18, 2005, 12:08 PM
Kp
[quote author=LoRd[nK] link=topic=11882.msg116360#msg116360 date=1119088745]
break ends the most current loop or conditional statement, which in your case would be the switch statement.[/quote]

Minor point, but the wording of this seems to imply that break can get you out of an if.  It can't.  break ends switch, for, while, do {} while, but not if.
June 18, 2005, 3:30 PM
kamakazie
[quote author=OnlyMeat link=topic=11882.msg116362#msg116362 date=1119096509]
[quote author=MyndFyre link=topic=11140.msg106811#msg106811 date=1112384409]
I know both C/++ AND Java, as well as Assembly.  I also know old-school BASIC.  I don't know what to pick!
[/quote]

LMAO.
[/quote]

Same thread a little further down:

[quote author=MyndFyre link=topic=11140.msg107293#msg107293 date=1112627531]
I know (in order of comfort levels):
C#/VB.NET/J#
Java
JavaScript (again, scripting) in browsers, WSH, and IIS
C/++, in a pinch.
BASIC
Assembly, if I really have to
[/quote]

You're an annoying troll.
June 18, 2005, 6:22 PM
Myndfyr
[quote author=Kp link=topic=11882.msg116369#msg116369 date=1119108610]
[quote author=LoRd[nK] link=topic=11882.msg116360#msg116360 date=1119088745]
break ends the most current loop or conditional statement, which in your case would be the switch statement.[/quote]

Minor point, but the wording of this seems to imply that break can get you out of an if.  It can't.  break ends switch, for, while, do {} while, but not if.
[/quote]

Yeah, I knew that much.  I was pretty sure that break ended the switch, and that's what I was betting on (I finished the conversion shorly after the post), but I wanted to confirm.  Like I said, there wasn't any difference in using break or continue if it broke the switch, but it's just unusual to see continue in any context.

Thanks to those of you who are intelligent.  OnlyMeat -- you may wish to consult my signature for the cure for your condition (to clarify, it's the latter).
June 18, 2005, 7:44 PM
Kp
[quote author=MyndFyre link=topic=11882.msg116397#msg116397 date=1119123872]
Yeah, I knew that much.  I was pretty sure that break ended the switch, and that's what I was betting on (I finished the conversion shorly after the post), but I wanted to confirm.  Like I said, there wasn't any difference in using break or continue if it broke the switch, but it's just unusual to see continue in any context.[/quote]

I expected you knew that break had no effect on an if, but I wanted to correct the mis-implication in case a newbie stumbled into the thread later.  I disagree about continue being unusual; a quick search reveals it occurs over 200 times just in some of my Linux code. :)
June 18, 2005, 8:22 PM
St0rm.iD
[quote author=dxoigmn link=topic=11882.msg116383#msg116383 date=1119118949]
[quote author=OnlyMeat link=topic=11882.msg116362#msg116362 date=1119096509]
[quote author=MyndFyre link=topic=11140.msg106811#msg106811 date=1112384409]
I know both C/++ AND Java, as well as Assembly.  I also know old-school BASIC.  I don't know what to pick!
[/quote]

LMAO.
[/quote]

Same thread a little further down:

[quote author=MyndFyre link=topic=11140.msg107293#msg107293 date=1112627531]
I know (in order of comfort levels):
C#/VB.NET/J#
Java
JavaScript (again, scripting) in browsers, WSH, and IIS
C/++, in a pinch.
BASIC
Assembly, if I really have to
[/quote]

You're an annoying troll.
[/quote]

Actually, it appears that you and OnlyMeat are the annoying trolls here.

[quote author=Adron link=topic=11793.msg115502#msg115502 date=1118595968]
Edit: Throwing in more examples of how virtually every post by OnlyMeat is incorrect...



[quote author=OnlyMeat link=topic=11793.msg115401#msg115401 date=1118454495]
Seems clear to me you are saying the code snippet above doesn't allocate any memory. This is incorrect, if you don't believe me run the code in my reply, you will find the size of the variable buf == 1 (the null terminator). This is clearly allocating 1 byte on the stack giving you a valid zero length string:-

[code]
char* buf = "";
char* buf2="hello";
cout << sizeof(*buf) << buf2 << endl;
[/code]
[/quote]


#1: It's not allocating a byte on the stack
#2: It may not be allocating anything at all
#3: You're not testing for anything being allocated; example:
[code]
char* buf = 0;
char* buf2="hello";
cout << sizeof(*buf) << buf2 << endl;
[/code]

Tell me how assigning null to a pointer would be allocating a byte for it? Yet according to you, sizeof says it does? ;)



[quote author=OnlyMeat link=topic=11793.msg115415#msg115415 date=1118463847]
Say what you like but the point is buf points to 1 byte of memory. You just agreed with that thanks :).
[/quote]

Memory is committed (in win32) in 4096 byte blocks, also known as pages. Your pointer will point to somewhere in your address space. There most likely won't be just 1 byte available at the location buf points to, much like when you allocate 5 bytes there won't most likely be just 5 bytes where your pointer points to. Maybe what you wanted to say was "buf points to 1 byte of memory allocated for you to use as you like"? Unfortunately that'd be incorrect as well...



[quote author=OnlyMeat link=topic=11793.msg115419#msg115419 date=1118470515]
It has a static lifetime, however it must still be allocated and loaded into memory at runtime. Which proves the point that the variable buf will point to 1 byte of data. It doesn't matter if it's on the stack or allocated when the program starts statically it will be allocated in memory.
[/quote]

There will be memory for the '\0', that's all you can say. That memory may have been allocated for the byte, or it may have been allocated for something else and reused for the '\0'. The memory may be allocated a load-time, or it may have been allocated much earlier. An example would be writing code for a BIOS chip, in which case you might say the memory was allocated when the EEPROM was written. The point is that the declaration char *buf = ""; not necessarily causes an allocation, and particularly doesn't give you  memory that you can do anything other than read a '\0' from.

[/quote]
June 19, 2005, 5:24 PM

Search