D
D
Danil Antoshkin2015-06-26 20:24:36
Java
Danil Antoshkin, 2015-06-26 20:24:36

Can you tell how this piece of code works?

public class Main
{
    public static void main(String[] args)
    {
        int i, N = 20;

        for (i = 0; -i < N; i--)  {
            System.out.print("*");
        }
    }
}

Why does this code work?
If i is always negative and when it becomes -20 then exit the loop, which means that N is no longer greater than i

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2015-06-26
@TwerTrue

Minus minus twenty (-(-20)) equals twenty. Well, twenty is not less than twenty.
Just replace System.out.print("*"); to System.out.print(-i); and everything will become clear to you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question