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

This is a discussion on Question about a program fragment's product~ :o) within the C Programming forums, part of the General Programming Boards category; i don't get why the output would be: "onetwo" (i thought it would just be "one")... can any1 explain the ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    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, 09: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, 06:55 AM

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