Autore Topic: Controllo logico di interruzione quando si arriva ad un'istruzione vera  (Letto 326 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.718
  • Ne mors quidem nos iunget
    • Mostra profilo
Vi riporto questa discussione apparsa nella M.L. ufficiale:


" Hi!

Though I have limited knowledge of C++, I do believe it has the following
functionallity that also could be useful in Gambas:

If you have an IF-block with several statements that are to be evaluated,
in C++ it will jump out of that evalutaion proccess as soon as it
encounters a true statement. That way you can put procedure-calls from the
IF-block that would normally crash the program if the prior checks are
TRUE, but otherwise would run successfully.

Just a silly example

Codice: [Seleziona]
If(itsWinterTime OR iceCreamWagonIsOpen) Then 'There are no Ice Wagons
around in Winter, and the program will wonder what you are talking about
and go crash sending out an Error message, but if it is summer time, there
will be a ice wagon, but it is not neccessarily open :)
   ...
End if

derox "


" Well... I think you mean: jumps out of a chain of ORs when a TRUE statement
is reached and jumps out of a chain of ANDs when a FALSE statement is
reached?

This is called short-circuit evaluation and Gambas has it. Look at... the
documentation of If:
http://gambasdoc.org/help/lang/if?v3

Regards,
Tobi
"


" Hehe, yes - this is what I meant, but I struggeled to find the right terms
that you so well presented here. Life get's easier when you know the right
words :p

I though I found my code running after a TRUE statement was passed...
that's why I made the suggestion, but I must have missed something :)

Great to know that this exists already!

derox
"


" Yeah, Gambas uses a slightly different syntax:
Codice: gambas [Seleziona]

If bExpr And bExpr2 Then
  ' ...
Endif


is normal non-short circuit. But if you use the And If operator:
Codice: gambas [Seleziona]

If bExpr And If bExpr2 Then
  ' ...
Endif


then you get what you want.

Regards,
Tobi
"


" Wow, OK! Then I get it!

derox
"
« Ultima modifica: 22 Novembre 2013, 23:21:39 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »