How to create ‘if’ Statements (Conditional Scripting)

Overview

‘if’ Statements are a basic fundamental of programming languages.
It is used for creating condition checks.
conditional statements are features of a programming language which perform different computations or actions depending on whether a programmer-specified Booleancondition evaluates to true or false
This how to will introduce you to ‘if’ sentences and show you how you can use it within survey to go.

How it is constructed?

IF
(predicate)


THEN


(consequent)


ELSE


(alternative)

Within the ‘Predicate’ place you should write the condition.
Within the ‘Consequent’ you should write the code that will be executed if the condition is ‘true’.
Within the alternative you should write the code that will be executed if the condition is ‘false’.

How it really looks like?

if(Answer(2)==5)

{

    SetAnswer(CurrQues,2);

}

else

{

    Prompt(“This is a test”);

}

This example will do the following:
“if the answer of question 2 equals to 5, set the answer of the current question with the value 2.
Else, Prompt a message saying ‘This is a test’ “.

‘if’ sentences in SurveyToGo are useful within Start Scripts, End Scripts and ‘Advanced Scripts’.
They are not need for ‘Entrace Rules’ & ‘Jump Rules’.

Adding more conditions?

‘if’ statements are not limited to only one condition and alternative but many, although, it is not recommended to use more then 10 alternatives in the same ‘if’ statement, as some operating systems such as ‘Android’ have some limitation’s and might not process it right.
Example of several conditions:

if(Answer(1)==2 && Answer(2)==3)

{

    x=15;

}

else if(Answer(1)==3 && Answer(2)==4)

{

    Prompt(“This is a test”);

}

else if(Answer(1)==4 && Answer(2)==5)

{

    Prompt(“This is another test test”);

}

else

{

    Prompt(“Thats it”);

}

This will do the following:
if Answer of 1 is equal to 2 AND answer of 2 is equal to 3 -> input value 15 to x;

Else if : if Answer of 1 is equal to 3 AND answer of 2 is equal to 4 -> Prompt:”This is a test”

Else if : if Answer of 1 is equal to 4 AND answer of 2 is equal to 5 -> Prompt:”This is another test”

Else: -> Prompt:”This is another test”

Using an ‘if’ statement within an ‘if’ statement?

Similar to the movie Inception with the dreams, there can be an ‘if’ statement within an ‘if’ statementJ.

That’s it!

Was this article helpful?
0 out of 1 found this helpful
Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.