Back to Tutorials Index... ## If - Then Statements The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular condition evaluates to true and do something else when the condition is false. We already covered the basic use of conditional edges in a previous tutorial. In this guide we will show an example of a common if-then clause. First, start by creating a new project in SceneMaker and drag two normal nodes to the workspace. Then create a local int variable by right-clicking on the workspace and selecting the **"Add Variable"** from the drop-down menu. ![Alt text](../../images/tutorials/newVar.png) Then connect the start node to the second node through a conditional edge, the condition will be (x>2). Remember to wrap the condition in parenthesis! Then connect the start node to itself through a timeout node. Leave the default timeout as it is. Finally, add a command execution to the start node by double clicking on it and selecting **"Add command execution"** from the dropdown menu. The command will be *x = x + 1* ![Alt text](../../images/tutorials/ifthen1.png) Congratulations, now we have a basic if-then clause equivalent to ``` if(x>2) { Go to N2 } else { Stay in N1 } ``` Another straight foward example is to create an If-Else statement without time condition. In this case, instead of using a *timeout edge* ![Alt text](../../images/tutorials/if_else2.png) Now in this case we have another basic if-then clause equivalent to: ``` if(x>2) { Go to N2 } else { Go to *Else Node* and doSomething(); } ``` Great! But what about builing more complex conditions, for example, and if-then-elseif-then ![Alt text](../../images/tutorials/IfElseIf.png) The equivalent in this case will be: ``` if(x>2) { Go to N2 } else if(x == 0) { Go to *N5* and doSomething(); } else{ Go to "Else" } ```
Back to Tutorials Index...