Quantcast

[Scilab-users] linking (jumping) from line of code to an other

classic Classic list List threaded Threaded
4 messages Options
Frieder Nikolaisen Frieder Nikolaisen
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Scilab-users] linking (jumping) from line of code to an other

Hello,

another question to solve the locomotive stuff, giving you a example with the not implented code. 

I don't want to code the same stuff twice. Thats why I want to jump between lines of code. Is this possible and how? 

P = 200;
DM = 1;

for n = 1:10
    if DM == 1 then 
        if P > 100 then     DM = 1
                            P = P - 60
                            disp('P bigger 100. DM = ' + string(DM))
        else    DM = 0
                            P = P + 100 //instead of writing P = P + 100, I would like ...
                            disp('P smaller 100. DM = ' + string(DM))
        end 
    else // D == 0 
        if P > 100 then DM = 1          
                                        disp('P bigger 100. DM = ' + string(DM))
                        P = P - 30
        else        DM = 0              // ... to continue here 
                                        disp('P smaller 100. DM = ' + string(DM))
                    P = P + 100       // to use this P = P + 100
        end
    end 
    disp(string(n) + ' ' + string(P))
end
 

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Tim Wescott Tim Wescott
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: linking (jumping) from line of code to an other

Jumps are generally bad style; I don't know if Scilab even supports
them.  If it does I advise you not to use them for something this
trivial (error handling is the only place I've seen them seriously
recommended, and even there they can be very problematical -- it's why
people invented exceptions).

For something that small the cost in making your code into spaghetti is
larger than the cost of just doing it.

The two ways that software professionals would deal with this are to
either get rid of 100 as a "magic number" and assign its value to a
variable (i.e., before the "for" statement assign PowerIncrement =
100), or rearrange the conditional so that the P = P + PowerIncrement
line is only encountered once.

Or do both.

Certainly if you use the existing structure and if the two lines that
increment P are supposed to do the same thing always, then using a
named variable is a Good Thing -- that means that at some later date,
when some poor ignorant fool reads your code they don't have to wonder
if the "P+100" in one spot means the same thing as the "P+100" in
another, AND they don't run the risk of changing the (hopefully proper)
increment in one place but not another.

And note, based on my experience writing software for the last 40
years, that often the poor ignorant fool who has to maintain your
software is you, months or years down the road -- so it pays to make
your code readable.  Nothing makes you feel dumber than not being able
to understand your own code, because no matter which way the blame
flies, it always lands squarely on you.

On Fri, 2017-05-12 at 11:15 +0200, Frieder Nikolaisen wrote:

> Hello,
> another question to solve the locomotive stuff, giving you a example
> with the not implented code. 
> I don't want to code the same stuff twice. Thats why I want to jump
> between lines of code. Is this possible and how? 
> P = 200;
> DM = 1;
>
> for n = 1:10
>     if DM == 1 then 
>         if P > 100 then     DM = 1
>                             P = P - 60
>                             disp('P bigger 100. DM = ' + string(DM))
>         else    DM = 0
>                             P = P + 100 //instead of writing P = P +
> 100, I would like ...
>                             disp('P smaller 100. DM = ' + string(DM))
>         end 
>     else // D == 0 
>         if P > 100 then DM = 1          
>                                         disp('P bigger 100. DM = ' +
> string(DM))
>                         P = P - 30
>         else        DM = 0              // ... to continue here 
>                                         disp('P smaller 100. DM = ' +
> string(DM))
>                     P = P + 100       // to use this P = P + 100
>         end
>     end 
>     disp(string(n) + ' ' + string(P))
> end
>  
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.scilab.org/mailman/listinfo/users
--

Tim Wescott
www.wescottdesign.com
Control & Communications systems, circuit & software design.
Phone: 503.631.7815
Cell:  503.349.8432



_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Frieder Nikolaisen Frieder Nikolaisen
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: linking (jumping) from line of code to an other

Dear Tim,

thanks for your answer. I have already the trouble of handling the code from last autum. In the moment I have written my exam, my boss has the trouble all to himself.

I will still use your advice. Unfortuanlly I had to learn in a few weeks coding  on myself. It's surely easy to see by reading the code.

Best regards
Frieder

 

Am 2017-05-12 18:50, schrieb Tim Wescott:

Jumps are generally bad style; I don't know if Scilab even supports
them.  If it does I advise you not to use them for something this
trivial (error handling is the only place I've seen them seriously
recommended, and even there they can be very problematical -- it's why
people invented exceptions).

For something that small the cost in making your code into spaghetti is
larger than the cost of just doing it.

The two ways that software professionals would deal with this are to
either get rid of 100 as a "magic number" and assign its value to a
variable (i.e., before the "for" statement assign PowerIncrement =
100), or rearrange the conditional so that the P = P + PowerIncrement
line is only encountered once.

Or do both.

Certainly if you use the existing structure and if the two lines that
increment P are supposed to do the same thing always, then using a
named variable is a Good Thing -- that means that at some later date,
when some poor ignorant fool reads your code they don't have to wonder
if the "P+100" in one spot means the same thing as the "P+100" in
another, AND they don't run the risk of changing the (hopefully proper)
increment in one place but not another.

And note, based on my experience writing software for the last 40
years, that often the poor ignorant fool who has to maintain your
software is you, months or years down the road -- so it pays to make
your code readable.  Nothing makes you feel dumber than not being able
to understand your own code, because no matter which way the blame
flies, it always lands squarely on you.

On Fri, 2017-05-12 at 11:15 +0200, Frieder Nikolaisen wrote:
Hello, another question to solve the locomotive stuff, giving you a example with the not implented code.  I don't want to code the same stuff twice. Thats why I want to jump between lines of code. Is this possible and how?  P = 200; DM = 1; for n = 1:10     if DM == 1 then          if P > 100 then     DM = 1                             P = P - 60                             disp('P bigger 100. DM = ' + string(DM))         else    DM = 0                             P = P + 100 //instead of writing P = P + 100, I would like ...                             disp('P smaller 100. DM = ' + string(DM))         end      else // D == 0          if P > 100 then DM = 1                                                   disp('P bigger 100. DM = ' + string(DM))                         P = P - 30         else        DM = 0              // ... to continue here                                          disp('P smaller 100. DM = ' + string(DM))                     P = P + 100       // to use this P = P + 100         end     end      disp(string(n) + ' ' + string(P)) end   _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users

 

 

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Pierre Payen Pierre Payen
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Scilab-users] linking (jumping) from line of code to an other

In reply to this post by Frieder Nikolaisen
Is there some routines like goto ? I've never heard of it, so I doubt it.
The jumping (which can be partially achieved with break, abort, continue) has to be avoided , with the reason explained in the previous emails.

Regarding your code, if we follow the algorithm then no matter DM, if P < 100 then you set up DM to 0 and increase P.
So you could switch the if statement , making the P test in the first place, thus you only write your decreasing of P only once, as asked :

P = 200;
DM = 1;

for n = 1:10
    if P > 100 then
      if DM == 1 then
        P = P - 60
      else
        P = P - 30
      end
      DM = 1
      printf('P bigger  than 100. DM = %d\n',DM)
    else    
      DM = 0
      P = P + 100 //instead of writing P = P + 100, I would like ...
      printf('P smaller than 100. DM = %d\n',DM)
    end
  printf('%d %d\n',n,P) // changed disp for printf
end
Loading...