Thread: Case statement help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Exclamation Case statement help

    I am using this site to teach myself C++ and am doing very well. I am taking notes on everything that I read (hell I even write down the quizes and grade myself). One thing that I don't really understand is the following question:

    What is the result of the folowing code:
    Code:
    x=0;
    switch(x) {
    case1: cout<<"One";
    case0: cout<<"Zero";
    case2: cout<<"Hello World";
    }
    The answer to the question is: ZeroHello World
    I just don't understand how the program would come out with this out come. Can anyone help me understand?

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Because it starts at x, then goes in ascending order until it reaches a break. for example:

    Code:
    x=0;
    switch(x)
    {
    case 1: cout<<"One"; break;
    case 0: cout<<"Zero";
    case 2: cout<<"Two";
    }
    The preceding code would print: ZeroTwo. break's tell the switch statement where to stop, not putting them in makes the statement go to the next number in the list.

    EDIT: I shouldn't say number, it goes to the next case in the list, mb
    Last edited by durban; 10-30-2005 at 02:22 PM.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    Thank you for clearing that up for me. I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  2. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  3. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  5. Strings and Switch Statement
    By Ajsan in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2004, 01:16 PM