img
process is described in detail later in this topic.) This approach can be generalized.
Using the + operator, you can join together as many items as you want within a single
println( ) statement.
The next line of code assigns num the value of num times 2. Like most other languages,
Java uses the * operator to indicate multiplication. After this line executes, num will contain
the value 200.
Here are the next two lines in the program:
System.out.print("The value of num * 2 is ");
System.out.println(num);
Several new things are occurring here. First, the built-in method print( ) is used to display
the string "The value of num * 2 is ". This string is not followed by a newline. This means
that when the next output is generated, it will start on the same line. The print( ) method is
just like println( ), except that it does not output a newline character after each call. Now
look at the call to println( ). Notice that num is used by itself. Both print( ) and println( )
can be used to output values of any of Java's built-in types.
Two Control Statements
Although Chapter 5 will look closely at control statements, two are briefly introduced here so
that they can be used in example programs in Chapters 3 and 4. They will also help illustrate
an important aspect of Java: blocks of code.
The if Statement
The Java if statement works much like the IF statement in any other language. Further, it is
syntactically identical to the if statements in C, C++, and C#. Its simplest form is shown here:
if(condition) statement;
Here, condition is a Boolean expression. If condition is true, then the statement is executed.
If condition is false, then the statement is bypassed. Here is an example:
if(num < 100) System.out.println("num is less than 100");
In this case, if num contains a value that is less than 100, the conditional expression is
true, and println( ) will execute. If num contains a value greater than or equal to 100, then
the println( ) method is bypassed.
As you will see in Chapter 4, Java defines a full complement of relational operators
which may be used in a conditional expression. Here are a few:
Operator
Meaning
<
Less than
>
Greater than
==
Equal to
Notice that the test for equality is the double equal sign.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home