Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Kotlin
fun main(args: Array<String>) {
    var x=0
    var y=20
    for(outer in 1..3){
        for(inner in 4 downTo 2){
            x+=6
            y++
            x+=3
        }
        y-=2
    }
    println("$x $y")

}


What I have tried:

i want to understand why the result is 81 23

thank you
and best regards
Posted
Updated 16-Sep-20 7:00am
Comments
[no name] 16-Sep-20 12:45pm    
You're executing your inner loop 9 times. 9 x 9 = 81 (x).

Go through it on paper, or line by line in a debugger

var x=0


So x is 0

for(outer in 1..3)


everything in this for will loop with outer being 1 then 2 then 3. So for the first pass outer is 1;

x = 0
outer = 1

for(inner in 4 downTo 2)


this will loop with inner being 4, 3, 2

x = 0
outer = 1
inner = 4

x+=6


x = x + 6, as x was 0 it is now six

x = 6
outer = 1
inner = 4

x+=3


Same thing, so now x is 9

x = 9
outer = 1
inner = 4

That's the "inner" loop finished so it runs again only now inner is 3

x = 9
outer = 1
inner = 3

x+=6


x = 15
outer = 1
inner = 3

x+=3


x = 18
outer = 1
inner = 3

and so on. Follow the maths and you'll see why you get the results you do.
 
Share this answer
 
Comments
Orestis Tsanakas 16-Sep-20 16:54pm    
ok sorry to understand the fun under in 1..3 how many times it will run? 3 right??? but to make it 81 we have x :0 9 18 27 36 45 64 73 81 so it is 9 times why ? and what about y how many times? and the first run will be 20 +1= 21 - 2= 19... ?
F-ES Sitecore 17-Sep-20 6:34am    
There are two loops, the inner loop will loop 3 times, however the outer loop will loop the inner loop 3 times, so that is 9 loops in total.
Quote:
i want to understand why the result is 81 23

Why don't you watch the code as it execute step by step with the help of debugger?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900