Although Loop Weirdness in Java

I noticed that java (hence possibly C) has no problem with this:

while(condition1) {
    //do somethin'
} while(condition2);

This is the same as:

while(condition1 && condition2) {
    //do somethin'
}
+3
source share
3 answers

No, you have two loops.

while(condition1) {
  // do something
}

while(condition2); // second loop which does nothing.

The second cycle coincides with

while(condition2) { }

EDIT: My suggestion is to regularly use the automatic formatter in your IDE. Otherwise, you can create formatting that assumes that the code does what it does not.

example 1

if (condition)
    statement1;
    statement2;
statement3;

In this example, it seems that the first two statements are part of the if condition, but only the first.

example 2

http://www.google.com/
statement;

Not like legal Java, but that’s not for the reasons formatting offers;)

+14
source

No, they are different.

The first one will be launched while(condition1).

while(condition2), , ;, - .

, , if, for, while, {}, .

:

if (condition)
    System.out.println("hello"); // prints only if condition is true.
    System.out.println("no"); // not bound to the 'if'. Prints regardless.

while (condition)
    ; // do nothing!
    System.out.println("something"); // not bound to the while

while Java

7.6 while

A while :

while (condition) {
    statements;
}

while :

while (condition);
+4

java , . ,

do {

} while (cond)

EDIT: . } . .

-1

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


All Articles