Csbp219 Sp24 Java Week3
Csbp219 Sp24 Java Week3
Spring 2025
Week 3 – Part 1
Basics of Java
Control Structure – Repetition
Objectives
In this chapter you will learn:
Repetition (looping) control structures
Counter-controlled repetition structures
Sentinel-controlled repetition structures
Flag-controlled repetition structures
EOF-controlled repetition structures
break, continue
for looping (repetition) structures
Nested control structures
Slide 2
while Looping (Repetition) Structure
Slide 3
Counter controlled while Loops
When we know how many times certain
statements need to be executed, we use a
counter controlled while loop
Slide 4
while Loops
Question: How many times this loop is going
to iterate?
Slide 5
Sentinel-Controlled while Loops
A sentinel-controlled while loop uses a
sentinel value to control the loop.
Slide 6
Flag-Controlled while Loops
A flag-controlled while loop uses a Boolean
variable to control the loop.
Slide 7
EOF-Controlled while Loops
When reading from a file, the End-Of-File
while loop tests for the end of file reached
using the scanner’s hasNext() method.
Slide 8
break And continue Statements
The break and continue statements alter
the flow of control in a program.
The break statement provides an immediate
exit from the loop.
The continue statement skips the remaining
statements in the loop & proceeds with the
next iteration of the loop.
Slide 9
break Statements
Example:
Slide 10
continue Statement
Example:
Slide 11
for Looping Structure
Syntax:
Example:
Slide 12
for Looping Structure
Example:
System.out.print( i ); // 10
OR
Slide 13
for Loop Vs while Loop
Slide 14
Nested Controlled Structures
Nested repetition is when a control structure
is placed inside of the body or main part of
another control structure.
Nested repetition are often used for
processing things like multi-dimensional
arrays.
Example: Output:
for (int i=0; i < 5; i++) { **********
for (int j = 0; j < 10; j++) { **********
System.out.print("*");
}
**********
System.out.println(); **********
} **********
Slide 15
Exercises
Write a program to display all odd numbers between
100 and 200. Use a while loop.
Write a program to display all numbers between 100
and 200 that are divisible by 3. Use a for loop
Write a program that will repeatedly ask the user to
input a number, then count all the positive and
negative numbers entered by the user. The program
stops when the user enters a value of zero.
Write a program that will ask the user to input the
number of items purchased, then for each item, asks
for the item price. The program then calculates and
displays the total price of all items, the highest price
of all items, and the lowest price of all items.
CSBP219: Object Oriented Programming
Spring 2024
Week 3 – Part 2
Basics of Java
Methods
Objectives
In this chapter you will learn:
Pre-defined methods.
Value returning methods.
Void methods.
Actual and formal parameters.
Methods with no parameters
The scope of identifiers.
Method overloading.
Slide 18
Pre-defined Classes and Methods
• Many predefined packages, classes, and
methods in Java
• Library: collection of packages
• Package: contains several classes
• Class: contains several methods
• Method:
Examples set of classes:
of Predefined instructions
• String, Math, Random, …
• Scanner, FileReader, PrintWriter, …
• Integer, Double, Float, Character, …
Slide 20
Class String methods
Slide 21
class Math (Package: java.lang)
Slide 22
class Character (Package: java.lang)
Slide 23
Example
User-Defined Methods
User-defined methods in Java are classified into
two categories:
Slide 25
Value-Returning Methods
Value-returning methods compute and
return a value.
Syntax:
Header
Body
Slide 26
Value-Returning Methods
Example:
x Mai
n
Squar
e
number
Slide 27
Void Methods
Void methods do not return any value
Slide 29
Methods With No Parameters
A method’s formal parameter list can be
empty, but the parentheses are still needed:
Example
Slide 30
Variable Scope
A variable, x, declared within a
block is accessible only within its
block and by those blocks that are
nested within that block.
Slide 31
Method Overloading
Method overloading: creating several
methods, within a class, with the same name
but with differnet parameter list.
The following methods correctly overload the method myFun: