Skip to content
Classroom

Conditional Questions

"Conditional" questions are not a distinct answer block type in IMathAS — there is no [conditional] tag. Instead, conditional refers to a pattern: use if/else logic in the question code (Common Control or Question Control) to vary what is shown or graded, while the answer block itself remains one of the standard types ([number], [multchoice], [calculated], etc.).

This page shows common conditional patterns.

Basic Syntax

Conditional logic lives in normal PHP-like code blocks:

if ($studentAnswer > 10) {
    feedback = "Too high!";
}

Do not use @if, @else, or @endif — those are Blade directives from Laravel templating and are not recognized by IMathAS.

Varying Question Display

Use if/else in the Common Control section to pick which prompt to show.

$level = randfrom(array("easy", "medium", "hard"));

if ($level == "easy") {
    $prompt = "Solve: x + 5 = 12";
    $ans = 7;
} elseif ($level == "medium") {
    $prompt = "Solve: 2x - 3 = 15";
    $ans = 9;
} else {
    $prompt = "Solve: 3x^2 - 11x + 6 = 0";
    $ans = 3;  // one of the roots
}

Then reference $prompt in the question text and $ans in the answer block:

`$prompt`

[number]
answer = $ans;

Feedback Based on Answer

$correctAnswer = 25;

[number]
answer = $correctAnswer;
tolerance = 0;

if (abs($studentAnswer - $correctAnswer) <= 5) {
    feedback = "Close! Check your calculation.";
} elseif ($studentAnswer > $correctAnswer) {
    feedback = "Your answer is too high. Re-examine step 2.";
} else {
    feedback = "Your answer is too low. Review the formula.";
}

Conditional Hints

$answer = 25;

[number]
answer = $answer;

if ($attempts == 0) {
    hint = "Try multiplying 5 x 5.";
} elseif ($attempts == 1) {
    hint = "The answer is a perfect square.";
} elseif ($attempts >= 2) {
    hint = "The answer is 25.";
}

Multi-Part with Conditional Branches

For branching across parts of a question, use [multipart] and set variables in earlier parts that later parts read.

[multipart]

[[part1]]
Pick a difficulty level:

[multchoice]
a) Easy
b) Medium
c) Hard
answer = a;

[[part2]]
// In Question Control for this part:
$level = $answers['part1'];

if ($level == 'a') {
    $prompt = "Solve: x^2 - 4 = 0";
    $ans = 2;
} elseif ($level == 'b') {
    $prompt = "Solve: x^2 - 5x + 4 = 0";
    $ans = 1;
} else {
    $prompt = "Solve: 2x^2 - 11x + 5 = 0";
    $ans = 0.5;
}

`$prompt`

[number]
answer = $ans;

Feedback on Common Misconceptions

$correctAnswer = 12;

[number]
answer = $correctAnswer;
tolerance = 0;

$commonMistakes = array(
    6  => "Did you forget to multiply? 2 x 6 = 12.",
    10 => "Close! But remember the order of operations.",
    14 => "You may have added instead of multiplied.",
    24 => "You may have calculated 2 x 12. Try 2 x 6.",
);

if (isset($commonMistakes[$studentAnswer])) {
    feedback = $commonMistakes[$studentAnswer];
} else {
    feedback = "Not quite. Review the calculation: 2 x 6 = ?";
}

Best Practices

  • Keep conditions simple: Complex nested conditions become hard to maintain
  • Test edge cases: Verify all branches work correctly
  • Provide clear alternative paths: Students should understand why they got a different question
  • Use for remediation: Offer easier questions if students struggle
  • Avoid overuse: Simpler questions are often better

Conditional Syntax Notes

  • Use == for equality checks: if ($answer == 5)
  • Use != for inequality: if ($answer != 0)
  • Use >, <, >=, <= for comparisons
  • Use && for AND: if ($a > 0 && $b < 10)
  • Use || for OR: if ($a == 1 || $a == 2)
  • No Blade directives: @if/@endif will not work

See Also