Thread: Question about a program fragment's product~ :o)

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    Question about a program fragment's product~ :o)

    i don't get why the output would be: "onetwo" (i thought it would just be "one")... can any1 explain the logic behind this loco Q?

    *16. What output does the following program fragment produce? (Assume that 'i' is an integer variable.)

    i = 1
    switch (i % 3) {
    case 0: printf("zero");
    case 1: printf("one");
    case 2: printf("two");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It tests to see who is awake, and realises that most uses of switch/case require the use of

    break;

    after each case

    Did you try this for yourself first?

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    It finds the case 1 and executes that, thus the "one". Then the code continues onto the next line and prints "two". Normally in a switch statement, you would put a break after it prints the "one" so it would not invoke case 2.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    silly me (forgetting the break; part!)~!!!! thanx Salem & Cshot~!!!! now i can finally move on to chapter 6 (loops)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  2. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  3. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Replies: 8
    Last Post: 03-26-2002, 07:55 AM