# Lesson 14: For loop and range expressions in Kotlin Language

##### Share this

For loop and range expressions in Kotlin Language. The for loop structure has some variants in Kotlin, in this concept we will see the **for loop **structure with range expressions.

Let’s look first as you define and create a range.

A range defines an interval that has an initial value and a final value, it is defined using the operator ..

Examples of definition of ranges:

Val oneDigit = 1..9 Val dozen = 1..12 Var letters = "a" .. "z"

If we need to know if a value is within a range we must use the operator in or el! In:

Val dozen = 1..12 If (5 in a dozen) Println ("5 is in the dozen range") If (18! In dozen) Println ("18 is not in the dozen range")

The two if are true.

Let’s now see the repetitive structure for using a range to repeat a block of commands.

### Problem 1

Perform a program that prints the numbers from 1 to 100 on the screen using kotlin For loop.

#### Project47 – Principal.kt

```
fun main (parameter: Array <String> ) {
for (i in 1..100)
println (i)
}
```

The variable “i” is defined from type **Int** by inference since the range is 1..100

In the first execution of the repetitive cycle i stores the initial value of the range ie the 1. After executing the block the variable i takes the value 2 and so on.

### Problem 2

Develop a program that allows the loading of 10 values per keyboard and then show us the sum of the values entered and their average. This problem we already developed using the while, we will solve it using the for loop structure.

#### Project48 – Principal.kt

```
fun main (parameter: Array <String>) {
//coding180.com
var sum = 0
for (i in 1..10) {
print ("Enter a value:")
val value = readLine () !!. toInt ()
sum += value
}
println ("The sum of the values entered is $sum")
val average = sum / 10
println ("Your average is $average")
}
```

As we can see the variable i within the for loop is not used within the repetitive block and only serves us to make the block contained in the for is repeated 10 times.

When we know how many times to repeat a block of instructions it is more convenient to use the for a while where we must define, initialize and increment a counter.

### Problem 3

Write a program that reads 10 student grades and tell us how many have grades greater than or equal to 7 and how many grades using for loop.

#### Project49 – Principal.kt

```
fun main (parameter: Array <String>) {
//coding180.com
var approved = 0
var failed = 0
for (i in 1..10) {
print ("Enter note:")
val note = readLine () !!. toInt ()
if (note>= 7)
approved ++
else
failed ++
}
println ("Number of students with grades greater than or equal to 7: $approved")
println ("Number of students with grades smaller than 7: $failed")
}
```

Again as we need to load 10 values per keyboard we have a for:

```
For (i in 1..10) {
```

### Problem 4

Develop a program that counts how many multiples of 3, 5 and 9 are in the range of 1 to 10000 (Do not load values by keyboard)

#### Project50 – Principal.kt

```
fun main (parameter: Array <String>) {
//coding180.com
var mult3 = 0
var mult5 = 0
var mult9 = 0
for (i in 1..10000) {
if (i% 3 == 0)
mult3 ++
if (i% 5 == 0)
mult5 ++
if (i% 8 == 0)
mult9 ++
}
println ("Amount of multiples of 3: $mult3")
println ("Amount of multiples of 5: $mult5")
println ("Amount of multiples of 9: $mult9")
}
```

In this problem remember that the counter i takes the first turn the value 1, then the value 2 and so on up to the value 10000.

If we want to find out if a value is a multiple of 3 we get the remainder to divide it by 3 and if that result is zero then we can infer that the number is a multiple of 3.

### Problem 5

Write a program that reads n integers and computes the number of even values entered with for loop.

This type of problem can also be solved by using the for repetitive structure because when we express the range we can have a variable name.

#### Project51 – Principal.kt

```
fun main (Parameters: Array <String>) {
//coding180.com
var cant = 0
print ("How many values will you enter to analyze:")
val quantity = readLine () !!. toInt ()
for (i in 1..quantity) {
print ("Enter value:")
val value = readLine () !!. toInt ()
if (value% 2 == 0)
cant
}
println ("Number of pairs: $cant")
}
```

As we see in the repetitive structure for the final value of the range we have the variable quantity instead of a fixed value:

```
For (i in 1..quantity) {
```

Until the program is executed we can not know how many times the for is repeated. The number of repetitions will depend on the number that the operator loads.

### For variants

If we need the for variable not to receive all the values included in the range but to advance by 2 in 2 we can use the following syntax:

For (i in 1..10 step 2) Println (i)

The values 1, 3, 5, 7, 9 are printed

If we need the variable to take the value 10, then the 9 and so on, ie in reverse form we use the following syntax:

For (i in 10 downTo 1) Println (i)

The values 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 are printed.

We can also use the step with the downTo:

For (i in 10 downTo 1 step 2) Println (i)

The values 10, 8, 6, 4, 2 are printed.

## Problems proposed

- Make a program that reads n pairs of data, each pair of data corresponds to the measure of the base and the height of a triangle. The program must inform:

A) From each triangle the measure of its base, its height and its surface (the surface is calculated by multiplying the base by the height and dividing by two).

B) The number of triangles whose surface area is greater than 12. - Develop a program that requests the loading of 10 numbers and print the sum of the last 5 values entered with FOR loop.
- Develop a program that shows the multiplication table of 5 (from 5 to 50) using for loop
- Make a program that allows entering a value from 1 to 10 and show us the multiplication table of the same (the first 12 terms)

Example: If you entered 3, the values 3, 6, 9, up to 36 must be displayed using for loop. - Make a program that reads the sides of n triangles, and inform:

A) Of each of them, what type of triangle is: equilateral (three equal sides), isosceles (two equal sides), or scalene (no side equal)

B) Number of triangles of each type. - Write a program that asks you to enter coordinates (x, y) that represent points in the plane using for loop.

Inform how many points have been entered in the first, second, third and fourth quadrant. At the beginning of the program you are asked to enter the number of points to process. - It loads 10 integer values per keyboard. You want to know:

A) The amount of negative values entered.

B) The number of positive values entered.

C) The number of multiples of 15.

D) The accumulated value of the numbers entered that are even.

## Solution

[sociallocker id=”958″]

```
fun main (parameter: Array <String>) {
//coding180.com
var amount = 0
print ("How many triangles will be processed:")
val n = readLine () !!. toInt ()
for (i in 1..n) {
print("Enter the base value:")
val base = readLine()!!.toInt()
print("Enter height value:")
val height = readLine()!!.toInt()
val surface = base * height / 2
println("Surface is $surface")
if (surface > 12)
amount++
}
println ("The number of triangles with an area greater than 12 are: $amount")
}
```

project 53

```
fun main (parameter: Array <String>) {
// coding180.com
var sum = 0
for (i in 1..10) {
print ("Enter an integer value:")
val value = readLine () !!. toInt ()
if (i> 5)
sum += value
}
print ("The sum of the last 5 values is: $ sum")
}
```

//project 54

```
fun main (parameter: Array <String>) {
for (table5 in 5..50 step 5)
println (table5)
}
```

// project 55

```
fun main (argument: Array <String>) {
print ("Enter a value between 1 and 10:")
val value = readLine () !!. toInt ()
for (i in value .. value * 12 step value)
println (i)
}
```

Project 56

```
fun main (argument: Array <String>) {
// coding180.com
var cant1 = 0
var cant2 = 0
var cant3 = 0
print ("Enter the number of triangles:")
val n = readLine () !!. toInt ()
for (i in 1..n)
print ("Enter side 1:")
val side1 = readLine () !!. toInt ()
print ("Enter side 2:")
val side2 = readLine () !!. toInt ()
print ("Enter side 3:")
val side3 = readLine () !!. toInt ()
if (side1 == side2 && side1 == side3) {
println ("It is an equilateral triangle.")
cant1 ++
} else
if (side1 == side2 || side1 == side3 || side2 == side3) {
println ("It is an isosceles triangle.")
cant2 ++
} else {
println ("It is a scalene triangle.")
cant3 ++
}
// coding180.com
println ("Number of equilateral triangles: $cant1")
println ("Number of isosceles triangles: $cant2")
println ("Number of scalen triangles: $cant3")
}
```

Project 57

```
fun main (parameter: Array <String>) {
// coding180.com
var cant1 = 0
var cant2 = 0
var cant3 = 0
var cant4 = 0
print ("Number of points to enter:")
val quantity = readLine () !!. toInt ()
for (i in 1..quantity) {
print ("Enter x: coordinate")
val x = readLine () !!. toInt ()
print ("Enter coordinate and:")
val y = readLine () !!. toInt ()
if (x> 0 && y> 0)
cant1 ++
else
if (x <0 && y> 0)
cant2 ++;
else
if (x <0 && y <0)
cant3 ++
else
if (x> 0 && y <0)
cant4 ++
}
//coding180.com
println ("Number of points in the first quadrant: $cant1")
println ("Number of points in the second quadrant: $cant2")
println ("Number of points in the third quadrant: $cant3")
println ("Number of points in the fourth quadrant: $cant4")
}
```

Project 58

```
fun main (parameter: Array <String>) {
var negative = 0
var positive = 0
var mult15 = 0
var sum = 0
for (i in 1..10) {
print ("Enter value:")
val value = readLine () !!. toInt ()
if (value <0)
negative ++
else
if (value> 0)
positive ++
if (value% 15 == 0)
mult15 ++
if (value% 2 == 0)
sum + = value
}
//coding180.com
println ("Number of negative values: $negative")
println ("Number of positive values: $positive")
println ("Multivalue amount of 15: $mult15")
println ("Sum of even values: $sum")
}
```

[/sociallocker]

## Comments

## Leave a Comment