Thread: Whats wrong with my loop?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    106

    Whats wrong with my loop?

    #include <iostream.h>
    #include <conio.h>
    #include <conio.c>


    int main()
    {
    for (int i=0;i<=40;i+=2) {
    cout << i << ", ";

    if(i==10) {
    cout << "DECADE, "; }

    continue;

    if(i==20) {
    cout << "2 DECADES, "; }

    continue;

    if(i==30) {
    cout << "3 DECADES, "; }

    continue; }

    getch();
    return 0;
    }


    It compilers fine. But after i hits 20 , it doesn't say 2 DECADES. The first if statement worked, but the second and third one doesn't.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
      for (int i=0;i<=40;i+=2) {
        if(i==10)
          cout << "\nDECADE, ";
        else if(i==20)
          cout << "\n2 DECADES, ";
        else if(i==30) 
         cout << "\n3 DECADES, ";
        cout << i << ", ";
      }
      getch();
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    106

    ok

    I finally figured out what was wrong. So one told me to take away the continues. But what are the continues for in a loop?
    I saw someone use it before.

  4. #4
    Yes, it was the continues - take them out. "continue" means ignore what's below the continue command and go to the next iteration in the loop.

    continues should only be used inside the scope of if{} statements. Continues are rarely used and are considered bad practice along with using the goto statement. Try not to use either of those statements in your loops next time.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  4. What is wrong with my while loop?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-19-2002, 12:07 PM
  5. Whats wrong w/ my loop?? Please help?
    By aspand in forum C Programming
    Replies: 6
    Last Post: 05-30-2002, 03:42 AM