This structure is called a “ternary” operator ( ternary = comprised of 3 parts), and it is a way of having an if-else statement all in one line. It turns out that these statements are actually pretty cool. If you’re like me in the early stages of learning RobotC, at this point your eyes glaze over and you think something like, “This is too complicated, I’ll look for another answer somewhere else.”
Robotc if else code#
We should instead use if-then-else statements as follows:Įlse if gpa>= 3.0 then gpagroup = "Good" Įlse if gpa >= 2.If you’re new to RobotC and searching for sample code or explanations, you will frequently run into statements that look very confusing, involving a question-mark (?) and a colon (:), such as: motor = abs ( vexRT ) > 10 ? vexRT : 0 If gpa >= 2.5 then gpagroup = "Satisfactory" If gpa>= 3.5 then gpagroup = "Excellent Grades"
![robotc if else robotc if else](https://s3.studylib.net/store/data/008748620_1-38e752508ffc5a7e251405ecce5b5eef-768x994.png)
The exception is in the value of character variables. Note that SAS does not generally distinguish between upper and lower case (you can use either). If gpa= 3.0 then group = "good standing" The new variable called group takes on one of two values: "good standing" if a person's gpa is greater than or equal to 3.0 and "not good standing" if a person's gpa is less than 3.0. The following code creates a new variable called group from an existing variable called gpa. SAS will keep evaluating the if-then-else-if statements until it encounters the first true statement.Ĭharacter variable data must always be enclosed in quotes. Note that this if-then-else-if statement could equivalently be writtenĪn if statement can be followed by exactly one else statement or by many else-if statements.
![robotc if else robotc if else](https://i.ytimg.com/vi/FXNY-W_66bA/maxresdefault.jpg)
![robotc if else robotc if else](https://i.ytimg.com/vi/q1xhzd9G2C0/maxresdefault.jpg)
![robotc if else robotc if else](http://www.mrjopp.com/uploads/5/2/5/3/5253521/448210_orig.jpg)
else-if statements are useful when forming mutually exclusive groups. SAS evaluates the expression in the else-if statement only when the previous expression is false. Note that the above statement could equivalently be writtenĪn optional else statement can be included (if-then-else) to provide an alternative action when the if expression is false.įor a person whose age is less than 65, the variable older will equal 0.Īn optional else-if statement can follow the if-then statement. For a person whose age is less than 65, the variable older will be missing. When the expression is false, SAS ignores the statement following then. When the expression is true, the statement following then is executed. Creating New Variables Using if-then if-then-else and if-then-else-then StatementsĪn if-then statement can be used to create a new variable for a selected subset of the observations.įor each observation in the data set, SAS evaluates the expression following the if.