0% found this document useful (0 votes)
50 views

Two Ranges Nested Loop

The document contains code examples demonstrating various Scala features including nested loops, for expressions, filtering, match expressions, functions, and object nesting. It shows how to iterate over ranges and lists, filter lists, use match expressions to compare values, define functions to perform math operations, and nest objects to organize functions.

Uploaded by

Blinx Guy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Two Ranges Nested Loop

The document contains code examples demonstrating various Scala features including nested loops, for expressions, filtering, match expressions, functions, and object nesting. It shows how to iterate over ranges and lists, filter lists, use match expressions to compare values, define functions to perform math operations, and nest objects to organize functions.

Uploaded by

Blinx Guy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Two ranges nested loop

object demo
{
def main(args: Array[String]) //
{

for (i <-1 to (5))


{
println("i using to " +i);

for (j <-1 until (5))


{
println("j using until " +j);
}

for( i <- 1 to 5; k<-1 to 3 )


{
println("i using multiple range" +i +k);
}
}
}

List
object demo
{
def main(args: Array[String]) //
{

for (i <-1 to (5))


{
println("i using to " +i);

for (j <-1 until (5))


{
println("j using until " +j);
}

for( i <- 1 to 5; k<-1 to 3 )


{
println("i using multiple range" +i +k);
}

val lst1=List(1,2,3,4,5,6,7,8,9);
for(i<-lst1)
{
println("i using lst" +i);

}
//filtering
for(i<-lst1)
{
println("i using lst" +i);

Filtering

object demo
{
def main(args: Array[String]) //
{

for (i <-1 to (5))


{
println("i using to " +i);

for (j <-1 until (5))


{
println("j using until " +j);
}

for( i <- 1 to 5; k<-1 to 3 )


{
println("i using multiple range" +i +k);
}

val lst1=List(1,2,3,4,5,6,7,8,9);
for(i<-lst1)
{
println("i using lst" +i);

//filtering
for(i<-lst1; if i<6 )
{
println("i using filter " +i);

}
}
object demo
{
def main(args: Array[String]) //
{

for (i <-1 to (5))


{
println("i using to " +i);

for (j <-1 until (5))


{
println("j using until " +j);
}

for( i <- 1 to 5; k<-1 to 3 )


{
println("i using multiple range" +i +k);
}

val lst1=List(1,2,3,4,5,6,7,8,9);
for(i<-lst1)
{
println("i using lst" +i);

//filtering
for(i<-lst1; if i<6 )
{
println("i using filter " +i);

// for as expression

val result= for{i<-lst1; if i<6 }


yield
{
i*1
}

println("result=" +result);

}
}
// match expression
object demo
{
def main(args: Array[String]) //
{

val age=18;
age match
{
case 20 => println(age);
case 25 => println(age);
case 18 => println(age);
case 30 => println(age);
case 40 => println(age);
}

}
}

// match expression
object demo
{
def main(args: Array[String]) //
{

val age=18;
age match
{
case 20 => println(age);
case 25 => println(age);
case 18 => println(age);
case 30 => println(age);
case 40 => println(age);
case _ => println("default");
}

}
}

// match expression
object demo
{
def main(args: Array[String]) //
{

val age=18;
age match
{
case 20 => println(age);
case 25 => println(age);
case 18 => println(age);
case 30 => println(age);
case 40 => println(age);
case _ => println("default");
}
// match as expression
val result= age match
{
case 20 => age;
case 20 => age;
case 25 => age;
case 18 => age;
case 30 => age;
case 40 => age;
case _ => println("default");
}
}
}

=-- odd/even

// match expression
object demo
{
def main(args: Array[String]) //
{

val age=18;
age match
{
case 20 => println(age);
case 25 => println(age);
case 18 => println(age);
case 30 => println(age);
case 40 => println(age);
case _ => println("default");
}
// match as expression
val result= age match
{
case 20 => age;
case 20 => age;
case 25 => age;
case 18 => age;
case 30 => age;
case 40 => age;
case _ => println("default");
}

val i=7;
i match
{
case 1|3|5|7|9 => println ("odd");
case 2|4|6|8|10 => println ("odd");

}
}
}

// Functions in scala
//def Functionname(x:Int, Y:INT): Int ={ }
object demo
{

def add(x:Int, y:Int):


Int = { return x+y; };

def main(args : Array [String])


{

println(add(45,13));
}
}

// Functions in scala
//def Functionname(x:Int, Y:INT): Int ={ }
object demo
{

def add(x:Int, y:Int):


Int = x+y;

def sub(x:Int, y:Int):


Int = x-y;

def mul (x:Int, y:Int):


Int = x*y;

def div(x:Int, y:Int):


Int = x/y; ;

def mod(x:Int, y:Int):


Int = x%y;

def main(args : Array [String])


{

println(add(45,13));
println(sub(45,13));
println(div(45,13));
println(mul(45,13));
println(mod(45,13));
}
}
// Functions in scala
//def Functionname(x:Int, Y:INT): Int ={ }
object demo
{
object Math
{

def add(x:Int, y:Int):


Int = x+y;
}

def add(x:Int, y:Int):


Int = x+y;

def sub(x:Int, y:Int):


Int = x-y;

def mul (x:Int, y:Int):


Int = x*y;

def div(x:Int, y:Int):


Int = x/y; ;

def mod(x:Int, y:Int):


Int = x%y;

def main(args : Array [String])


{
println(Math.add(34,76));
println(add(45,13));
println(sub(45,13));
println(div(45,13));
println(mul(45,13));
println(mod(45,13));
}
}

Llllllllllll
// Functions in scala
//def Functionname(x:Int, Y:INT): Int ={ }
object demo
{
object Math
{

def add(x:Int, y:Int):


Int = x+y;
def square(x:Int)=x*x;
}

def add(x:Int, y:Int):


Int = x+y;
def sub(x:Int, y:Int):
Int = x-y;

def mul (x:Int, y:Int):


Int = x*y;

def div(x:Int, y:Int):


Int = x/y; ;

def mod(x:Int, y:Int):


Int = x%y;

def main(args : Array [String])


{
println(Math.add(34,76));
println(add(45,13));
println(sub(45,13));
println(div(45,13));
println(mul(45,13));
println(mod(45,13));
println(Math.square(5));
}
}

You might also like