Title: Zhen Jiang
1CSC141 Computer Science I
- Zhen Jiang
- Dept. of Computer Science
- West Chester University
- West Chester, PA 19383
- zjiang_at_wcupa.edu
2Loop
- Smart if-decision making, smart program work,
talent programmer - Research experience (REU) - click on this link
- Temperature/humidity detection every second
- A repetition process for the 7x24 hours seamless
surveillance - Needs a computer support to REPEAT
3- While loop
- Format Logic, page 197, Figure 4-1.
- Sample, code 4-3, page 198.
4- ltinitializationgt
- while (lttestgt)
- ltbodygt
-
5- Do-while loop
- Format, page 208
- Logic, page 209, Figure 4-6.
- Sample, code 4-6, page 209.
6- How does this differ from the while loop?
- The controlled ltstatement(s)gt will always execute
the first time, regardless of whether the lttestgt
is true or false.
7- For loop
- Format, page 212, Figure 4-7.
- Logic, page 212, Figure 4-8.
- Sample, code 4-7, page 213.
8for (ltinitgt lttestgt ltupdategt) ltbodygt
9Body first, and then event change/update
10- Ex10
- http//www.cs.wcupa.edu/zjiang/141_ex10.pdf
- Ex11
- http//www.cs.wcupa.edu/zjiang/141_ex11.pdf
11- Trial
- population
- http//www.cs.wcupa.edu/zjiang/6billion.exe
- TV purchase
- http//www.cs.wcupa.edu/zjiang/tv563.exe
- 1248...
- http//www.cs.wcupa.edu/zjiang/1_2_4.exe
- 1234...99
- http//www.cs.wcupa.edu/zjiang/1to99.exe
12- Development process
- http//www.cis.temple.edu/jiang/LoopDevelopment.h
tm
13(No Transcript)
14Controlling Number of Loop Iterations
- If the number of iterations is known before the
loop starts, the loop is called a
count-controlled loop. - Counter 0, counter, counter ltnumber
- Counter 1, counter, counter ltnumber
- Use for loop for an easy development.
15(No Transcript)
16(No Transcript)
17Mapping iterations to counter values
- Suppose that we have the following loop
- for (int count 0 count lt 49 count)
- ...
-
- What statement could we write in the body of the
loop that would make the loop print the following
output? - 0 2 4 6 8
- Answer
- for (int count 0 count lt 49 count)
- System.out.print(2 count " ")
18- Now consider another loop of the same style
- for (int count 0 count lt 49 count)
- ...
-
- What statement could we write in the body of the
loop that would make the loop print the following
output? - 3 5 7 9 11
- Answer
- for (int count 0 count lt 49 count)
- System.out.print(2 count 3 " ")
19- What statement could we write in the body of the
loop that would make the loop print the following
output? - 2 7 12 17 22
- To find the pattern, it can help to make a table.
- Each time count goes up by 1, the number should
go up by 5. - But count 5 is too big by 3, so we must
subtract 3.
number to print
count 5
count 5 - 3
count
2
5
2
1
7
10
7
2
12
15
12
3
17
20
17
4
22
25
22
5
20(No Transcript)
21- Caution This is algebra, not assignment!
- Recall slope-intercept form (y mx b)?
- Slope is defined as rise over run (i.e. rise /
run). Since the run is always 1 (we increment
along x by 1), we just need to look at the
rise. The rise is the difference between the y
values. Thus, the slope (m) is the difference
between y values in this case, it is 5. - To compute the y-intercept (b), plug in the value
of y at x 1 and solve for b. In this case, y
2. - y m x b
- 2 5 1 b
- Then b -3
- So the equation is
- y m x b
- y 5 x 3
- y 5 count - 3
22- Algebraically, if we always take the value of y
at - x 1, then we can solve for b as follows
- y m x b
- y1 m 1 b
- y1 m b
- b y1 m
- In other words, to get the y-intercept, just
subtract the slope from the first y value (b 2
5 -3)? - This gets us the equation
- y m x b
- y 5 x 3
- y 5 count 3
- (which is exactly the equation from the previous
slides)?
23- What statement could we write in the body of the
loop that would make the loop print the following
output? - 17 13 9 5 1
- Let's create the loop table together.
- Each time count goes up 1, the number should ...
- But this multiple is off by a margin of ...
24- Code
- for (int i 1 i lt 4 i)
- System.out.println(i " squared is " (i
i)) -
- Output
- 1 squared is 1
- 2 squared is 4
- 3 squared is 9
- 4 squared is 16
1st iteration? 2nd iteration? 3rd iteration?
http//www.cs.wcupa.edu/zjiang/141_ex11.pdf
25- Coding (different from execution check)
- nkeyboard.nextInt() // try 6!
- for (int i 1 i lt n i)
-
- System.out.print("")
-
- System.out.println()
- Output
-
What is the body? Counter-controlled loop?
26- More complicate case
- nkeyboard.nextInt() // try 6!
- for (int i 1 i lt n i)
-
- for (int j 1 j lt n j)
-
- System.out.print("")
-
- System.out.println()
-
- Output
-
-
-
-
-
-
What is the body? Counter-controlled loop?
27- Code
- nkeyboard.nextInt() // try 5!
- for (int i 1 i lt n i)
-
- for (int j 1 j lt 10 j)
-
- System.out.print(
- (i j) " ")
-
- System.out.println()
-
- Output
- 1 2 3 4 5 6 7 8 9 10
- 2 4 6 8 10 12 14 16 18 20
- 3 6 9 12 15 18 21 24 27 30
- 4 8 12 16 20 24 28 32 36 40
- 5 10 15 20 25 30 35 40 45 50
What is the body? Counter-controlled
loop? Initialization and body detail?
28- How to confirm the initialization correct?
- On preparing the 1st iteration
- How to ensure the detail of the body?
- A consistent view of 1st, 2nd, 3rd iterations
- Map of the counter value to the iteration
expression
29- Code
- nkeyboard.nextInt() // try 6!
- for (i 1 iltn i) System.out.print()
- System.out.println()
- for (i 1 i lt n-2 i)
- System.out.print()
- for (int j 1 j lt n-2 j)
- System.out.print( )
- System.out.println()
-
- for (i 1 iltn i) System.out.print()
- System.out.println()
- Output
-
-
-
-
-
-
What is the body? Counter controlled loop
30- Code
- nkeyboard.nextInt() // try 6!
- for (int i 1 i lt n i)
- for (int j 1 j lt i j)
- System.out.print("")
-
- System.out.println()
-
- Output
-
-
-
-
-
-
i each line!
31- Code
- nkeyboard.nextInt() // try 6!
- for (int i 1 i lt n i)
- for (int j 1 j lt i j)
- System.out.print(i)
-
- System.out.println()
-
- Output
- 1
- 22
- 333
- 4444
- 55555
- 666666
How many numbers each line? What are they?
32- Code
- nkeyboard.nextInt() // try 5!
- for (int i 1 i lt n i)
- for (int j 1 j lt (n - i) j)
- System.out.print(" ")
-
- for (int k 1 k lt i k)
- System.out.print(i)
-
- System.out.println()
-
- Output
- 1
- 22
- 333
- 4444
- 55555
Space and numbers?
33Controlling Event of Loop Iterations
- Otherwise (unknown or unclear), the loop is
called a event-controlled loop. - Use a while loop or a do-while loop for an easy
checkpoint development. - Asking the user before each iteration if it is
time to end the loop is called the
ask-before-iterating technique. - Appropriate status update (or event initializing)
for a sequence of iterations
34(No Transcript)
35- Finds and prints a number's first factor other
than 1 - int n keyboard.nextInt() // try 91
- int f 2
- while (n f ! 0)
- f
-
- System.out.println("First factor" f)
- Sample run
- First factor7
Body exploring the factor. When/what to stop
the loop? n f 0 divisible! The range of
f? Initialization of f? The change of f?
36- Write a program that will repeatedly prompt the
user to type a number until the user types a
non-negative number, then square it. - Example log
- Type a non-negative integer -5
- Invalid number, try again -1
- Invalid number, try again -235
- Invalid number, try again -87
- Invalid number, try again 11
- 11 squared is 121
37Body trying different value of n. When/what to
stop the loop? ngt 0 non-negative! The range of
n? -gt any (since nlt0) Initialization of n? The
change of n?
- System.out.print("Type a non-negative integer
") - int n keyboard.nextInt()
- while (n lt 0)
- System.out.print("Invalid number, try again
") - n keyboard.nextInt()
-
- int square n n
- System.out.println(n " squared is " square)
- Notice that the number variable had to be
declared outside the while loop in order to
remain in scope.
38- Write a class named DigitSum that reads an
integer from the user and prints the sum of the
digits of that number. You may assume that the
number is non-negative. - Example
- Enter a nonnegative number 29107
- prints out 19 (i.e.,29107 )
- Hint Use the operator to extract the last
digit of a number. If we do this repeatedly,
when should we stop?
39Body adding the last digit and extracting that
from the original number (for next
round) When/what to stop? All digits are
counted! n lt 0 no more need to count! The
change of n? -gt make the extraction
valid Initialization?
- import java.util.Scanner
- public class DigitSum
- public static void main(String args)
- Scanner keyboard new Scanner(System.in)
- int n keyboard.nextInt()
- int sum 0
- while (n gt 0)
- sum n 10 // add last digit to sum
- n n / 10 // remove last digit
-
- System.out.println(sum sum)
-
40Body try all possible numbers k, count k (sum)
only when n is divisible by k. When/what to stop?
Kgtn (trial from 1 is over) The range of k? -gt 1
to n Change? k Initialization?
- Write a program named CountFactors that reads in
an integer and displays its number of factors. - For example, if the user enters 60, CountFactors
displays 12 because 1, 2, 3, 4, 5, 6, 10, 12, 15,
20, 30, and 60 are all factors of 60.
Scanner keyboard new Scanner(System.in)
int n keyboard.nextInt() int sum 0, k
? while ( )
System.out.println(sum sum)
41- Scanner keyboard new Scanner(System.in)int n
keyboard.nextInt()int k 1int sum
0while (kltn) if(nk0) sum
kSystem.out.print("sum " sum)
42- Exercise
- population
- http//www.cis.temple.edu/jiang/6billion.exe
- TV purchase
- http//www.cis.temple.edu/jiang/tv563.exe
- 1248...
- http//www.cis.temple.edu/jiang/1_2_4.exe
- 1234...99
- http//www.cis.temple.edu/jiang/1to99.exe
43(No Transcript)
44Solution
45- Ex 12
- http//www.cs.wcupa.edu/zjiang/141_ex12.pdf
- Ex 13
- http//www.cs.wcupa.edu/zjiang/141_ex13.pdf
46- File writing, page 237-240
- Filename
- PringWriter
- Println
- Close
- Sample, code 4-17, page 237
47- Appending data to a (existing) file
- FileWriter (, true), page 240
48- File Reading, page 241-245
- File
- Scanner
- nextXXXX( )
- close
- Sample, code 4-18, page 242.
49- Detecting the end of a file
- hasNext
- Code 4-19, page 245.
- Detecting the existence of a file
- exists
- Code 4-21, page 249.
50- Random number generator
- randomNumbers.nextXXX( )
- Sample, code 4-23, page 253.
51- Objects of the Random class generate
pseudo-random numbers. - Class Random is found in the java.util package.
- import java.util.
- The methods of a Random object