Download presentation
Presentation is loading. Please wait.
Published byKelley Carroll Modified over 9 years ago
1
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1
2
Announcements Program 1 due Lab 2 due Friday, 2pm Chapters 1 and 2 review solutions posted on web site Program 2 assigned today 2
3
Questions? 3
4
String’s substring method 4 UNCisGreat 01234567891011 String output = myString.substring(1, 8);
5
String’s substring method 5 UNCisGreat 01234567891011 String output = myString.substring(1, 8);
6
Today in COMP 110 switch statements loops
7
Java Example: if/else Is input greater than 10? YesNo 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"); } } }
8
Tracing if/else 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;
9
Example problem Write a program that takes as input your year in college (as an integer) and outputs your year as freshman, sophomore, junior, or senior
10
Example problem Which year? 1 Prompt user for year freshman sophomore 23 junior 4 senior Next step
11
With if/else if (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”);
12
switch statement switch(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
13
switch statement syntax switch (controlling expression) { case case label: statements; break; case case label: statements; break; default: statements; break; }
14
switch statement details Only int and char can be used in the controlling expression Case labels must be of same type as controlling expression The break statement ends the switch statement, go to the next step outside the braces in the code The default case is optional
15
Loops Often you need to repeat an action in a program Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich
16
Loops Loop: part of a program that repeats Body: statements being repeated Iteration: each repetition of body Stopping condition Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich
17
Types of loops while do-while for
18
while loop start; while (not enough sandwiches) { make a sandwich; } distribute sandwiches; Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich
19
while loop syntax while (boolean expression) { statements; }
20
while 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 No Yes Make sandwich
21
while loop Start Enough sandwiches? Distribute sandwiches No Yes Do a backflip Infinite loop!
22
Using a while loop Output 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
23
Using a while loop int n = 1; while (n <= 10) { System.out.println(n); n = n + 1; }
24
Program 2 Due Wednesday, September 24 at 2pm NEW: In addition to emailing your program, print out your program and hand it in
25
Friday Lab 2 due Friday, 2pm Bring laptop and textbook 25
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.