Introduction Welcome Getting Started My First Program     Troubleshooting and Distributing Transforming Data Interactive Programs     Programming Exercises     Making Games
Documentation Declarations     Procedures     Variables Statements     Flow Control Expressions Libraries     Print     IO     Random     Game Engine
Tutorials Complete Tutorials     Introduction to Programming     Quick Start     Programming Exercises     Programming Games

FLOW CONTROL


All About the Statements you can use to
Control The Flow of your Program in Vizzcode





About Flow Control

Like we've seen before statements are the building blocks you use to create your programs.
There are statements that allow you to control the flow of your program: they allow to change how the commands are executed in the program (for example, by executing them conditionally or repeatedly).

Diagram showing the execution flow of an example program





If Statement

One of the most powerful language constructs is the if statement.
They allow you to branch the execution of code based on some condition.
The condition can be any boolean.

For example, the following code only executes the print if the variable condition is true.
#import
"Print"


main
:: () {
    condition :=
5
<
7
;
    
if
condition {
        
print
(
"True!"
);
    }
}

How does an If work in Vizzcode

You can also chain an else after the if to execute code if the condition is not true:
#import
"Print"


main
:: () {
    condition :=
5
<
7
;
    
if
condition {
        
print
(
"True!"
);
    }
else
{
        
print
(
"Fase!"
);
    }
}

Using If and Else if in Vizzcode

Ifs can be tested sequentically, in such a way if the first condition is not true, it tests the second; if the second is not true, tests the third and so one:
#import
"Print"

#import
"Random"


main
:: () {
    var :=
random_int_in_range
(
0
,
3
);
    
if
var ==
0
  
print
(
"Zero!"
);
    
else
if
var ==
1
  
print
(
"One!"
);
    
else
if
var ==
2
  
print
(
"Two!"
);
    
else
if
var ==
3
  
print
(
"Three!"
);
}

Using several Else Ifs together in Vizzcode

Just like any other scope, you only need to include the { } in the if's block in text code if the block has more than one statement.
Similarly, the parethesis around the condition is optional.

For example:
#import
"Print"

#import
"Random"


main
:: () {
    var :=
random_int_in_range
(
0
,
3
);
    
if
var ==
0
  
print
(
"Zero!"
);
    
else
if
var ==
1
  
print
(
"One!"
);
    
else
if
var ==
2
  
print
(
"Two!"
);
    
else
if
var ==
3
  
print
(
"Three!"
);
}

Is the same as:
#import
"Print"

#import
"Random"


main
:: () {
    var :=
random_int_in_range
(
0
,
3
);
    
if
(var ==
0
) {
        
print
(
"Zero!"
);
    }
else
if
(var ==
1
) {
        
print
(
"One!"
);
    }
else
if
(var ==
2
) {
        
print
(
"Two!"
);
    }
else
if
(var ==
3
) {
        
print
(
"Three!"
);
    }
}

The are several examples of using if statements to create expressive programs here.




Loops

Loops are a powerful tool in creating programs.
They allow parts of the code to be repeated multiple times, until some condition is false or even indefinetely.

The most common way to loop through code is the while statement. It tests the condition and then run the body if the condition is true. Then it tests the condition again and so on. With whiles you can express any kind of loop in the program.

To facilitate the creation of loops that run a specific number of times, there is the for loop. In Vizzcode, the for loop simply iterates from the minimum value all the way to the maximum value (inclusively), running the loop body every iteration of the iterator variable.

There are also the useful break and continue statements to help you build more expressive loops.

In the Programming Exercises, you can follow a structured guide on how to use loops in your programs in a practical way, starting from simple examples all the way to more complex programs.

The following sections dive deeper into while and for loops and then break and continue.




While Loop

The while loop is a statement that executes a body of code repeatedly, for as long as a boolean condition is true.
They are similar to if statements in their construction:
#import
"Print"


main
:: () {
    count :=
0
;
    
while
count <=
10
{
        
print_int
(count);
        
print
(
"\n"
);
        count +=
1
;
    }
}

How do while loops work in Vizzcode

This is how the previous program will run:
  • Test the condition (0 <= 10). It's true, so it will execute the loop body.
  • Test the condition again (1 <= 10). It's true, so it will execute the loop body.
  • Test the condition again and so on, until the condition turns out to be false and it exits the loop.

This is the program output:
0
1
2
3
4
5
6
7
8
9
10

There could be break and continue statements that influence on the flow of the loop.

You can learn about how to apply the while loop in real programs here.




For Loop

For loops are a syntactically simpler way to execute a body of code a number of times.

For example, the following while loop:
main
:: () {
    count :=
0
;
    
while
(count <=
10
) {
        
print
(
"Hello!\n"
);
        count +=
1
;
    }
}

How to create a while loop in Vizzcode

Could be written using the for loop this way:
main
:: () {
    
for
0
..
10
{
        
print
(
"Hello!\n"
);
    }
}

How do For Loops work in Vizzcode

Just like the previous while example has a count variable (that helps iterating through the loop), the if declares a variable as well.
It's named it by default:
main
:: () {
    
for
0
..
10
{
        
print_int
(it);
        
print
(
"\n"
);
    }
}

Example program showing what is the it iterator variable in a For Loop

You can easly change the iterator variable's name like this:
main
:: () {
    
for
iterator:
0
..
10
{
        
print_int
(iterator);
        
print
(
"\n"
);
    }
}

How to change the it iterator variable in a For Loop

Note that the for loop will always increment the iterator by one, and execute the loop body while the iterator is greater than or equal to the maximum value, starting from the minimum value. This while example shows the mechanics of a for loop that goes from 10 to 20:

#import
"Print"


main
:: () {
    mininum_value :=
10
;
    maxinum_value :=
20
;
    
    it := mininum_value;
    
while
(it <= maximum_value) {
        
print_int
(it);
        
print
(
"\n"
);
        it +=
1
;
    }
}

Fors are specilly useful when iterating through arrays as shown in detail in these exercises.




Break and Continue

A Break Statement is used to exit the current loop.
In the following example, the print will never execute because the loop will break before getting to it:
#import
"Print"


main
:: () {
    
while
true
{
        
break
;
        
print
(
"Hello"
);
    }
}

How do Break Statements work in Vizzcode

It can be combined with if statements to exit the loop only on some conditions.
In this next exemple, the program will print "What would you like to do?" and read the response until the user enters "exit":
#import
"IO"


main
:: () {
    
while
true
{
        input :=
scan
(
"What would you like to do? "
);
        
if
input ==
"exit"
  
break
;
     }
    
print
(
"OK"
);
}

Combining a Break Statements with an If

A Continue Statement is used to skip the current iteration and move to the next iteration.

The following code will skip the print call when the iterator is 7 or 9.
#import
"Print"


main
:: () {
    
for
0
..
10
{
      
if
it ==
7
  
continue
;
      
if
it ==
9
  
continue
;
      
print_int
(it);
      
print
(
"\n"
);
    }
}

Controling the program's Execution Flow

Here is the outputs of the previous code:
0
1
2
3
4
5
6
8
10

The break and continue statements are useful tools when dealing with loops. You can see several examples of their use with explanations in this page.