comp 110 switch statements and while loops luv kohli september 10, 2008 mwf 2-2:50 pm sitterson 014...
TRANSCRIPT
COMP 110COMP 110switchswitch statements and statements and whilewhile loops loops
Luv KohliSeptember 10, 2008
MWF 2-2:50 pmSitterson 014
1
AnnouncementsAnnouncementsProgram 1 due
Lab 2 due Friday, 2pm
Chapters 1 and 2 review solutions posted on web site
Program 2 assigned today
2
Questions?Questions?
3
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);
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);
Today in COMP 110Today in COMP 110switch statements
loops
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"); } }}
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;
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
Example problemExample problem
Which year?1
Prompt user for year
freshman sophomore
2 3
junior
4
senior
Next step
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”);
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
switchswitch statement syntax statement syntax
switch (controlling expression){ case case label: statements; break; case case label: statements; break; default: statements; break;}
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
LoopsLoopsOften you need to repeat an action in a
programStart
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
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
Types of loopsTypes of loopswhiledo-whilefor
whilewhile loop loop
start;
while (not enough sandwiches)
{
make a sandwich;
}
distribute sandwiches;
Start
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
whilewhile loop syntax loop syntax
while (boolean expression)
{
statements;
}
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
whilewhile loop loop
Start
Enough sandwiches?
Distribute sandwiches
NoYesDo a
backflip
Infinite loop!
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
Using a Using a whilewhile loop loop
int n = 1;
while (n <= 10)
{
System.out.println(n);
n = n + 1;
}
Program 2Program 2Due Wednesday, September 24 at 2pm
NEW: In addition to emailing your program, print out your program and hand it in
FridayFridayLab 2 due Friday, 2pm
Bring laptop and textbook
25