Since I started studying Java Silver, I will upload it as my memorandum. Since it is a rough memo, details are not described.
It is possible to omit {} in the while statement and do-while statement. However, note that if {} is omitted, only one sentence will be repeated. In the following example sentence, only "System.out.println (" 1st sentence ");" is repeated.
int cnt = 0; while(cnt++ < 10) System.out.println ("1st sentence"); System.out.println ("second sentence");
Also note that it is one sentence, not one line. Even if there is no line break, only the first sentence will be repeated if it is separated by ";".
Also, if you write two or more sentences when {} is omitted, a compile error will occur. int cnt = 0; do System.out.println ("1st sentence"); System.out.println ("second sentence"); while(cnt++ < 10);
Recommended Posts