News:

Function Finder  Find all the functions within source code files

Main Menu

PlayBASIC Tutorial: Into to IF / Then and IF / ENDIF statements ( 2017/03/21)

Started by kevin, March 21, 2017, 02:05:25 AM

Previous topic - Next topic

kevin

PlayBASIC Tutorial:  Into to IF  / Then  and IF / ENDIF statements ( 2017/03/21)

This #tutorial walks the new coder through the IF / THEN and IF / ENDIF statement pairs which are used to selectively execute sections of the code, based upon the result of the comparison operators. The IF statements actually change control, so they act upon the comparison, but it's just easier to think of them as part of the comparisons landscape when learning programming.


Also read: PlayBASIC Help Tutorial - Comparisons - Making Decisions with IF Statements
https://playbasic.com/help.php?page=ABOUT.COMPARISONS

https://playbasic.com
https://underwaredesign.com

#coding #learntocode #programming #basic #makegames #howto #programmer #code



PlayBASIC Code: [Select]
   Setfps 20

Do
Cls rgb(20,30,40)

;
Mx = MouseX()
My = MouseY()

Circle Mx,My,40,true

print Mx
print My


box 500,300,600,400,false

if (Mx > 500) and (Mx < 600) and My >300 and My<400
print "Mouse is within zone"
endif

Sync
loop




stevmjon

this reminded me about optimising.
why is it better to have the compares on their own line, rather than all on a single line?

it was interesting to see how playbasic treats code, and where it jumps to next upon certain conditions. i like those sort of explinations.

  stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

Quote from: stevmjon on March 21, 2017, 08:34:52 PM
this reminded me about optimising.
why is it better to have the compares on their own line, rather than all on a single line?

 opt'ing anything is all about situation, so the data that's being processed will determine the layout of the routine...

 you can stack loads of compares into an expression,  but then the expression has a fixed execution cost.   So everything in the expression is being solved, so it's boiling it all down to a single answer.    if you can do a simple test and reject something up front, then that lowers the cost of a fail but raises the cost of a success.   So if situtation means the code will fail 99% of time say like when running through a big chunk of data, then in terms of mirco opt's then its quicker to split stuff than stack all the compares into an expression..

 In terms of the opt'ing for the compiler, the stuff mentioned in opt'd tutoriak  thing written for PlayBASIC users, but it's actually general programming advice..

  A Crash Course In BASIC program Optimization

 There's a great quote from computer science teacher back in the 80's, along the lines of "The fastest routine in your program is the one that's never executed"