Introduction To Scala 2
Introduction To Scala 2
(Control Structure)
By Chin Teck Min
If .. else
if(Boolean_expression){
//Executes when the Boolean expression is true
}else{
//Executes when the Boolean expression is false
}
While loop(pre-test loop
while(condition){
statement(s);
}
do … while (post-test loop)
do {
statement(s);
} while(condition)
For-Comprehension
A for loop is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times.
The format of for loop is : for (seq)
seq is a semicolon-separated sequence of generators, definitions, and
filters, beginning with a generator.
A generator has the form variable <- list, where the list may be any
expression resulting in a list.
A definition has the form variable = expression (the keywords var and
val are not used here).
A filter has the form if condition.
for loop
for( var x <- Range ){
statement(s);
}