Advanced Programming Concepts

This section explains more advanced programming concepts like loops, conditionals, variables, and functions.

Loops

Loops allow you to repeat a set of instructions multiple times. KodeVR provides the following loop blocks:

  • Repeat: Repeats a set of instructions a specified number of times.
  • Repeat Forever: Repeats a set of instructions indefinitely.
  • Repeat Until: Repeats a set of instructions until a condition is met.
// Example of a Repeat loop
repeat 4 times {
    moveForward(100)
    turnRight(90)
}

// This creates a square path

Conditionals

Conditionals allow you to execute different instructions based on certain conditions. KodeVR provides the following conditional blocks:

  • If Then: Executes a set of instructions if a condition is true.
  • If Then Else: Executes one set of instructions if a condition is true, and another set of instructions if the condition is false.
// Example of an If-Then-Else conditional
if (getDistance() < 50) {
    // If an obstacle is detected less than 50 units away
    turnRight(90)
} else {
    // If no obstacle is detected
    moveForward(100)
}

Variables

Variables allow you to store and manipulate data in your program. KodeVR provides the following variable blocks:

  • Set Variable: Sets the value of a variable.
  • Get Variable: Retrieves the value of a variable.
  • Change Variable: Changes the value of a variable by a specified amount.
// Example of using variables
set distance to getDistance()
set heading to getHeading()

if (distance < 50) {
    turnRight(90)
} else {
    moveForward(distance / 2)
}

Functions

Functions allow you to create reusable blocks of code. KodeVR provides the following function blocks:

  • Define Block: Creates a new function.
  • Call Block: Calls a function.
// Example of defining and calling a function
define makeSquare(size) {
    repeat 4 times {
        moveForward(size)
        turnRight(90)
    }
}

// Call the function
makeSquare(100)
makeSquare(50)