Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 173
Watch Indentation and Braces
ОглавлениеOne area where the exam writers will try to trip you up is if
statements without braces ({}
). For example, take a look at this slightly modified form of our example:
if(hourOfDay < 11) System.out.println("Good Morning"); morningGreetingCount++;
Based on the indentation, you might be inclined to think the variable morningGreetingCount
is only going to be incremented if hourOfDay
is less than 11
, but that's not what this code does. It will execute the print statement only if the condition is met, but it will always execute the increment operation.
Remember that in Java, unlike some other programming languages, tabs are just whitespace and are not evaluated as part of the execution. When you see a control flow statement in a question, be sure to trace the open and close braces of the block, ignoring any indentation you may come across.