COMP 110 Even more loops and stuff - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 110 Even more loops and stuff

Description:

Free food, bring r sum for chance to win prizes. Lab 3 due Friday, 2pm ... YOU are responsible for making sure assignments are turned in correctly ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 21
Provided by: csU49
Learn more at: http://www.cs.unc.edu
Category:
Tags: comp | even | loops | more | prizes | stuff

less

Transcript and Presenter's Notes

Title: COMP 110 Even more loops and stuff


1
COMP 110Even more loops and stuff
  • Luv Kohli
  • September 17, 2008
  • MWF 2-250 pm
  • Sitterson 014

1
2
Announcements
  • 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
3
Policy 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

4
Questions?
4
5
Today in COMP 110
  • Go over Lab 2
  • More about loops

6
Lab 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
7
Lab 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
8
Lab 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
9
Lab 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
10
Lab 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
11
Review 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
12
Review Using a while loop
  • int n 1
  • while (n lt 10)
  • System.out.println(n)
  • n n 1

13
Review Using a do-while loop
  • int n 1
  • do
  • System.out.println(n)
  • n n 1
  • while (n lt 10)

Dont forget the semicolon!
14
Review Using a for loop
  • int n
  • for (n 1 n lt 10 n)
  • System.out.println(n)

15
for 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)

16
The break statement
  • Where have we seen break before?

17
The 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?

18
The 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.

19
The 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)

20
Friday
  • Lab 3 due, 2pm

20
Write a Comment
User Comments (0)
About PowerShow.com