Sams Teach Yourself C in 21 Days

(singke) #1
Java Language Fundamentals 717

BD4


This has been a very brief explanation of Java’s operators. For more details you can turn
back to Day 4,”Statements, Expressions and Operators,” to review C’s operators.

Flow Control ......................................................................................................


Flow control refers to controlling which parts of your code execute, when they execute,
and how many times they execute. Flow control statements are essential so your program
can react to user input, different data conditions, and other factors.

Java uses braces {} to enclose the code in a class or a method. Braces are also
used to create compound statements (a block, just as in C or C++). Any
group of two or more Java statement within braces is a compound state-
ment, and it is treated as a unit for execution purposes.

Note


if...else......................................................................................................

Theifcontrol structure executes just like in C and C++. A Java statement is executed
only if a condition is true. Optionally, the elseclause executes a second statement if the
condition is false. Remember, a statement can be a single simple statement or a com-
pound statement (in braces) containing multiple simple statements. The syntax is
if (condition) statement1 else statement2;
Conditionis an expression that evaluates to true or false. statement1is executed if con-
ditionis true; statement2is executed if conditionis false. You can simply omit the
elseclause if there are no statements to be executed or conditionis false:
if (condition) statement1;
if...elsestructures can be nested as required to test multiple conditions:
if (condition1) {
if (condition2)
statement1;
else
statement2;
}
else
statement3;
You can also stack ifstatements to test multiple conditions:
if (condition1)
statement1;
else if (condition2)

39 448201x-Bonus4 8/13/02 11:19 AM Page 717

Free download pdf