Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2024E

Section A.7 Executable Statements

Simple statements, such as assignment statements, should be written one per line and should be aligned with the other statements in the block.
Compound statements are those that contain other statements. Examples would include if statements, for statements, while statements, and do-while statements. Compound statements should use braces and appropriate indentation to highlight the statement’s structure. Here are some examples of how to code several kinds of compound statements:
if (condition) {        // A simple if statement
     statement1;
     statement2;
 } // if

 if (condition1) {       // An if-else statement
     statement1;
 } else if (condition2) {
     statement2;
     statement3;
 } else {
     statement4;
     statement5;
 } // if/else

for (initializer; entry-condition; updater) { // For loop
    statement1;
    statement2;
} // for

while (condition) {          // While statement
    statement1;
    statement2;
} // while

do {                        // Do-while statement
    statement1;
    statement2;
} while (condition);
You have attempted of activities on this page.