COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014...

25
COMP 110 COMP 110 switch switch statements and statements and while while loops loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1

Transcript of COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014...

Page 1: COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

FridayFridayLab 2 due Friday, 2pm

Bring laptop and textbook

25