You can use check_for(), check_while() and check_if_else() chains to check whether a student has properly coded control constructs.

Example 1: For loop

Consider the following solution:

A suitable SCT for this exercise can be the following:

  • check_for() checks whether students have specified a for loop in their code. If this is the case, it produces a substate, referring to the condition of the for loop and its body.
  • Next, you can ‘zoom in’ on the condition and body of the for loop, using check_cond() and check_body() respectively. After this zooming in, you can continue the chain of SCT functions to check these ‘parts’ of the code. The check_code() call, for example, only looks to match the regular expression in the iteration part of the for loop.

Example 2: Check if else

Consider the following solution:

The following SCT checks its correctness:

  • check_if_else() parses the student code and checks if there is an if-else statement in there.
  • check_cond, check_if, and check_else() all ‘zoom in’ on a particular part of the if-else statement: the condition, the body of the if statement and the body of the else statement. After this zooming in, you can continue the chain of SCT functions to check these ‘parts’ of the code. The first check_function("print") chain for example, only looks for the function call inside the if body.

Example 3: Check if, else if, else

It is also possible to use else if in R. How to test this? Well, behind the scenes, R parses the following structure:

as if it follows this structure:

If you want to test such a piece of code, you therefore need the following construct:

Caution

Not all of the SCT chains that you specify after a ‘zooming in’ step might work as you’d expect. Remember that functions like check_object() and check_function() often depend on both the student and solution environment. It is possible that during execution of control flow these values change. The testwhat functions will always compare the ‘end environment’ of student and solution, it is not possible to do matching on intermediate values of objects that are changed further in the script.