im a noob not rod

This is a discussion on im a noob not rod within the C++ Programming forums, part of the General Programming Boards category; I'm really new to this and I started reading the tutorials and got to a quiz and I don't understand ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    im a noob not rod

    I'm really new to this and I started reading the tutorials and got to a quiz and I don't understand why the answer is what it is. I think this is pretty confusing stuff. I have plenty of experience with html though.

    Here is the question:

    4. What is the result of the following code?

    x=0;

    switch(x)

    {

    case 1: cout<<"One";

    case 0: cout<<"Zero";

    case 2: cout<<"Hello World";

    }

    A. One
    B. Zero
    C. Hello World
    D. ZeroHello World

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    There is a prefectly good explanation for this behavior. The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly.

    In C and C++ the switch statement allows an implicit fall-through at the end of a case. For example:
    Code:
    switch (value) {
    	case 0:
    		printf("In case 0\n");
    	case 1:
    		printf("In case 1\n");
    	case 2:
    		printf("In case 2\n");
    	break; 
    }
    The code above, when value was 0, would execute case 0, case 1, and case 2; therefore, printing:
    Code:
    In case 0
    In case 1
    In case 2
    Simply, the reason your program displays that way is because your case is falling through the other statements. To prevent that, break each of your case expressions. The break statement causes an immediate exit from the switch. Because cases serve just as labels, after the code for one case is done, execution falls through to the next unless you take explicit action to escape.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    You have the right idea - what you're forgetting is that case statements fall through, unless you explicitly use break.

    So in your example, x is 0, so the "cout <<"Zero";" statement executes. But then execution carries on with the next statement - "cout<<"Hello World";".

    If you didn't want that behaviour, you'd do:
    PHP Code:
     case 1:
     
    cout<<"One";
    break;

    case 
    0
    cout<<"Zero";
    break;

    case 
    2:
     
    cout<<"Hello World";
    break; 

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    So case 0 executes first? If so, why is that?

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In many systems cases are implimented by a jump table using an offset. This is possible because the case values have to be integers. What is happening is that it the cases are being rearranged so your compiler can do this. I am not sure if that is defined behaviour or not.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    So if it rearranges so 0 executes first then what happens to case 1?

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok nm I misread the output
    it executed case 0 first because thats the value of x

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    then what happens to 1?
    Is it just tossed aside or what?
    because how i see it the answer should have the one it as well

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I probably could have gotten a simpler answer if I would have told you this is a question from one of the quizes. I'm not trying to put this into a program or anything........... least not yet

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    its not tossed aside or like skipped over. Think of this way: Your program is constantly falling from the "top" to the "bottom". It will keep going this way until you tell it to go somewhere else. When you have a switch() you basically tell it to go to the case that has the same value as what you are switching on. From there it continues falling. break tells it to go to the end of your switch and continue from there. The jump table I was refering to just says where to jump to.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Alright I think I've got it thanks for all the help everybody.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. street Rod 3
    By cdoublejj in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 08-04-2008, 02:08 AM
  2. my noob program doesnt work
    By Scarvenger in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2005, 12:40 PM
  3. noob here
    By lilhawk2892 in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 11:12 AM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 09:49 AM
  5. RoD Stands for something??
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 09-28-2004, 01:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21