I am using a tic tac toe code to study for my final exam, and have come across a few things in the code which I dont understand the use for. The word guess appears twice in the program once after declaring that one of the players is defined as the number one; the next line is simply "guess." The final line in the program is "goto guess." Any explanation would be helpful in ensuring my sanity. Thanks
go to and guess
Page 1 of 12 Replies - 1069 Views - Last Post: 16 December 2010 - 01:09 PM
Replies To: go to and guess
#2
Re: go to and guess
Posted 15 December 2010 - 11:44 PM

POPULAR
If you are coding 'goto' in your final exam then I would suggest taking C/C++ classes at another school. If you found this in example code, I would suggest you stop reading it as a useful source.
You will not use goto in C/C++ projects in the professional world. Period. Define functions, call them. It's not terribly difficult. Goto is QBasic stuff. This is a compiler language & should be coded as one.
You will not use goto in C/C++ projects in the professional world. Period. Define functions, call them. It's not terribly difficult. Goto is QBasic stuff. This is a compiler language & should be coded as one.
#3
Re: go to and guess
Posted 16 December 2010 - 01:09 PM

POPULAR
but to answer you question:
The goto statement transfers program flow to a specific named spot int the code:
So the jump right to the line labeled "begin" and begins execution there.
So another thing you will see in projects that use goto are line labels. The syntax for line labels is just a proper identifier name followed by a colon.
So somewhere in the program you are looking at there should be a line labeled "guess:" and that is where the program logic will jump to when the program reaches "goto guess;"
Back in the history of programming goto was the primary method of controling program flow. It simulates the jump symantics used in assembly language programming and so back when programming was less evolved it was used quite a bit. But as programming developed techniques like "structured programming" and structures such as if/if-else/while/do-while/for and function calls were established the use of goto began to fade and now pretty much only crazy recluse programmers and beginners and desperate maintenance programmers ever dare to add a goto-statement to their programs.
First of all, Goto's are not easily parsed by human eyes. structures such as conditional statments and loops are (when properly indented) pretty easy for your eye to scan over and read the logic, goto's require you to parse the logic line for line keeping track of where the labels are and where the jumps are, then there are problems like forward jumps which can be confusing because you have yet to see the label etc.
When you begin to mix forward and backwards jumps you end up with what is affectionately referred to as "spaghetti code" where tracing the flow of logic is like untangling a bowl of spaghetti.
Take my word for it the less you use it, the less you will have to "unlearn" -- it is just better to avoid it all together.
This is HORRIBLE. plus it can make debugging very difficult. So modern programmer's avoid the use of goto and so should you.
The goto statement transfers program flow to a specific named spot int the code:
#include <iostream>
using namespace std;
int main() {
goto begin;
cout << "This line will not happen" << endl;
begin:
cout << "We jumped to here" << endl;
return 0;
}
So the jump right to the line labeled "begin" and begins execution there.
So another thing you will see in projects that use goto are line labels. The syntax for line labels is just a proper identifier name followed by a colon.
So somewhere in the program you are looking at there should be a line labeled "guess:" and that is where the program logic will jump to when the program reaches "goto guess;"
Back in the history of programming goto was the primary method of controling program flow. It simulates the jump symantics used in assembly language programming and so back when programming was less evolved it was used quite a bit. But as programming developed techniques like "structured programming" and structures such as if/if-else/while/do-while/for and function calls were established the use of goto began to fade and now pretty much only crazy recluse programmers and beginners and desperate maintenance programmers ever dare to add a goto-statement to their programs.
First of all, Goto's are not easily parsed by human eyes. structures such as conditional statments and loops are (when properly indented) pretty easy for your eye to scan over and read the logic, goto's require you to parse the logic line for line keeping track of where the labels are and where the jumps are, then there are problems like forward jumps which can be confusing because you have yet to see the label etc.
When you begin to mix forward and backwards jumps you end up with what is affectionately referred to as "spaghetti code" where tracing the flow of logic is like untangling a bowl of spaghetti.
Take my word for it the less you use it, the less you will have to "unlearn" -- it is just better to avoid it all together.
This is HORRIBLE. plus it can make debugging very difficult. So modern programmer's avoid the use of goto and so should you.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|