โŒšSB Watch 2
๐Ÿšš Free fedex shipping from 97 $
๐ŸŒŽ WORLDWIDE SHIPPING for 25 $
Search
Close this search box.

Control Flow

Introduction

Control flow refers to the order in which statements and instructions are executed in a program. MagiScript supports several control flow statements that allow you to control the flow of your program.

If/Else Statement

The if/else statement is used to perform different actions based on different conditions. The syntax is as follows:

				
					if (condition) {
  // code to be executed if the condition is true
} else {
  // code to be executed if the condition is false
}
				
			

Here, theย conditionย is evaluated, and if it is true, the code inside the first block is executed. If the condition is false, the code inside the second block is executed.

You can also use multipleย else ifย statements to test for additional conditions:

				
					if (condition1) {
  // code to be executed if condition1 is true
} else
if (condition2) {
  // code to be executed if condition2 is true
} else {
  // code to be executed if neither condition1 nor condition2 is true
}
				
			

While Loop

The while loop is used to execute a block of code repeatedly as long as a certain condition is true. The syntax is as follows:

				
					while (condition) {
  // code to be executed repeatedly while the condition is true
}
				
			

Here, theย conditionย is evaluated before each iteration of the loop, and if it is true, the code inside the block is executed. This continues until the condition becomes false.

do..while Loop

The do..while loop is similar to the while loop, but the condition is checked at the end of each iteration instead of the beginning. This means that the block of code is always executed at least once. The syntax is as follows:

				
					do {
  // code to be executed at least once, and
  // repeatedly as long as the condition is true
} while (condition);
				
			

For Loop

The for loop is used to execute a block of code a specific number of times. The syntax is as follows:

				
					for (initialization; condition; update) {
  // code to be executed repeatedly while the condition is true
}
				
			

Here the initialization is executed before the loop starts, the conditionย is checked before each iteration, and theย updateย is executed after each iteration. The loop continues until the condition becomes false.

Typically for loops are used to count from a number until a number, like this (will print the numbers from 1 to 10):

				
					for (let i = 1; i <= 10; i++) {
  console.log(i);
}
				
			

Conclusion

Control flow statements are essential for any programming language, and MagiScript provides the most used statements to help you control the flow of your program. By using if/else statements, while and do..while loops, and for loops, you can create complex programs that perform a variety of tasks.

General

๐Ÿ“ƒย First Steps

MagiScript Editor

๐Ÿ“ƒย Basics

๐Ÿ“ƒย Running Your First Program

๐Ÿ“ƒย App Store

๐Ÿ“ƒย Atom Settings (Editor)

๐Ÿ“ƒย Debugging Techniques

Examples

๐Ÿ“ƒย Atom Time

๐ŸŽž๏ธ Time Practice

๐Ÿ“ƒย Atom Pi (Pi Revelations)

๐Ÿ“ƒย Atom Drum

๐ŸŽž๏ธ Atom Stack

๐Ÿ“ƒย Atom Square

๐Ÿ“ƒย Atom Level

๐Ÿ“ƒย Atom THMPR

๐Ÿ“ƒย Poker Hands

๐Ÿ“ƒย Keyboard Numeric

๐Ÿ“ƒย Keyboard NOKIA

๐Ÿ“ƒย Keyboard Cursor

๐Ÿ“ƒย Keyboard Media

๐Ÿ“ƒย Keyboard Custom

Input/Output

๐Ÿ“ƒย Buttons

๐Ÿ“ƒย Vibration Motor

๐Ÿ“ƒย RGB LED

๐Ÿ“ƒย Devices

๐Ÿ“ƒย PeekSmith

๐Ÿ“ƒย SB Watch

๐Ÿ“ƒย Bluetooth Keyboard

๐Ÿ“ƒย Bluetooth Mouse

๐Ÿ“ƒย Timers

๐Ÿ“ƒย Database

๐Ÿ“ƒย Events

๐Ÿ“ƒย System (exit, sleep, rand)

๐Ÿ“ƒย Objects (card, time)

Language

๐Ÿ“ƒย Summary

๐Ÿ“ƒย Comments

๐Ÿ“ƒย Variable Declarations

๐Ÿ“ƒย Operators

๐Ÿ“ƒย Control Flow

๐Ÿ“ƒย Functions

๐Ÿ“ƒย Numbers

๐Ÿ“ƒย Strings

๐Ÿ“ƒย Arrays

๐Ÿ“ƒย Objects

๐Ÿ“ƒย Uint8Array