methodName {
statement1;
statement2;
statement3;
.
.
.
}
IF CONDITION such-and-such IS TRUE THEN, AND ONLY THEN, DO THE FOLLOWING...
{
statement1;
statement2;
...
}
DO THE FOLLOWING EXACTLY 10 TIMES...
{
statement1;
statement2;
...
}
KEEP DOING THE FOLLOWING FOR AS LONG AS CONDITION such-and-such IS TRUE...
{
statement1;
statement2;
...
}
statements-before;
if ( condition ) {
statements;
...
}
statements-after;
if ( condition ) {
it will evaluate whatever condition is. If it evaluates to true, then, and
only then, will Java start executing the body of the if structure (the stuff
between the two squiggly brackets), and once finished, will proceed executing the statement
after the if structure. If condition evaluates to false, then the
body of the if structure is skipped entirely, and Java continues executing with the
statement appearing after the if structure.
if ( condition ) {
statementsA;
} else {
statementsB;
}
if ( conditionA ) {
statementsA;
} else if ( conditionB ) {
statementsB;
}
if ( conditionA ) {
statementsA;
} else {
if ( conditionB ) {
statementsB;
}
}
if ( conditionA ) {
statementsA;
} else if ( conditionB ) {
statementsB;
} else if ( conditionC ) {
statementsC;
} else if ( conditionD ) {
statementsD;
}
if ( conditionA ) {
statementsA;
} else if ( conditionB ) {
statementsB;
} else if ( conditionC ) {
statementsC;
} else if ( conditionD ) {
statementsD;
} else {
else-statements;
}
if ( conditionA ) {
statementsA;
} else if ( conditionB ) {
statementsB;
} else {
statementsC;
}
if ( monthNum == 1 )
output.setText("January");
else if (monthNum == 2)
output.setText("February");
else if (monthNum == 3)
output.setText("March");
else if (monthNum == 4)
output.setText("April");
else if (monthNum == 5)
output.setText("May");
else if (monthNum == 6)
output.setText("June");
else if (monthNum == 7)
output.setText("July");
else if (monthNum == 8)
output.setText("August");
else if (monthNum == 9)
output.setText("September");
else if (monthNum == 10)
output.setText("October");
else if (monthNum == 11)
output.setText("November");
else if (monthNum == 12)
output.setText("December");
else
output.setText("Smarch! Just kidding, bad monthNum");
switch( monthNum ) {
case 1:
output.setText("January");
break;
case 2:
output.setText("February");
break;
case 3:
output.setText("March");
break;
case 4:
output.setText("April");
break;
case 5:
output.setText("May");
break;
case 6:
output.setText("June");
break;
case 7:
output.setText("July");
break;
case 8:
output.setText("August");
break;
case 9:
output.setText("September");
break;
case 10:
output.setText("October");
break;
case 11:
output.setText("November");
break;
case 12:
output.setText("December");
break;
default:
output.setText("Smarch! Just kidding, bad monthNum");
}
switch ( expression ) {
case valueA:
statementsA;
break;
case valueB:
statementsB;
break;
case valueC:
statementsC;
break;
.
.
.
default:
default-statements;
}
case value:
statements;
break;
switch ( expression ) {
Java will evaluate expression and determine its value. Suppose the value it
evaluates to is X. IF there is a "case" block of the form
case X:
statements;
break;
then execution will "jump to" the corresponding set of statements;. Once execution
of statements; finished, and the break; statement is
reached, execution will continue after the end of the switch structure. If,
however, there is no "case" block with the appropriate value X, then one of two
things will happen: if there is no default: block, then
none of the statements will be executed, and Java will continue execution after the
end of the switch structure. Otherwise, the statements within the "default" block
will be executed, and once they are finished execution will continue after the end of
the switch statement.
statements-before;
while ( condition ) {
statements;
...
}
statements-after;
while ( condition ) {
It will evaluate whatever condition is. If it evaluates to false, then the
body of the while loop is skipped entirely and Java continues executing after the while loop.
If condition evaluates to true, however, then Java will execute the body of
the loop and, once finished, return to the line
while ( condition ) {
and continue. That is, it will check condition again, and if true,
execute the body again. Otherwise, it will "exit the while loop".
int numToDisplay = 0;
while (numToDisplay < clickCount) {
output.append(numToDisplay + " ");
numToDisplay = numToDisplay + 1;
}
pre-statement;
while (condition) {
body-statements;
post-statement;
}
for ( pre-statement ; condition ; post-statement ) {
body-statements;
}
for ( int numToDisplay = 0 ; numToDisplay < clickCount ; numToDisplay = numToDisplay + 1 ) {
output.append(numToDisplay + " ");
}
int answer;
// break based on what the user wants...
if ( wantsDisplayed == "sum" ) {
// compute the sum
answer = 0;
for (int i = 0; i < N; i++) {
answer = answer + i;
}
} else {
// compute the product
answer = 1;
for (int i = 0; i < N; i++) {
answer = answer * i;
}
}
// initialize answer appropriately
int answer = 0;
if (wantsDisplayed == "product") {
answer = 1;
}
// compute the answer
for (int i = 0; i < N; i++) {
if (wantsDisplayed == "sum")
answer = answer + i;
else
answer = answer * i;
}