How to avoid 'goto' | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to avoid 'goto'

Why goto should be avoided and how to make my code without it?

13th Mar 2017, 3:44 PM
Martin Markov
Martin Markov - avatar
3 Answers
+ 3
use a while loop instead. while(i < 10) { cout << "enter number greater than 10"<<endl; cin >> i; } no need to use goto at all
13th Mar 2017, 4:24 PM
Nikunj Arora
Nikunj Arora - avatar
+ 1
goto makes it hard to reason about code that uses it. You shouldn't need it to accomplish anything. If you have code using goto, post it and we'll come up with an equivalent version without it.
13th Mar 2017, 3:50 PM
Igor B
Igor B - avatar
0
ok can someone make this without goto #include <iostream> using namespace std; int main() { int i = 0; again: cout <<"Enter number greater than 10"<<endl; cin >> i; if (i < 10) { goto again; } cout << i; return 0; }
13th Mar 2017, 3:58 PM
Martin Markov
Martin Markov - avatar