Title: COMP 110 Even more loops and stuff
1COMP 110Even more loops and stuff
- Luv Kohli
- September 17, 2008
- MWF 2-250 pm
- Sitterson 014
1
2Announcements
- Microsoft info session tomorrow night,
530pm-630pm, SN014 - Free food, bring résumé for chance to win prizes
- Lab 3 due Friday, 2pm
- Remember to print out your modifyFace() method
and hand it in
2
3Policy and such
- YOU are responsible for making sure assignments
are turned in correctly - Read each assignment EARLY and COMPLETELY
- Start EARLY and ask questions EARLY if something
doesnt make sense - Come to office hours
- When I ask in-class questions, raise your hand
4Questions?
4
5Today in COMP 110
- Go over Lab 2
- More about loops
6Lab 2
- Convert to uppercase letters
- toUpperCase() method
- Print characters at positions 0 and 5
- charAt() method
- Substring starting at 0 and ending at 5
- substring() method
- Without first word
- indexOf() and substring() methods
W h a t i s y o u r n a m e ?
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
7Lab 2
- Convert to uppercase letters
- JOptionPane.showMessageDialog(null,
inputString.toUpperCase())
W H A T I S Y O U R N A M E ?
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
8Lab 2
- Print characters at positions 0 and 5
- JOptionPane.showMessageDialog(null,inputString.ch
arAt(0) ", " inputString.charAt(5))
W h a t i s y o u r n a m e ?
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
9Lab 2
- Substring starting at 0 and ending at 5
- JOptionPane.showMessageDialog(null,
inputString.substring(0, 6))
W h a t i s y o u r n a m e ?
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
10Lab 2
- Without first word
- JOptionPane.showMessageDialog(null,inputString.su
bstring(inputString.indexOf( ) 1))
W h a t i s y o u r n a m e ?
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
11Review Types of Loops
- while
- Safest choice
- Not always most elegant
- do-while
- Loop iterates AT LEAST once
- for
- Similar to while, but often more convenient syntax
11
12Review Using a while loop
- int n 1
- while (n lt 10)
-
- System.out.println(n)
- n n 1
13Review Using a do-while loop
- int n 1
- do
-
- System.out.println(n)
- n n 1
- while (n lt 10)
Dont forget the semicolon!
14Review Using a for loop
- int n
- for (n 1 n lt 10 n)
-
- System.out.println(n)
-
15for loop and the comma operator
- Used to perform more than one initializing action
or more than one update action - int n
- int product
- for (n 1, product 1 n lt 5 n)
-
- product product n
-
- System.out.println(product)
16The break statement
- Where have we seen break before?
17The break statement
- Causes the immediately enclosing switch statement
or loop to end the remainder of the loop (or
switch) is not executed - Why might you want a loop to end early?
18The break statement
- Example You can buy up to 5 items, but you have
a budget of 100. Buy items until you have
exceeded your budget.
19The break statement
- for (int item 1 item lt 5 item)
-
- System.out.print(Enter cost of item
item ) - amount keyboard.nextDouble()
- total total amount
- if (total gt 100)
-
- System.out.println(You spent all your
money.) - break
-
- System.out.println(Your total so far is
total) -
- System.out.println(You spent total)
20Friday
20