+ 42

How to use "goto" in c++?

20th Nov 2017, 2:11 AM
Ahmed Heakl
Ahmed Heakl - avatar
63 Respostas
+ 45
You shouldn't use goto. It's a bad coding practice and makes your code hard to read. Google "Spaghetti Code". https://www.tutorialspoint.com/cplusplus/cpp_goto_statement.htm
20th Nov 2017, 2:15 AM
ChaoticDawg
ChaoticDawg - avatar
+ 44
guys this is horrible,I don,t know why people downvote such questions,is,nt sololearn a place for begginers to learn,then why people downvote genuine queries and upvote useless stuffs unrelated to programming.Don,t discourage begginers from asking questions!! This question has got 2 downvotes,if you don,t want,t to answerlthen why downvote. btw,he only asked how to use it,he did,nt ask whether it's a good programming practice or not!!
20th Nov 2017, 5:12 AM
Abhinav
Abhinav - avatar
+ 38
Firstly, I'd like to say thank you to all friends for adjusting the programming path for learners and making them aware of bad practices in programming. Even though fooling around with goto guy isn't considered a clean way to deal with the conditional branching, but for the sake of experiment and to see how it's been used in some legacy codes (or perhaps refreshing your assembly memory!), it's worth it to do a quick review as part of a simple loop. (In fact, the inspiration comes from the fact that compilers generating assembly code out of your c++ code, and interestingly, loop stuff and their mechanisms after conversion, contain a couple of goto statements.) My experiment : [https://code.sololearn.com/cF3K2X2c3AC3] // ============================ // dependent numbers sequence // Done for: Bela Kristianti // By : Babak Sheykhan // ============================ #include <iostream> using namespace std; int main() { int x = 0; int y = 0; int i = 1; do { cout << "Enter x: "; cin >> x; } while (!(x >= 7 && x <= 100)); do { cout << "Enter y: "; cin >> y; } while (!(y >= 4 && y <= x)); cout << endl; // Loop using goto start: if (x > 0) { if (i <= y) { cout << i++ << " "; --x; } else i = 1; goto start; } else goto end; end:; } The main Idea was introduced by Mr. JPM7. Credit: [https://code.sololearn.com/cvL7PY7iXp35] Mr.JPM7 profile: [https://www.sololearn.com/Profile/1866768]
20th Nov 2017, 5:06 AM
Babak
Babak - avatar
+ 13
Don't use it, not recommended
20th Nov 2017, 2:28 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 12
check here:https://code.sololearn.com/cKYTp2xlVlK0/?ref=app goto is use in this simple manner and definately you can easily understand how and it's work in c++: program: #include<stdio.h> int main() { int a; a=10; start:a++; if(a<=20) { printf("%d\n",a); goto start; } } output: 11 12 13 14 15 16 17 18 19 20 https://www.sololearn.com/Profile/928735/?ref=app
26th Nov 2017, 3:27 PM
ASIF BILAKHIYA
ASIF BILAKHIYA - avatar
+ 9
using goto is not good coding practice.
24th Nov 2017, 6:16 AM
Adem AKKUŞ
Adem AKKUŞ - avatar
+ 9
you should not ise goto in codes.
24th Nov 2017, 6:16 AM
Adem AKKUŞ
Adem AKKUŞ - avatar
+ 8
@ChaoticDawg, I meant every link is a goto. It is strange we won't accept it in programming. But on internet we can't live without it, internet is really a collection of goto's.
20th Nov 2017, 5:35 AM
Paul
Paul - avatar
+ 7
@Paul I provided that particular link about goto because not only does it answer the OP's question on how to use it, but it also briefly discusses why you shouldn't use it in addtion to when you may consider using goto in an appropriate manner. If you've ever written a large batch (CMD) style program/script or similar that uses goto like syntax, you'd understand why the use of goto is discouraged. I've been there and it really sucked trying to keep things straight. Makes one really appreciate the functions, methods and loop styles most higher level languages have.
20th Nov 2017, 5:24 AM
ChaoticDawg
ChaoticDawg - avatar
+ 7
yeah.. i think sololearn police just dragged him out..😝😝😝😝😝😝
20th Nov 2017, 4:26 PM
Andrew Ting Mai Zau
Andrew Ting Mai Zau - avatar
+ 6
It is funny, never use goto in programming, they say. Then they give you a link, to goto. In the past I have used goto. It was always difficult to maintain your programs. Goto used line numbers. If you changed your program, lines numbers changed, and goto's didn't work anymore, because you forgot to change them.
20th Nov 2017, 5:12 AM
Paul
Paul - avatar
+ 6
#include <iostream> using namespace std; int main () { int age; LOOP:do { cout<<"Enter your age:"; cin>>age; if( age<10) { goto LOOP; } else{ cout << "Good to go"; break; } } while( true); return 0; } //if this is not your answer.. please go to //stockoverflow and ask for help 😄😄😄😄 //God Bless
20th Nov 2017, 4:53 PM
Andrew Ting Mai Zau
Andrew Ting Mai Zau - avatar
+ 6
You have answers, so...this time with references: My position remains the same: skilled, conscious deliberate use -- know why you're using it, when it results in cleaner, safer code -- and why to do it in c but not c++: Linus Torvalds (wrote Linux): https://www.kernel.org/doc/Documentation/process/coding-style.rst "Centralized exiting of functions (section 7): The goto statement comes in handy when a function exits from multiple locations and some common work such as cleanup has to be done. " Linux kernel 4.14.1 is 3 days old. In fork.c: http://elixir.free-electrons.com/linux/latest/source/kernel/fork.c#L547 Ref: https://www.quora.com/What-are-the-commonly-used-C-idioms-in-Linux-Kernel "Sometimes ... valid to use GOTO as an alternative to exception handling within a single function: <code sample> COM [Component Object Model] code seems to fall into this pattern fairly often." https://stackoverflow.com/a/46638 "...[it's a new generation, so the original meaning of the warning got lost; today] the biggest problem with goto is [probably social]." https://stackoverflow.com/a/25773503 A kernel pattern example (C) https://stackoverflow.com/a/245761 Google "Go" source code, lines 186 + 193: https://golang.org/src/math/gamma.go Ref: https://stackoverflow.com/a/11065563 When to use goto when programming in C https://www.cprogramming.com/tutorial/goto.html Clarity (like preventing memory leaks), optimization, finite state machines: https://blog.smartbear.com/development/goto-still-has-a-place-in-modern-programming-no-really/ "Mea culpa! Sometimes, the experts agree, GOTO can be very useful."
23rd Nov 2017, 8:05 PM
Kirk Schafer
Kirk Schafer - avatar
+ 5
goto label; // label is your target code . /* . some . code . . */ label: // jumped here leaving some code // your code goto is mainly used for jumping statements that is why it is criticized, as it disrupts flow of control
20th Nov 2017, 8:06 AM
#RahulVerma
#RahulVerma - avatar
+ 5
using goto isn't really a good practice, particularly in c++, there are different ways to alter program flow and goto isn't a good choice. now your question : how to use goto: eg #include <iostream> int main(){ int age; reEnterAge: std::cout<<"enter your age"; //create a label std::cin>>age; if(age < 10 ) goto reEnterAge; //alter program flow back to label else std::cout<<"good to go"; }
20th Nov 2017, 4:40 PM
Germain F
Germain F - avatar
+ 5
gotoxy (x,y)
21st Nov 2017, 8:25 AM
Anil Kumar
Anil Kumar - avatar
+ 4
@Ahmed is your question academic or practical in nature? Do you have a particular use case or problem that you are trying to solve? Some commenters have already stated the conventional wisdom to avoid goto statements. Generally, this is good advice but you may have a good reason to use goto. Often there is a better way of coding your design with using other safer and more common coding idioms (e.g. if/else, switch, while, for...).
20th Nov 2017, 2:16 PM
Aaron Becker
Aaron Becker - avatar
+ 4
yeah.. he should stop...maybe @Ahha is just a kid...3 or 4 years 😀😀😀😀😀
20th Nov 2017, 4:22 PM
Andrew Ting Mai Zau
Andrew Ting Mai Zau - avatar
+ 4
in my country, some guys are doing like that..really notorious....without drinking anything they just did stupid things.. i didn't get it untill now😜😜😜
20th Nov 2017, 4:32 PM
Andrew Ting Mai Zau
Andrew Ting Mai Zau - avatar
+ 4
goto goto statement transfer the control to the label specified with the goto statementlabel is any name give to particular part in programlabel is followed with a colon (:) Syntax : label1: - - goto label1;
21st Nov 2017, 4:57 AM
Ankit
Ankit - avatar