Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jump backwards to the beginning with go to order? 1

Status
Not open for further replies.

granbycools

Programmer
Jul 18, 2012
8
IL
Hi everyone,

at a certain point in my code I would like to jump backwards to the beginning of my code, in order to carry out a new run
with the before calculated value. So far I´ve tried to use a "go to "marker"" order and placed the marker where I would like to
start again. However that doesn´t work, the " go to 900" order seems to create a building failure. Any ideas would be very much appreciated.


granby
 
Best would be, if you provide us with a piece of your code.

A structure like this should be working - but is considered to be not so easy to debug spaghetti coding:

Code:
program xy

statements
900 statement
...
...
...
if (condition) goto 900
...

end program

Note: You must enter some condition to exit the loop you are creating or your prog will run and never complete.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
That is what I´ve tried so far..

Code:
..
..
900 continue
if(jump .eq. 0)jump = 1
..
..
..
if(jump .eq. 1) go to 901
jump = 0 go to 900

901 continue
..
Still got an building failure.
 
But your problem seems to be this
Code:
jump = 0 go to 900
This isn't one statement but two.
 

There shouldn't be a need to use GOTO...you should be able to achieve the same thing with a DO or DO WHILE and with
EXIT and CYCLE.

As it is, your jump composition is a bit confusing...what's the value of jump coming into the loop? The way I see it, whether it is 0 or 1, does not matter, your loop will only be done once...unless you are modifying jump in the portion of the code not being shown.
 
As it is, your jump composition is a bit confusing...what's the value of jump coming into the loop?

... that's why spaghetti coding should be avoided.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Easiest old-fashioned way is to draw a flowchart and then pretend that there is a string at the entry and exit points and pull the string straight. Then redraw the chart and write the code accordingly. It is a simple technique and can sometimes simplify relatively complicated looking loops.
 
Sure, do/while loops would have been the best way possible…but
+as there are about 3000 lines existing program code between the two algorithms
+each algorithm is in another subroutine,
… I was unable to fit loop in the program structure. In fact I haven´t been able to jump from the second subroutine to the first one in order to transfer the desired value from subroutine 2 into subroutine 1.
ANy ideas would be very much appreciated..:)
 
3000 lines are not that many
if you could organize your code in nice subroutines, that is even better, then your do loop is really short making just a few calls
if you think you can have a "go to xxxx" in one subroutine and a "xxxx continue" in another, your are mistaking :)
if you are thinking about getting into a subroutine at another point other than the beginning...you are not ready for it :)

if anything, follow xwb advice, breaks your code a bit more into smaller subroutines so that your jump-back locations are in-between subroutines, so that you can place a "go to" right there outside subroutines and at the main level and part of your loop.
 
You are right, but I can´t split the program into more subroutines because this hole existing code is a big mess. I am also far away from good enough in programming to figure that out...if I split it up even more, I will have the risk, that the assignment for other variables won´t work any longer.
I am pretty sure I am not ready putting both subroutines in a loop as well.. But I need this value and transfer to the other subroutine to create a iteration.. any advice?
 
well, the first thing that comes to mind is to use a common block with an explicit name and that possibly only exists in the two subroutines for which you want to establish communications...then, put there whatever variables you want to share between the two subs. Or, if you are passed f77, you can use a module with the variables and then use a "USE" in those subs, etc.
 
There are three ways "to transfer the desired value from subroutine 2 into subroutine 1":
1. Use subroutine parameters...
2. ...or some kind of common storage (old-fashioned common blocks or modules in modern fortran)...
3. ...or external storage (write then read files).

But you never transfer data between program units with gotos or another control flow jumps. Think about what do you want really. It seems gotos and loops do not bear a relation to your problem...
 
Hhhhm,

as mikrom said, 3000 lines of code is not that much. But you can create a spaghettiknot with much less than this.

You seem caught in between your project, but this is a feeling we all have once in a while. And forget about this good at programming issue. The structures within Fortran to control the flow are not too many and not too difficult to understand. Start methodically on xwb's advice and you will get to grips with your code eventually.

I do not understand the thing with these algorithms. Are they provided to be used by your program or did you program them or do you want to modify them ?

Sit back and try to think out what it is you want to do using small and easy steps. Tiny steps. I wonder why Salgerman did not mention his babysteps.

Norbert




The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
@Norbert. It is a modification. I´ve already modified the two mentioned algorithms (optimization of a vortex lattice method). But in order to complete the modification,
I need to create a iterative procedure or rather a loop running as long as my convergence criterion isn´t archieved -.- ( and I need both results in sub1 for that..argh)

@ salgerman, ArkM , gummibär: Thanks, for ur response. I am trying to work something out with ur suggestions!;)
 
sounds like you should use a structure like this

Code:
program myprogram

(data declaration statements)

(establish starting conditions)
lCriterion = .false.

do while (.not. lCriterion)
    call sub1 (params)
    call sub2 (other params)   
    if (conditions are met) lCriterion = .true.
enddo
present results

end program

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top