comp 110 switch statements and while loops luv kohli september 10, 2008 mwf 2-2:50 pm sitterson 014...
TRANSCRIPT
![Page 1: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/1.jpg)
COMP 110COMP 110switchswitch statements and statements and whilewhile loops loops
Luv KohliSeptember 10, 2008
MWF 2-2:50 pmSitterson 014
1
![Page 2: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/2.jpg)
AnnouncementsAnnouncementsProgram 1 due
Lab 2 due Friday, 2pm
Chapters 1 and 2 review solutions posted on web site
Program 2 assigned today
2
![Page 3: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/3.jpg)
Questions?Questions?
3
![Page 4: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/4.jpg)
String’s substring methodString’s substring method
4
U N C i s G r e a t
0 1 2 3 4 5 6 7 8 9 10 11
String output = myString.substring(1, 8);
![Page 5: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/5.jpg)
String’s substring methodString’s substring method
5
U N C i s G r e a t
0 1 2 3 4 5 6 7 8 9 10 11
String output = myString.substring(1, 8);
![Page 6: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/6.jpg)
Today in COMP 110Today in COMP 110switch statements
loops
![Page 7: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/7.jpg)
Java Example: Java Example: ifif//elseelse
Is input greater
than 10?Yes No
Prompt user for integer
Print: “big
number”
Print: “small
number”
import java.util.*;
public class FlowChart{ public static void main(String[] args) { System.out.println("Give me an integer:"); Scanner keyboard = new Scanner(System.in); int inputInt = keyboard.nextInt(); if (inputInt > 10) { System.out.println("big number"); } else { System.out.println("small number"); } }}
![Page 8: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/8.jpg)
Tracing Tracing ifif//elseelse
int days = 0;
if (input < 6)
System.out.print(“I worked ” + input +
“ days this week”);
else
{
days = input – 5;
System.out.print(“I worked ” + days +
“ days of overtime”);
}
int input = 5;
int input = 6;
![Page 9: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/9.jpg)
Example problemExample problemWrite a program that takes as input your
year in college (as an integer) and outputs your year as freshman, sophomore, junior, or senior
![Page 10: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/10.jpg)
Example problemExample problem
Which year?1
Prompt user for year
freshman sophomore
2 3
junior
4
senior
Next step
![Page 11: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/11.jpg)
With With ifif//elseelseif (year == 1)
System.out.println(“freshman”);
else if (year == 2)
System.out.println(“sophomore”);
else if (year == 3)
System.out.println(“junior”);
else if (year == 4)
System.out.println(“senior”);
![Page 12: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/12.jpg)
switchswitch statement statementswitch(year){ case 1: System.out.println(“freshman”); break; case 2: System.out.println(“sophomore”); break; case 3: System.out.println(“junior”); break; case 4: System.out.println(“senior”); break; default: System.out.println(“unknown”); break;}
Controlling expression
Case labels
Break statements
Default case:all other values
![Page 13: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/13.jpg)
switchswitch statement syntax statement syntax
switch (controlling expression){ case case label: statements; break; case case label: statements; break; default: statements; break;}
![Page 14: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/14.jpg)
switchswitch statement details statement detailsOnly int and char can be used in the
controlling expressionCase labels must be of same type as
controlling expressionThe break statement ends the switch
statement, go to the next step outside the braces in the code
The default case is optional
![Page 15: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/15.jpg)
LoopsLoopsOften you need to repeat an action in a
programStart
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
![Page 16: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/16.jpg)
LoopsLoops Loop: part of a program that
repeats
Body: statements being repeated
Iteration: each repetition of body
Stopping condition
Start
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
![Page 17: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/17.jpg)
Types of loopsTypes of loopswhiledo-whilefor
![Page 18: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/18.jpg)
whilewhile loop loop
start;
while (not enough sandwiches)
{
make a sandwich;
}
distribute sandwiches;
Start
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
![Page 19: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/19.jpg)
whilewhile loop syntax loop syntax
while (boolean expression)
{
statements;
}
![Page 20: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/20.jpg)
whilewhile loop loop
The stopping condition is a boolean expression. The body code is run if the boolean expression is true.
The body must be able to change the value of the boolean expression from true to false.
Start
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
![Page 21: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/21.jpg)
whilewhile loop loop
Start
Enough sandwiches?
Distribute sandwiches
NoYesDo a
backflip
Infinite loop!
![Page 22: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/22.jpg)
Using a Using a whilewhile loop loopOutput integers from 1 to 10
int n = 1;
System.out.println(n);
n = n + 1;
System.out.println(n);
n = n + 1;
System.out.println(n);
...
// until n == 10
![Page 23: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/23.jpg)
Using a Using a whilewhile loop loop
int n = 1;
while (n <= 10)
{
System.out.println(n);
n = n + 1;
}
![Page 24: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/24.jpg)
Program 2Program 2Due Wednesday, September 24 at 2pm
NEW: In addition to emailing your program, print out your program and hand it in
![Page 25: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f1f5503460f94c37c4a/html5/thumbnails/25.jpg)
FridayFridayLab 2 due Friday, 2pm
Bring laptop and textbook
25