[ Prev | Index | Next ]
Japanese English

Control Structures

In this section, we will cover control structures, which are methods for executing conditional branching and loop processes.

Sponsored Link


Control Structures

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:

syntax_keyword ( condition_expression, etc. ) {
    statements;
}

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.

"if" Statement

Let's now use one of the control structures, the if statement. Try writing and executing the following:

- Execution Result -

Hello

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.

"else" Statement and Combination with "if" Statement

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 -

1 <= f

Immediately following an "else" keyword, you can connect another if statement to add a condition, as often used in the following example:

- Execution Result -

f < 3

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.

"while" Statement

Next, let's try using the while statement. Write and execute the following code:

- Execution Result -

0
1
2
3
4
5
6
7
8
9
10

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.

"for" Statement

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 -

0
1
2
3
4
5
6
7
8
9
10

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:

  • Initialization: int i = 0
  • Condition: i <= 10
  • Update: i = i + 1

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.

Combining Different Control Structures

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 -

0
2
4
6
8

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.

Acknowledgement: We greatly appreciate the cooperation of two ChatGPT AIs in translating this page.
» How we translated this page


Sponsored Link



Japanese English
Index
News

Software Updates: Command Expansion in RINEARN Graph, and English Support in VCSSL
2024/02/05 - We updated our apps. This updates include "Enhancing the Command-Line Features of RINEARN Graph" and "Adding English Support to VCSSL." Delves into each of them!

Inside the Repetitive Execution Speedup Impremented in Vnano Ver.1.1
2024/01/17 - Delves into the update in Vnano 1.1 from a developer's viewpoint, providing detailed insights into the specific improvements made to the internal structure of the script engine.

Scripting Engine Vnano Ver.1.1 Released: Dramatic Speed Improvement for Repetitive Executions of the Same Content
2023/12/22 - Released the Vnano script engine Ver.1.1. In this version, we've made significant enhancements in processing speed by reducing the overhead of handling requests. Explains the details.

Updated Contents
Circular Wave Animation

Draws the circular wave as 3D animation, under the specified wave parameters.
2022-12-14
Sine Wave Animation

Draws the sine wave as animation, under the specified wave parameters.
2022-11-26
Tool For Converting Units of Angles: Degrees and Radians

A GUI tool for converting the angle in degrees into radians, or radians into degrees.
2022-11-22
Connector Fatal Exception - Specification
The unchecked exception thrown when errors have occurred, caused by incorrect implementations (might be bugs).
2022-09-26
Connector Exception - Specification
The checked exception thrown when errors have occurred, cause by expected normal problems.
2022-09-26