Statements
From FizzFuzz
Statements are used to perform functions, and programs are written using many statements. Below is a list of valid statements in FizzFuzz.
Contents |
if
Format
if(expression) ...
Arguments
- expression: The expression to be evaluated.
Usage
if statements are one of the back bones of programming, and are normally essential for any usable program to work. An if statement works by evaluating the included expression. If this expression is true, the included code will be executed. If not, then the code will be skipped over (and an else if statement will be evaluated, if it's there, or an else statement will have its code block run, if it's there).
Example
if((a + b) > 1) global.output((a + b) + " is greater than one .")
In this example, if a + b is greater than one, the output() code will be executed.
else if
Format
if(expression) ... else if(expression2) ...
Arguments
- expression: The expression to be evaluated.
- expression2: The expression to be evaluated if expression is false.
Usage
The else if statement is used to evaluate a statement an execute the code in the block provided a previous if or else if statement evalutes to false. Essentially, an else if statement is the same as a series of nested if statements.
//This is a series of else if statements. if(a) ... else if(b) ... else if(c) ... else ... //The previous code is essentially equal to this. if(a) ... else if(b) ... else if(c) ... else ...
It's easy to see the advantages of the else if statement. Given a long enough block of code, the nested format could get unmanageable.
Example
if((a + b) > 10) global.output((a + b) + " is greater than 10.") else if((a + b) < 5) global.output((a + b) + " is less than 5.") else global.output((a + b) + " is between (or equal to) 5 and 10.")
else
Format
if(expression) ... else ...
Arguments
- expression: An expression to be evaluated as true or false.
Usage
The else statement is used as a sort of default if the previous if and all previous else if statements are evaluated to be false.
Example
if((a + b) > 10) global.output((a + b) + " is greater than 10.") else if((a + b) < 5) global.output((a + b) + " is less than 5.") else global.output((a + b) + " is between (or equal to) 5 and 10.")
switch
Format
switch(variable) if(literal) ... if(literal2, literal3) ... ... if(literal3 to literal4) ... else ...
Arguments
- variable: A variable to be checked.
- literal, literal2, ...: Literals to be checked against the variable.
Usage
The switch statement is used similiairly to an if-series, though expressions are invalid. The literal arguments check the variable directly against a literal. The FizzFuzz switch statement has some special functionality for its internal if statements:
- These if statements may only contain literals. Varibles and expressions are not allowed.
- Multiple literals can be listed by seperating the literals with commas. For example if(1, 2, 3, ...).
- Using the to operator, which is used on numeric data types, counts all numbers in between. For example, if the statement if(1 to 10) were used, all numbers betwen 1 and 10 would be valid (decimals included).
Example
string get_age_range() switch(age) if(0, 1) return "infant" if(2, 3) return "toddler" if(4 to 12) return "child" if(13 to 18) return "adolescent" if(19 to 59) return "adult" else return "senior"
return
Format
return value
Arguments
- value: The value to return to the previous value in the call stack.
Usage
return is used to halt the current function and continue with the function previously on the call stack. Optionally, a value can be sent back to the previous function. If none is explicitly stated, null will be returned.
Example
boolean iseven(int n) return !(n % 2)
goto
Format
goto LABEL
Arguments
- LABEL: A point in the code to jump to.
Usage
goto is used to jump directly to a previous point in the current function. Developers should tend to stay away from this and to with traditional loops to make structured code.
Example
global.output("Hello.")
goto LABEL
global.output("This won't be reached.")
LABEL
while
Format
while(expression) ...
Arguments
- expression: An expression to be evaluated upon each iteration.
Usage
The while loop is a statement to cause a loop, which means that it will continue to perform the included code until the expression is false, upon which time it terminates. A while loop is a pre-checking loop, meaning that it evalutes the expression before each iteration of the loop.
Example
int a = 0 while(a < 3) a ++
do-while
Format
do ... while(expression)
Arguments
- expression: An expression to be evaluated upon each iteration.
Usage
The do-while loop is a post-checking loop, meaning that it will iterate at least once, and it will check the expression as true or false after the iteration. If it is true, it will continue to iterate, and if it is false, it will continue with the rest of the code.
Example
int a = 0 do a ++ while(a < 3)
for
Format
for(var; expression; modification) ...
Arguments
- var: A variable to be initialized, or a previously defined variable.
- expression: Expression to be evaluated.
- modification: Modification to be made for the variable.
Usage
The for loop is used when the specific amount of iterations is known, as opposed to a while or do while loop, where it is the condition that is known.
Example
for(int a = 1; a <= 10; a ++)
global.output("The current value of a is " + a + ".")
break
Usage
The break statement is used to stop the loop where it is and move on with the code. It could be considered the "return" analog of a loop, though it doesn't send back any data.
Example
for(int a = 1; a <= 10; a ++) global.output(a) if(!(a % 5)) break
continue
Usage
The continue statement is used to stop the current iteration of the loop and continue with the next one.
Example
for(int a = 1; a <= 10; a ++) if(!(a % 5)) continue global.output(a)

