You are on page 1of 41

Chapter 4

Introduction to Control
Statements

Fundamentals of Java
Objectives

 Use the increment and decrement operators.


 Use standard math methods.
 Use if and if-else statements to make
choices.

2 Fundamentals of Java
Objectives (cont.)

 Use while and for loops to repeat a


process.
 Construct appropriate conditions for control
statements using relational operators.
 Detect and correct common loop errors.

3 Fundamentals of Java
Vocabulary

 Control statements
 Counter
 Count-controlled loop
 Entry-controlled loop
 Flowchart
 Infinite loop

4 Fundamentals of Java
Vocabulary (cont.)

 Iteration
 Off-by-one error
 Overloading
 Sentinel
 Task-controlled loop

5 Fundamentals of Java
Additional Operators

 Extended assignment operators


– Assignment operator combined with
arithmetic and concatenation operators

6 Fundamentals of Java
Additional Operators (cont.)

 Increment operator: ++
– Increase value of operand by 1
 Decrement operator: --
– Decrease value of operand by 1

7 Fundamentals of Java
Standard Classes and Methods:
The Math Class

Table 4-1: Seven methods in the Math class


8 Fundamentals of Java
Standard Classes and Methods:
The Math Class (cont.)

 The two abs() methods are overloaded.


– Overloaded: Multiple methods in the same class
with the same name
 Using sqrt() method example:

9 Fundamentals of Java
Standard Classes and Methods:
The Math Class (cont.)

 Math class methods example:

10 Fundamentals of Java
Standard Classes and Methods:
The Random Class

 Random number generator: Returns


numbers chosen at random from a pre-
designated interval

Table 4-2: Methods in the Random class

11 Fundamentals of Java
The if and if-else Statements

 Principal forms:

12 Fundamentals of Java
The if and if-else Statements
(cont.)

 Additional forms:

13 Fundamentals of Java
The if and if-else Statements
(cont.)

 Better to over-use braces than under-use


them
– Can help to eliminate logic errors
 Condition of an if statement must be a
Boolean expression
– Evaluates to true or false
 A flowchart can be used to illustrate the
behavior of if-else statements.
14 Fundamentals of Java
The if and if-else Statements
(cont.)

Figure 4-1: Flowcharts for the if and if-else statements


15 Fundamentals of Java
The if and if-else Statements
(cont.)

 Examples of if statements:

16 Fundamentals of Java
The if and if-else Statements
(cont.)

Table 4-3: Relational operators

17 Fundamentals of Java
The if and if-else Statements
(cont.): Checking Input for Validity

Example 4.1: Computes the area of a circle if the


radius >= 0 or otherwise displays an error message
18 Fundamentals of Java
The while Statement

 Provides a looping mechanism


– Executes statements repeatedly for as long as
some condition remains true

19 Fundamentals of Java
The while Statement (cont.)

Figure 4-2: Flowchart for a while statement

20 Fundamentals of Java
The while Statement (cont.)

 Example:

 Counter-controlled loop:
– cntr is the counter
– Loop repeats a determined number of times

21 Fundamentals of Java
The while Statement (cont.)

 Tracing: Track value of variables through


each iteration of the loop

Table 4-4: Trace of how variables change


on each iteration through a loop
22 Fundamentals of Java
The while Statement (cont.)

 Counting backward:

23 Fundamentals of Java
The while Statement (cont.)

 Task-controlled loop: Continues to


execute until some task is accomplished

24 Fundamentals of Java
The while Statement (cont.)

 Common structure of a while loop:

25 Fundamentals of Java
The while Statement (cont.)

Example 4.2: Compute and display the factorial of n


26 Fundamentals of Java
The for statement

 Combines counter initialization, condition test,


and update into a single expression
 Easy to create count-controlled loops

27 Fundamentals of Java
The for statement (cont.)

 Example:

28 Fundamentals of Java
The for statement (cont.)

 Count-controlled input:

29 Fundamentals of Java
The for statement (cont.)

 Better to declare the loop control variable


within the loop header
– Only visible within the loop
– Variable name can be reused later in other loops

30 Fundamentals of Java
The for statement (cont.)

 Both for loops and while loops are entry-


controlled loops.
– Condition tested at top of loop on each pass
 Choosing a for loop versus a while loop is
often a matter of style.
– for loop advantages:
 Can declare loop control variable in header
 Initialization, condition, and update in one line of
code
31 Fundamentals of Java
Nested Control Statements and
the break Statement
 Control statements may be nested.

32 Fundamentals of Java
Nested Control Statements and
the break Statement (cont.)
 break statement: Prematurely end a loop

33 Fundamentals of Java
Nested Control Statements and
the break Statement (cont.)

 Sentinel-controlled input: Continue a loop


until a sentinel variable has a specific value

34 Fundamentals of Java
Using Loops with Text Files

 Advantages of using text files versus input


from a human user:
– Data sets can be much larger.
– Data input quickly, with less chance of error.
– Data can be used repeatedly.
 Data files can be created by hand in a text
editor or generated by a program.

35 Fundamentals of Java
Using Loops with Text Files (cont.)

 Text file format:


– If data is numerical, numbers must be separated
by white space.
– Must be an end-of-file character
 Used by program to determine whether it is done
reading data
 When an error occurs at run-time, the JVM
throws an exception.
– IOException thrown if error occurs during file
operations
36 Fundamentals of Java
Using Loops with Text Files (cont.)

Example 4.3: Computes and displays the average


of a file of floating-point numbers
37 Fundamentals of Java
Using Loops with Text Files (cont.)

Example 4.4: Inputs a text file of integers and


writes these to an output file without the zeroes
38 Fundamentals of Java
Summary

 Java has operators for extended assignment


and for increment and decrement.
 The Math class provides several useful
methods, such as sqrt and abs.
 The Random class allows you to generate
random integers and floating-point numbers.
 if and if-else statements are used to
make one-way and two-way decisions.
39 Fundamentals of Java
Summary (cont.)

 The comparison operators, such as ==, <=,


and >=, return Boolean values that can serve
as conditions for control statements.
 The while loop allows the program to run a
set of statements repeatedly until a condition
becomes false.
 The for loop is a more concise version of
the while loop.
40 Fundamentals of Java
Summary (cont.)

 Other control statements, such as an if


statement, can be nested within loops.
 A break statement can be used with an if
statement to terminate a loop early.

41 Fundamentals of Java

You might also like