Thread: something weird in C.

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    something weird in C.

    Hi, I'm struggling to do a code to get totally out from the two for's loop whenever the i=0 and completing to the other row codes if founded, I've put a condition if for that, but one break isn't sufficient for getting out from two for's loops, so what should I do in this case to eliminate automatically the two loop for whenever I arrive to i=0? (I tried to put return instead of break, but it will get out from whole the function itself, and that's unlike what I wanted..)
    Code:
    int main()
    {
    	int m = 0;
    	for (int i = 0; i < 3; ++i)
    		for (m; m < 3; m++)
    			if (i == 0)
    			{
    				break;
    			}
    			else
    				printf("Cboard");
            printf("a new comer is gonna be over here sooner");
    }

    Any clue? thanks!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    "break" will only get you out of the current loop.

    There are a few possible way to do what you're asking. One way is to use a simple flag, which is checked during the conditional of the outer loop.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,850
    I am a little puzzled by your example though: i == 0 will be true on the very first iteration of the outer loop, so it will be true on the very first iteration of the inner loop, hence the code (once modified to break out of the outer loop) can be reduced to:
    Code:
    int main()
    {
        printf("a new comer is gonna be over here sooner");
    }
    Note that your indentation is misleading: the second printf statement is at the level of the body of the main function, not the body of the outer for loop. I recommend that you always use braces to demarcate the body of your if statements and loops, even when unnecessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi

    Besides any issues mentioned above...if you wanted to get out of the loops...I can't see a reason to not use a goto . Its not the ideal choice and not a popular choice...but can't see why it would not do the job...

    John

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    I am a little puzzled by your example though: i == 0 will be true on the very first iteration of the outer loop, so it will be true on the very first iteration of the inner loop, hence the code (once modified to break out of the outer loop) can be reduced to:
    Code:
    int main()
    {
        printf("a new comer is gonna be over here sooner");
    }
    Note that your indentation is misleading: the second printf statement is at the level of the body of the main function, not the body of the outer for loop. I recommend that you always use braces to demarcate the body of your if statements and loops, even when unnecessary.
    laser, I just coded the code spontaneously(logically unplanned), I just wanted to get the idea of how can I get out from the two for's ..

    anyone, thanks for your cooperation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with some weird bug.
    By Absurd in forum C++ Programming
    Replies: 13
    Last Post: 07-29-2013, 01:37 PM
  2. This is really weird..
    By Eman in forum C++ Programming
    Replies: 12
    Last Post: 03-17-2011, 09:37 AM
  3. Seriously Weird
    By Dae in forum Linux Programming
    Replies: 5
    Last Post: 09-11-2009, 02:40 AM
  4. weird
    By ala negru in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 06:51 AM
  5. Weird
    By ednayap in forum C Programming
    Replies: 4
    Last Post: 06-21-2003, 09:13 AM