View previous topic :: View next topic |
Author |
Message |
dahowarduk
Joined: 24 Jun 2020 Posts: 11
|
Posted: Sun Jul 05, 2020 8:50 pm Post subject: GO TO |
|
|
How do I construct a GOTO IN FTN95?
I entered the line GOTO 48
and got the error message LABEL48 HAS NOT BEEN DEFINED.
But I couldn't find out how to define a label.
Thanks |
|
Back to top |
|
|
Kenneth_Smith
Joined: 18 May 2012 Posts: 697 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Sun Jul 05, 2020 9:03 pm Post subject: |
|
|
Code: | program demo
i = 0
48 i = i + 1
print*, i
if (i .lt. 10) goto 48
end |
|
|
Back to top |
|
|
dahowarduk
Joined: 24 Jun 2020 Posts: 11
|
Posted: Sun Jul 05, 2020 11:12 pm Post subject: |
|
|
thanks..but that is sort of what I did...
43 GOTO 48
44
45 ENDIF
46
47 ENDDO
48 CALL SOLVE (CELL)
and I just get the message Label48 has not been defined.
What am I missing?
thanks |
|
Back to top |
|
|
Kenneth_Smith
Joined: 18 May 2012 Posts: 697 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Sun Jul 05, 2020 11:43 pm Post subject: |
|
|
The line numbers of the text file containing the code cannot be used as labels.
The line you want to jump to needs to be given a numerical label (conventionally in columns 1 to 5) and the GOTO statement points to that number.
In my example label 48 is on the third line. The advantage of this is if I then inset text above the third line, the GOTO statement still points towards the correct label even although its line number has changed.
Showing line numbers the above example appears as:-
Quote: | 1 program demo
2 i = 0
3 48 i = i + 1
4 print*, i
5 if (i .lt. 10) goto 48
6 end
|
|
|
Back to top |
|
|
dahowarduk
Joined: 24 Jun 2020 Posts: 11
|
Posted: Mon Jul 06, 2020 8:56 am Post subject: |
|
|
Got it. Brilliant...thanks |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7925 Location: Salford, UK
|
Posted: Mon Jul 06, 2020 9:07 am Post subject: |
|
|
dahowarduk
If you are a beginner then it is advisable to avoid using GOTO. Use other constructs such as IF/THEN and DO/ENDDO whenever possible.
GOTO can be found in old Fortran (pre Fortran 77) and is a nightmare to maintain - sometimes referred to a spaghetti code. |
|
Back to top |
|
|
dahowarduk
Joined: 24 Jun 2020 Posts: 11
|
Posted: Mon Jul 06, 2020 9:11 am Post subject: |
|
|
Yes. I sort of remember that advice for many years ago. I think it was called 'structured programming'...1 way in and 1 way out . Before I got your solution, I'd managed an alternative process via using a DO WHILE, which kept to the 'structured' approach. |
|
Back to top |
|
|
|