formulas question type with if and for statments

formulas question type with if and for statments

by Jorge Faria -
Number of replies: 6

Is possible to use formulas question with if and for statements if so anyone can provide me a simple example of the syntax


Basically I need to get a variable and based on the value choose the action to perform.

Average of ratings: -
In reply to Jorge Faria

Re: formulas question type with if and for statments

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Hello Jorge,

The "if" construct is not incorporated into the Formulas question, but you can use the "conditional (ternary) operator". You will find an example here ↗.

You will find here ↗ the syntax of the "for" loop as well as two examples in the last table at the bottom of the page.

In reply to Dominique Bauer

Re: formulas question type with if and for statments

by Jorge Faria -
Thanks Dominique. But looking for the possibilities it seem to be impossible to do something like this or i am wrong?

Random variable
Arti={"Hip", "Knee"};

if Arti == 0
X [1,2,3,4,5];

if Arti == 1
X [10,20,30,40,50];
In reply to Jorge Faria

Re: formulas question type with if and for statments

by Wendy Harper -
the conditional operator can be used, however, it requires a numerical value.
x = (a==0)?value of true:value if false;
For example:
a=1
b=2
x=(a>b}?3:4;
would result in x =4

I have wanted to use a condition to set a new variable equal to a string.
I do hope this feature will be added in the future.
In reply to Wendy Harper

Re: formulas question type with if and for statments

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Wendy,

To set a new variable equal to a string, just add a line as follows:

    a=1;
    b=2;
    x=(a>b)?0:1;
    newvariable=pick(x,"String 1","String 2");
In reply to Dominique Bauer

Re: formulas question type with if and for statments

by Wendy Harper -
Thank you!! This will help.
In reply to Jorge Faria

Re: formulas question type with if and for statments

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Jorge,

On the contrary, it is very easy to do.

Define a random variable equal to 0 or 1:

    a = {0,1};

Define global variables as follows:

    Arti = pick(a,"Hip","Knee");
   
    A = [1,2,3,4,5];
    B = [10,20,30,40,50];
    X = pick(a,A,B);