How do I terminate a FOR Loop?

This is a discussion on How do I terminate a FOR Loop? within the A Brief History of Cprogramming.com forums, part of the Community Boards category; In the below FOR Loop, I only want VALUE FOUND or VALUE NOT FOUND printed once. Right now depending on ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Question How do I terminate a FOR Loop?

    In the below FOR Loop, I only want VALUE FOUND or VALUE NOT FOUND
    printed once. Right now depending on what array[index] the value is in
    (ie. if it's in the third index) it will print VALUE NOT FOUND twice, then
    VALUE FOUND.

    How do I modify to print either statement 1x? Partial Code follows...
    -----------------------------------------------------------------------------------------------------

    cin >> choice;
    for (int count = 0; count < 5; count++)
    {
    if (numbers[count] == choice)
    cout << "VALUE FOUND: " << choice << endl;
    else
    cout << "VALUE NOT FOUND:" << endl;
    }
    -----------------------------------------------------------------------------------------------------
    Thanks for any assistance. I'm a beginner by the way.

    -JY

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    That loop should stop once it gets to 4.
    Hope that helps i'm new to this also. See ya.

  3. #3
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    i'm not sure this is what you want. but try break;.
    Code:
    for (int count = 0; count < 5; count++)
    {
    	if (numbers[count] == choice)
    	{
    		cout << "VALUE FOUND: " << choice << endl;
    		break;
    	}
    	else
    	{
    		cout << "VALUE NOT FOUND:" << endl;
    		break;
    	}
    }
    Sometimes, the farthest point from the center is the center itself.

    Your life is your canvas, it's only as beautiful as you paint it.

  4. #4
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704

    simple

    cin >> choice;
    int fail = 1; initialize as a fail.
    for (int count = 0; count < 5; count++)
    {
    if (numbers[count] == choice)
    fail = 0; // found the choice
    }
    if(fail == 1);
    cout << "sorry choice not found";
    else // optional
    cout << "Your choice:" << choice << endl;
    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.)

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: simple

    cin >> choice;
    int fail = 1;
    for (int count = 0; count < 5; count++)
    {
    if (numbers[count] == choice)
    fail = 0;
    }
    if(fail == 1); <-- Sorry, no ; here
    cout << "sorry choice not found";
    else
    cout << "Your choice:" << choice << endl;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    a simple typo, not like hed never beable to figure out why it doesnt work when he compiles..
    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.)

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I think what you're looking for was what the guy way up there ^ mentioned. break;. You may want to use an if statement like this:

    while (loop_needs_to_be_run == true)
    {
    // Yada, yada, yada
    if (loop_needs_to_exit == true)
    break;
    }

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Or if you just want to skip to the next value

    continue;
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. terminate() function
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2009, 10:37 AM
  2. Terminate Boost::Thread
    By CornyKorn21 in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2008, 01:19 PM
  3. Terminate() inside constructor
    By Mario F. in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2006, 04:59 AM
  4. how to terminate a target thread
    By swaugh in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2005, 11:13 AM

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