0% found this document useful (0 votes)
54 views14 pages

Introduction To Scala 2

This document discusses various control structures in Scala including if/else statements, while loops, do/while loops, and for loops. It also covers Scala arrays, explaining how to declare and initialize array variables, access elements using indexes, and create multi-dimensional arrays. Methods like range are described for generating arrays of integers over a specified range.

Uploaded by

Avan Tennant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views14 pages

Introduction To Scala 2

This document discusses various control structures in Scala including if/else statements, while loops, do/while loops, and for loops. It also covers Scala arrays, explaining how to declare and initialize array variables, access elements using indexes, and create multi-dimensional arrays. Methods like range are described for generating arrays of integers over a specified range.

Uploaded by

Avan Tennant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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);
}

 Here, the Range could be a range of numbers and that is


represented as i to j or sometime like i until j. Range is a type
of collection as list. The left-arrow <- operator is called a
generator, so named because it's generating individual values
from a range.
Scala Array
 Scala provides a data structure, the array, which stores a
fixed-size sequential collection of elements of the same type.
An array is used to store a collection of data, but it is often
more useful to think of an array as a collection of variables
of the same type.
 Instead of declaring individual variables, such as number0,
number1, ..., and number99, you declare one array variable
such as numbers and use numbers[0], numbers[1], and ...,
numbers[99] to represent individual variables
Scala Array
 This tutorial introduces how to declare array variables, create
arrays, and process arrays using indexed variables. The index
of the first element of an array is the number zero and the
index of the last element is the total number of elements
minus one.
Declaring Array Variables
 var z:Array[String] = new Array[String](3)
 Here, z is declared as an array of Strings that may hold up to
three elements. You can assign values to individual elements
or get access to individual elements, it can be done by using
commands like the following:
 z(0) = "Zara"; z(1) = "Nuha"; z(4/2) = "Ayan”
 Or
 var z = Array("Zara", "Nuha", "Ayan”)
Processing Arrays
Multi-Dimensional Arrays
 var myMatrix = ofDim[Int](3,3)

 def ofDim[T]( n1: Int, n2: Int ): Array[Array[T]]


 Creates a 2-dimensional array
Multi-Dimensional Arrays
Create Array with Range
 def range( start: Int, end: Int, step: Int ): Array[Int]
 Returns an array containing equally spaced values in some
integer interval.
 def range( start: Int, end: Int ): Array[Int]
 Returns an array containing a sequence of increasing integers
in a range.
Create Array with Range

You might also like