In this section, we will cover control structures, which are methods for executing conditional branching and loop processes.
To repeatedly execute a specific part of the program a certain number of times, or to execute it only when a certain condition is met, control structures are used.
Control structures in VCSSL are always paired with blocks and are written using the following syntax:
The following is the list of the control structures supported in VCSSL:
Keyword | Content in Parentheses | Details |
---|---|---|
if | Condition expression | Executes the contents in the block only if the condition expression evaluates to true. |
else | None (no parentheses) | Executes the contents in the block only if the value of the condition expression of the preceding if statement is false. |
else if | Condition expression | An extension of the else statement with an if statement attached, adding conditions to be executed. Therefore, strictly speaking, "else if" is not a single syntax, but it is easier to understand as such a syntax (a variant of else). |
while | Condition expression | Executes the block's contents repeatedly as long as the value of the condition expression true. |
for | Separated by semicolons ";", describe initialization statement, condition expression, and update statement | Similar to while loops, but with a counter variable for repeating the process a certain number of times. Initially, the initialization statement is processed, then as long as the condition expression evaluates to true, the contents in the block are repeatedly executed. During this, the update statement is processed each time. |
Let's now use one of the control structures, the if statement. Try writing and executing the following:
- Execution Result -
When you execute this, "Hello" will be displayed. This means that the print function inside the if block is executed. What happens if we rewrite it like this:
- Execution Result -
This time, it will exit without displaying anything. In other words, the if statement only executes the block's contents if the condition expression evaluates to true. In this case, 1 > 2, evaluates to false, hence nothing is displayed.
Additionally, it's possible to include a boolean variable instead of the condition expression. In that case, the block's contents will only be executed if the boolean variable's value is true.
The else statement is used after an if statement to execute code when the condition of the preceding if statement evaluates to false. Let's see an example:
- Execution Result -
Immediately following an "else" keyword, you can connect another if statement to add a condition, as often used in the following example:
- Execution Result -
Executing this will display "f < 3". When if and multiple else-if are combined as the above, conditions are evaluated sequentially from top to bottom. Once a condition is met, the block associated with it is executed and subsequent conditions are not evaluated or executed.
Also, if an else statement is attached at the end, the block associated with it will only be executed if none of the preceding if statements or else-if conditions are met.
Next, let's try using the while statement. Write and execute the following code:
- Execution Result -
Executing this will output numbers from 0 to 10.
As shown above, the contents of the block are executed repeatedly as long as the condition (in this case, i <= 10) remains true.
The process repeating something a certain number of times, as described in the while statement above, is very common in programming. However, the previous code is a bit cumbersome for such situation. To write this more concisely, the for statement is provided.
For example, let's rewrite the same process described with the while statement using the for statement:
- Execution Result -
As shown, you can write repetitive processes for certain number of times succinctly with the for statement.
Inside the parentheses of the for statement, three statements/expressions are separated by semicolons: an initialization statement, a condition expression, and an update statement.
In the example above:
For the for statement, the initialization statement is executed first, followed by the repeated execution of the block's contents as long as the condition expression is true.
Each time the block's contents are executed, the update statement is executed.
It's common to declare a variable to count the loops in the initialization statement. Such variables are generally called "loop counters". Variables declared in the initialization statement become local variables with their scope limited to the block.
For clarity, "i = i + 1" is used here for the update statement, but "i += 1" or "i++" (explained later) can also be used, which are generally more concise and preferred.
Multiple different control structures are often combined to implement complex logic. For instance, the if statement is frequently used within loops.
Let's write a program to output even numbers as an example:
- Execution Result -
In the above example, only even numbers are printed, as indicated by the condition (i%2)==0, which checks if the remainder of i divided by 2 is 0.
In the above example, we use "i++" which is a shorthand for "i = i + 1". This notation is commonly used in programming to increment the value of a variable by one.