Quick Reference

For people who already know how to program with some other languages and do not want to go through the complete Small Basic tutorials, here is a short example that contains most language constructs and a short explanation of each. You can copy this program into Small Basic for testing. This example will not run directly on the EV3 brick, because it uses the Small Basic text window.

' Demo program explaining most of Small Basic   

' (all comments start with single quotation mark)  

' Execution starts here   

A = 5       ' variables are implicitly declared at first use    

a = a + 1   ' everything is case-insensitive   

B[a] = 55   ' arrays are implicitly defined and grow as needed   

X = "hi"    ' variables can also hold text    

Sub write   ' defines a sub-program with this name (no parameters possible)    

    TextWindow.WriteLine(X) 'call library function, access variable defined elsewhere   

EndSub      ' control flow of the main program runs around the Sub - definitions    

TextWindow.WriteLine("A="+a) ' string concatenation with +   

WRITE()     ' call subprogram. name is also case-insensitive   

write2()    ' may call subprogram that is defined further down the code    

TextWindow.Writeline("B[6]="+B[6])  ' access to arrays      

For i=2 to 5         ' a loop from 2 to 5 (inclusive)

    TextWindow.Writeline("I:"+I)   

EndFor   

For i=6.5 to 10.5 step 2      ' a loop with fractional values and larger step 

    TextWindow.Writeline("I:"+I)   

EndFor    

Sub write2

    write()          ' subprogram can call other subprograms

    write()   

EndSub ' control flow of the main program runs around the Sub - definitions     

I=99 ' case insensitive - overwrites previous i    

while i>3    ' loop with condition      

    i=i/2      

    TextWindow.Writeline("I:"+i)    

endwhile    

TextWindow.WriteLine("PI="+Math.PI) ' a library property (access without parentheses)    

TextWindow.WriteLine("SIN="+Math.Sin(0.5)) 'library function returning value     

A=50    

B=20    

If a<5 then ' an IF-construct with multiple conditions

    TextWindow.WriteLine("first")    

elseif a<10 and b<100 then          ' logic 'and' of conditions         

    TextWindow.WriteLine("second")    

elseif a<20 or (b>40 and b<60) then ' logic 'or' and nested conditions

    TextWindow.WriteLine("third")    

else

    TextWindow.WriteLine("other")    

endif