Wednesday 27 September 2017

6)Scala programming constructs: for,while and do-while loop

for loop:
Repeatation of execution until the specific cycles.

1)Iterating 1 to 5 number in scala using to keyword:

Example:



Note:
keyword to which meant that iteration number 5 is included.

2)Iterating 1 to 5 number in scala using until keyword:

Example:



Note:

keyword until  which meant that iteration number 5 is not included.

3)if conditions in for loop as well:

if clause to add filtering support as you iterate through your items.

Example:




4)if conditions in for loop and return the result back using the yield keyword:


using the {} in our for loop to make our expressions more explicit.

Example:




5)for loop usage in 2-Dimensional array:

In scala, to work with 2-Dimensional arrays. There is a Scala Array class and the appropriate ofDim() function, pass in the type of String in square brackets [String] and then specify R by C array in the function parameter.

Syntax:
Declaration:
val varible_name = Array.ofDim[String](R,C)
Initialization:
varible_name[R1][C1]=valuevarible_name[R1][C2]=valuevarible_name[R2][C1]=valuevarible_name[R2][C2]=value

Then as usual, loop through it for each occurrence.

Example:





While loop:
Executes the block of code, only if the condition is true.

Example:



do while loop:
do {} will be ran at least once regardless of the condition within the while() clause.

Example:








No comments:

Post a Comment

Fundamentals of Python programming

Fundamentals of Python programming: Following below are the fundamental constructs of Python programming: Python Data types Python...