Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-20-2004, 02:53 AM   #1 (permalink)
cracksevi
Registered User
 
Join Date: Oct 2004
Posts: 14
cracksevi is on a distinguished road
Send a message via ICQ to cracksevi
whats that?

while i was reading a tutorial from a site i reached the goto thingy...but the example thats given there didnt explain really enuf to me....can someone tell me what is that "goto" for?


// goto loop example
#include <iostream.h>
int main ()
{
int n=10;
loop:
cout << n << ", ";
n--;
if (n>0) goto loop;
cout << "FIRE!";
return 0;
}

and the output is : 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!


thats the example in the site...but it seems to have no difference than while command..even while command could be more useful there..another explanation can be so nice.....
thanx bros !
cracksevi is offline   Reply With Quote
Old 10-20-2004, 09:12 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Do you see the "loop:" tag in the code?
The "goto" keyword goes to that tag. Goto's are evil.
__________________
Valmont is offline   Reply With Quote
Old 10-24-2004, 03:44 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
Quote:
Originally posted by Valmont
Goto's are evil.
They're only evil if you dont know what you're doing.
cracksevi, the code could have been written as:
Code:
#include <iostream.h>
int main ()
{
  int n=10;
  while(n > 0)
  {  
    cout << n << ", ";
    n--; 
  }
  cout << "FIRE!";
  return 0;
}
The execution will jump to the loop lable in the begining of the code, as long as the if (n>0) condition is true.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-24-2004, 08:03 PM   #4 (permalink)
Kernel_Killer
Regular Contributor
 
Kernel_Killer's Avatar
 
Join Date: Feb 2003
Location: indisclosed
Posts: 210
Kernel_Killer is on a distinguished road
Why they added GOTO's to C/C++ is beyond me. They were great back in the BASIC days, but really when dealing with C/C++ while/for/switch,etc are much better.
__________________
Network Synapse
Screaming Electron
Kernel_Killer is offline   Reply With Quote
Old 10-25-2004, 04:22 PM   #5 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
gotos are incredibly useful in some circumstances, but they are rare and must be used wisely. you don't need goto to write bad code, and c is not in the business of stopping you from doing stupid things, so there's no reason to take away a valuable tool. that's what java is for.

remember the c programmer's motto: "don't fuk up".
joe_bruin is offline   Reply With Quote
Old 10-25-2004, 04:40 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Perhaps they can be usefull in C. In C++ they are never needed I think unless one insists on an ( occasional) procedural programming.
__________________
Valmont is offline   Reply With Quote
Old 10-25-2004, 06:05 PM   #7 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
exceptions in c++ replace the use of gotos in many instances.
joe_bruin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:17 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting