switch - continue

This is a discussion on switch - continue within the C Programming forums, part of the General Programming Boards category; In my CS course a couple semesters ago at college, my professor stated that you can make a very clever ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    switch - continue

    In my CS course a couple semesters ago at college, my professor stated that you can make a very clever iterative construct using switch and continue statements. He stated that when a continue statement was placed inside of a switch construct, and that continue statement was executed, it would go back up to the top of the switch statement and re-evaluate the switch.

    This intrigued me, as I have been programming in C/C++ for almost 6 years now and have never thought of doing such a thing, or seen anyone else something like that.

    But I thought it would be pretty cool to make an iterative switch statement using continue statements instead of break statements, so I decided to try it out. Unfortunatley it doesnt seem to work. So far I have tried it on gcc, g++, and Turbo C++ and none of them like it.

    Should I email my professor and tell him he was utterly wrong?
    My Website

    "Circular logic is good because it is."

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Should I email my professor and tell him he was utterly wrong?
    Yes. continue can only appear in a loop body. Your professor either has no idea what he's talking about, or is relying on a nonstandard extension.

    On second thought, you may have misunderstood him. If the switch is contained in a loop and you use continue, the next iteration of the loop will be executed and the switch condition re-evaluated. That could be what he meant. For example, this prints only the odd numbers:
    Code:
    for (i = 0; i < 10; i++) {
      switch (i % 2) {
        case 0:
          continue;
        default:
          printf("%d\n", i);
          break;
      }
    }
    Last edited by Prelude; 06-24-2004 at 09:07 AM.
    My best code is written with the delete key.

  3. #3
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Quote Originally Posted by Prelude
    >Should I email my professor and tell him he was utterly wrong?
    Yes. continue can only appear in a loop body. Your professor either has no idea what he's talking about, or is relying on a nonstandard extension.

    On second thought, you may have misunderstood him. If the switch is contained in a loop and you use continue, the next iteration of the loop will be executed and the switch condition re-evaluated. That could be what he meant. For example, this prints only the odd numbers:
    Code:
    for (i = 0; i < 10; i++) {
      switch (i % 2) {
        case 0:
          continue;
        default:
          printf("%d\n", i);
          break;
      }
    }
    do does
    Code:
    for (i = 0; i < 10; i++) {
      switch (i % 2) {
        case 0:
          break;
        default:
          printf("%d\n", i);
          break;
      }
    }
    I doubt that would be what he ment - unless this is just a very vague example where it's rediculous to mention continue instead of break.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-03-2008, 08:48 PM
  2. Continue and switch
    By camzio in forum C Programming
    Replies: 10
    Last Post: 10-04-2008, 08:31 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. switch() inside while()
    By Bleech in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 02:34 AM
  5. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 05:40 PM

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