Conditional and Iterative Statements

Conditionals

Equivalent to the YesNo command in the block editor.

General form of an 'if' statement:

if (<condition>)
{

<command-list>
// commands in here executed only if <condition> is true

}

Example:

if (SENSOR_2>50)
{

// tell both engines to go backward
OnRev(OUT_A)
OnRev(OUT_C)

}

Operators

Equals and not equal-- ==, !=

less/greater than-- <,>,>=,<=

and/or-- &&, ||

Boolean Logic

true is 1, false is 0

When will the assignment statement to 'greens' be executed in the following:

if (true)
{

greens=greens+1;

}

if (true && false)
{

greens=greens+1;

}

if (true || false)
{

greens=greens+1;

}

Suppose we're counting lines and greens is 5 and blacks is 3. Which conditions are true:

if (greens==5 || blacks>3)

if (greens!= 5 || blacks>=3)

if (greens >=blacks && blacks>= greens || blacks==3)

if (1 && 0)

Iteration

General Form:

while (<condition>)
{

<command-list>

}

Like an 'if' statement, but after <command-list> is executed, the program checks the condition again and executes <command-list> if true. This 'loop' continues until <condition> becomes false.

Examples:

while (greens>0)
{

PlaySound (SOUND_LOW_BEEP);
greens=greens-1

}

while (true) // loop forever
{

PlaySound (SOUND_LOW_BEEP);

}

while (SENSOR_2>50) // while in white, do nothing
{}