Thread: The Goto Staetment

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    The Goto Staetment

    hi all ,
    i have a question about the goto statement;
    when execute it does it break the loop;
    for example

    Code:
    #include<stdio.h>
    main()
    {
            int f;
    
    
                   for(f=0;f<10;f++)
                    {
    
                             if(f==5)
                            {
                            goto here;
    
                            }
                    printf("%d\n",f);
    
                    }
    
    here:{
                 printf("The End\n");
         }
    }
    when i execute it
    C@St0rM-MaN:~/Desktop/c# ./goto
    0
    1
    2
    3
    4
    The End
    it break the loop when it's called
    my question is when execute it does it break the loop?
    thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    So you proved it breaks the loop, but you want to know if it breaks the loop?

  3. #3
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by MacGyver View Post
    So you proved it breaks the loop, but you want to know if it breaks the loop?
    kind of stupid i know
    but i just want to make sure that's there is nothing wrong with my code
    it's better than not asking and just ignore it
    by the way : it's 5 am now and i haven't sleep yet

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Your view of what happens is correct; goto breaks from the loop.

    The usage of goto is extremely discouraged, however. For learning what it is and what it does, I'm not in objection to. For real usage in real programs, I would suggest other tools be employed other than goto.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    For similiar code in the real world you do not use 'goto'. You would just use 'break'.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  3. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  4. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM
  5. I need a faster way to do this....
    By frenchfry164 in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2002, 05:05 PM