Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 02:09

  • Please register to post and access all features, it's quick, easy and FREE!


Can someone explain this output to me?

Reply
 
Old   #1
 
elite*gold: 0
Join Date: May 2010
Posts: 352
Received Thanks: 23
Can someone explain this output to me?

switch ('B')
{
case 'A' : puts ("Eins");
case 'A'+1: puts ("Zwei");
case 'A'+2: puts ("Drei");
default : puts ("Null");
}

why is the output of this code

Zwei
Drei
Null

i understand the Zwei because 'A'+1 is equivalent to B, but wouldnt 'A'+2 be C? Does default mean that it is always an element of the output?



Walter_White is offline  
Old 02/15/2018, 17:10   #2
 
elite*gold: 22
Join Date: May 2011
Posts: 516
Received Thanks: 178
Code:
switch('B') {
    case 'A':     puts("Eins");
                  break;        
    case 'A' + 1: puts("Zwei");
                  break;
    case 'A' + 2: puts("Drei");
                  break;
    default:      puts("Null");
                  break; #not necessary
};
you forgot the breaks. otherwise it will run through after the first matched case.
if no case matches, e.g. input 'D', it will print the default, in this case "Null"


EngelEatos is offline  
Thanks
1 User
Old 02/15/2018, 19:23   #3
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,067
Received Thanks: 501
Switches are jumping tables with the cases being the labels.
On hitting a switch, the PC jumps to the case with the correct value and starts executing every line from this point onwards. Using break you can jump to the end of the switch structure and therefore ommit executing everything behind the break.

If you for example like to execute the same code for 3 different values you can solve it like this:
Code:
switch (x) {
  case 1:
  case 2:
  case 3:
    Foo();
    break;
  default: Bar();
}
As no instruction is between the three cases, they all label the same instruction (Foo), so in case of 1 2 or 3 the program flow will jump to Foo.
Another example
Code:
switch (x) {
  case 1
    Foo();
  case 2:
    Bar();
    break;
  case 3: FooBar();
}
In this example entering with 1 will execute foo and then jump to bar and then break out of the switch. On 2 it will jump over the Foo call directly to Bar call, will continue to break and exit the switch.
In case 3 the execution jumps directly to the case 3 label skipping the foo and the bar call as well as the break, executing FooBar() and then reaching the end of the switch and exit it.

Because of the implementation as jumping tables switches are more efficient than ifs, but can be used for ordinal types only
warfley is offline  
Thanks
1 User
Reply



« Pointers and Objects of classes | Problem with reading dll »

Similar Threads
Can't start, someone can help/explain?
06/20/2013 - Archlord - 1 Replies
Hello I can't start the game on, i login perfectly etc (at the website) en then: http://www.elitepvpers.com/forum/members/4923576-tibbie-albums12754-error-picture43304-erroral.png (In FAQ there is no 10011 error code lol, only 10010..?) When i want to start with easylogin the first 3 things are V (good)
Can someone explain me these
05/18/2008 - Dekaron - 5 Replies
Hi yesterday i was in crespo dungeon and suddenly the icon in picture appears. Can someone explain me what this icon means. Thank you in advance http://img210.imageshack.us/img210/4645/imagehd1.jpg
TBot Nice And Clean Can Someone Explain That
11/09/2007 - SRO Hacks, Bots, Cheats & Exploits - 3 Replies
So ? http://i174.photobucket.com/albums/w95/mompel/bot.jpg
Can someone explain me?
09/14/2005 - Lineage 2 - 0 Replies
Hi guys :) i play on a C2 hispanic private server, i'd love to know how to use correctly l2walker, i'm actually with the 10.23 version of it, and i don't know how to connect to that server, i think i introduce correctly the things but then it doesn't seem so, can you help me please / if you can give me screens i'll be really thankfull) I attach the server info to see if you can tell me which port and IP i must introduce. Thanks to all and congrats for the good work you do :)



All times are GMT +1. The time now is 02:09.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Abuse
Copyright ©2018 elitepvpers All Rights Reserved.