Loop flow control logic

How is the next cycle for the cycle different from the original? Why does it check the condition after initialization?

Why is this behavior and what is another complex loop behavior for?

class FooBar
{
static public void main(String... args)
{
    for (int x = 1; ++x < 10;)
    {
        System.out.println(x);  // it starts printing from 2

    }
}
}

Output :
  2
  3
  4
  5
  6
  7
  8
  9

+4
source share
8 answers

The increment is xcombined with the condition. Usually the increment is executed in the third section of the loop for, which is the statement executed at the end of the iteration.

. , , , , - - x.

x 1, 2 .

+2

++x x. :

  • , for-loop (.. ), ,
  • - , .

1 9, :

for (int x = 1; x < 10; ++x) { ... }

, .

+3

, , -, x.

++x - x

x++ - x .

, :

for (int x = 1; ++x < 10;)

:

  • x 1
  • x 1
  • , x < 10. , . , .

, :

for (int x = 1; x < 10; x++)
  • x 1
  • , x < 10. , . , .
  • x 1
+1

for-loop ( ) -

for(initialization; conditionChecking; increment/decrement){

   //body

}

for-loop incremetn/decrement. for ( x) for-loop. , increment/decrement . for-loop, , -

for(int x=0; x<10; ){
   //do something
   x++;
}  

( ) for-loop , :

int x=0;
for(;x<10;x++); //empty body; do nothing; just increment x
+1

, . . x, ++x>10. . , for -.

+1

for, :

for(initialization; condition; inc/dec/divide..)

: for (initialization; Inc & check; nothing)

?: for(some blaBla; any Bla; some, more, bla)

. - , . some blaBla !

:

:

  for (int x = 1; x++ < 10;)
      {
           System.out.println(x);

      } 

2..10? :

   for (int x = 1; ++x < 10;)
          {
               System.out.println(x);

          } 

2..9

, pre/post fix. x++ , . x++ < 10; ++x < 10; , .

, x 9, 9 (++) < 10 → 9 ? , 9 ++. x. ++ x 1st so ++ 9 < 10 → 10 < 10 NO.

+1

:

x = 1;
while (++x < 10)
{
    System.out.println(x);
}

:

x = 1;
do
{
    System.out.println(x);
}
while (++x < 10);

.

, :

for (int x = 1; x < 10; x++)
{
    System.out.println(x);
}
+1

TL; DR - , ++x pre increment. ++x, x . 2 1 , ++x 10

0
source

Source: https://habr.com/ru/post/1599166/


All Articles