Nested Structures Chapters 3 & 4. Outline n Conditionals inside loops n Conditionals inside conditionals n If-else-if controls n Loops inside loops n.

Slides:



Advertisements
Similar presentations
Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
Advertisements

CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Repeating Actions The “while” and “for” Controls Sections 4.1 & 4.2 (Also: Special Assignment Operators and Constants)
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 2 - Algorithms and Design
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Repeating Actions The “while” and “for” Controls Sections 4.1 & 4.2 (Also: Special Assignment Operators, Constants, and Switching int and double)
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Chapter 5 Loops.
Flow of Control Part 1: Selection
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping with.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 5: Structured Programming
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 5: Control Structures II
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
CPS120 Introduction to Computer Science Iteration (Looping)
Decisions, Decisions, Decisions Conditional Statements In Java.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
The for loop.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Review of CSCI 1226 What You Should Already Know.
Control Statements: Part1  if, if…else, switch 1.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Chapter 5: Control Structures II
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
CiS 260: App Dev I Chapter 4: Control Structures II.
Repeating Actions The "while" and "for" Controls Sections 4.1 & 4.2
Lecture 07 More Repetition Richard Gesick.
Flow Control in Java.
Outline Altering flow of control Boolean expressions
The “if” Control Sections 3.1 & 3.2
Module 4 Loops.
Control Statements Loops.
Control Statements Loops.
Presentation transcript:

Nested Structures Chapters 3 & 4

Outline n Conditionals inside loops n Conditionals inside conditionals n If-else-if controls n Loops inside loops n The switch control n The do-while loop

Program Flow n Beginning: Sequential flow first command, second, third, …, lastfirst command, second, third, …, last n Next: Conditional execution one-way: if (…) { … }one-way: if (…) { … } two-way: if (…) { … } else { … }two-way: if (…) { … } else { … } n Loops: indefinite: while (…) { … }indefinite: while (…) { … } definite: for (…; …; …) { … }definite: for (…; …; …) { … }

Inside the Conditional / Loop n Sequential flow first command, second, third, …, lastfirst command, second, third, …, last n But can put anything inside the {} if, if-else, while, for, … (other things, too!)if, if-else, while, for, … (other things, too!) n And then you can put anything inside those as deep as you want to goas deep as you want to go (but usually not more than three levels)(but usually not more than three levels)

Loops with Conditionals n For each positive integer the user enters, say whether it’s divisible by 5 stop when enter a negative numberstop when enter a negative number System.out.println(“Enter some numbers below: ”); num = kbd.nextInt(); while (num >= 0) { // TODO: say whether num is divisible by 5 // TODO: say whether num is divisible by 5 // get next number // get next number num = kbd.nextInt(); num = kbd.nextInt();}kbd.nextLine();

Expected Output n Pattern: Print, Read, repeat ((Divisible OR NotDivisible), Read)Print, Read, repeat ((Divisible OR NotDivisible), Read) Enter some numbers below: is NOT divisible by is divisible by is NOT divisible by 5. Print Read NotDivisible Read Divisible Read NotDivisible Read Or: Print, repeat (Read, (Divisible OR NotDivisible)), Read But then we wouldn’t know when to stop looping!

Loops with Conditionals n whether divisible  if-else control while (num >= 0) { // say whether num is divisible by 5 // say whether num is divisible by 5 if (num % 5 == 0) { if (num % 5 == 0) { System.out.println(num + “ is divisible by 5.”); System.out.println(num + “ is divisible by 5.”); } else { } else { System.out.println(num + “ is not divisible by 5.”); System.out.println(num + “ is not divisible by 5.”); } // get next number // get next number num = kbd.nextInt(); num = kbd.nextInt();}

Note the Indentation n Each level  4 more spaces while (num >= 0) { // say whether num is divisible by 5 // say whether num is divisible by 5 if (num % 5 == 0) { if (num % 5 == 0) { System.out.println(num + “ is divisible by 5.”); System.out.println(num + “ is divisible by 5.”); } else { } else { System.out.println(num + “ is not divisible by 5.”); System.out.println(num + “ is not divisible by 5.”); } // get next number // get next number num = kbd.nextInt(); num = kbd.nextInt();} Remember the Format command!

Loop + Conditional Execution n whether divisible  if-else control while (num >= 0) { // say whether num is divisible by 5 // say whether num is divisible by 5 if (num % 5 == 0) { if (num % 5 == 0) { System.out.println(num + “ is divisible by 5.”); System.out.println(num + “ is divisible by 5.”); } else { } else { System.out.println(num + “ is not divisible by 5.”); System.out.println(num + “ is not divisible by 5.”); } // get next number // get next number num = kbd.nextInt(); num = kbd.nextInt();} 100 is divisible by 5 80 is divisible by 5 95 is divisible by 5 78 is NOT divisible by 5 84 is NOT divisible by 5 90 is divisible by 5

Exercise n Write a loop to read in six grades print “Good work!” if that grade is 90+print “Good work!” if that grade is 90+ n Use a for loop (why?) Enter your grade on A1: 100 Good work! Enter your grade on A2: 80 Enter your grade on A3: 95 Good work! Enter your grade on A4: 78 Enter your grade on A5: 84 Enter your grade on A6: 90 Good work!

if-else inside if-else What time is it? 9 pm Good evening! What time is it? 9 am Good morning! What time is it? 3 pm Good afternoon! What time is it? 3 am Good grief! Evening: 6pm to 11pm. Afternoon: 12pm to 5pm. Morning: 7am to 11am. 12am to 6 am. pmam A E M G

Get the Time // create variables Scanner kbd = new Scanner(System.in); int hour; String amPM; // get the time System.out.println(“What time is it?”); hour = kbd.nextInt(); // fix weird clock if (hour == 12) { hour = 0; } // NOTE: read next word in lower case amPM = kbd.next().toLowerCase(); kbd.nextLine();// read the Enter key!

Print the Message // print the appropriate message if ( amPM.equals(“pm”) ) { if ( hour < 6 ) { if ( hour < 6 ) { System.out.println(“Good afternoon!”); System.out.println(“Good afternoon!”); } else { } else { System.out.println(“Good evening!”); System.out.println(“Good evening!”); } } else { if ( hour > 6 ) { if ( hour > 6 ) { System.out.println(“Good morning!”); System.out.println(“Good morning!”); } else { } else { System.out.println(“Good grief!”); System.out.println(“Good grief!”); }} A E M G

9 AM Message // print the appropriate message if ( amPM.equals(“pm”) ) { if ( hour < 6 ) { if ( hour < 6 ) { System.out.println(“Good afternoon!”); System.out.println(“Good afternoon!”); } else { } else { System.out.println(“Good evening!”); System.out.println(“Good evening!”); } } else { if ( hour > 6 ) { if ( hour > 6 ) { System.out.println(“Good morning!”); System.out.println(“Good morning!”); } else { } else { System.out.println(“Good grief!”); System.out.println(“Good grief!”); }} “am”.equals(“pm”)? NO 9 > 6? YES

Note Indentation // print the appropriate message if ( amPM.equals(“pm”) ) { if ( hour < 6 ) { if ( hour < 6 ) { System.out.println(“Good afternoon!”); System.out.println(“Good afternoon!”); } else { } else { System.out.println(“Good evening!”); System.out.println(“Good evening!”); } } else { if ( hour > 6 ) { if ( hour > 6 ) { System.out.println(“Good morning!”); System.out.println(“Good morning!”); } else { } else { System.out.println(“Good grief!”); System.out.println(“Good grief!”); }} Remember the Format command!

if inside if n You can put if controls inside if controls if first condition is true, test second conditionif first condition is true, test second conditionSystem.out.println(“A”); if (condition1) { System.out.println(“B”); System.out.println(“B”); if (condition2) { if (condition2) { System.out.println(“C”); System.out.println(“C”); }} possible output: A AB ABC pattern: always A sometimes B when B, sometimes C

Eligible for Pension n Who is eligible for a pension seniors (age is 65+)seniors (age is 65+) disabled people 55+ who live on their owndisabled people 55+ who live on their own n Only ask question if need to know ask ageask age if , ask if disabledif , ask if disabled »if disabled, ask if live on own

Eligible for Pension // ask age System.out.print(“How old are you? ”); age = kbd.nextInt(); kbd.nextLine(); eligible = (age >= 65); // if , ask if disabled if (age >= 55 && !eligible) { System.out.print(“Are you disabled? ”); System.out.print(“Are you disabled? ”); answer = kbd.nextLine().toLowerCase(); answer = kbd.nextLine().toLowerCase(); // if disabled, ask if live on own // if disabled, ask if live on own if (answer.startsWith(“y”)) { if (answer.startsWith(“y”)) { System.out.print(“Do you live on your own? ”); System.out.print(“Do you live on your own? ”); answer = kbd.nextLine().toLowerCase(); answer = kbd.nextLine().toLowerCase(); } eligible = answer.startsWith(“y”); eligible = answer.startsWith(“y”);}

Exercise n Write nested if/if-else to generate: either “A”, “AB”, “ABC” or “ABD”either “A”, “AB”, “ABC” or “ABD” either “A”, “ABC” or “AC”either “A”, “ABC” or “AC” either “A”, “ABC”, or “D”either “A”, “ABC”, or “D”

Three-Way Choice n Exactly one of three options either “A”, “B”, or “C”either “A”, “B”, or “C” if (age < 18) { System.out.print(“Child”);} if (age >= 18 && age = 18 && age < 65) {System.out.print(“Adult”);} if (age >= 65) { System.out.print(“Senior”);} Child age is 15 Adult age is 25 Senior age is 65

Sequential If and If-Else n Be careful mixing if with if-else else parts go with each if separatelyelse parts go with each if separately if (age < 18) { System.out.print(“Child”);} if (age >= 18 && age = 18 && age < 65) {System.out.print(“Adult”); } else { System.out.print(“Senior”);} What happens when age = 15? Adult age is 25 Senior age is 65

If-Else Inside Else n If-Else inside an Else only do Adult/Senior choice if Child not chosenonly do Adult/Senior choice if Child not chosen if (age < 18) { System.out.print(“Child”); } else { if (age >= 18 && age = 18 && age < 65) { System.out.print(“Adult”); System.out.print(“Adult”); } else { } else { System.out.print(“Senior”); System.out.print(“Senior”); }} Adult age is 25 Senior age is 65 What happens when age = 15?

If-Else Inside Else n If-Else inside an Else usually written this way (3-way choice)usually written this way (3-way choice) if (age < 18) { System.out.print(“Child”); } else if (age >= 18 && age = 18 && age < 65) {System.out.print(“Adult”); } else { System.out.print(“Senior”);} Adult age is 25 Senior age is 65 What happens when age = 15?

Cascading If Statements n Simplified conditions already know age >= 18 in part 2already know age >= 18 in part 2 if (age < 18) { System.out.print(“Child”); } else if (age < 65) { System.out.print(“Adult”); } else { System.out.print(“Senior”);} Adult age is 25 Senior age is 65 What happens when age = 15?

Multi-Way Choice A or B or C or... or Z if (...) { doA(); } else if (...) { doB();... } else { doZ();} A or B or... or Z or none of the above if (...) { doA(); } else if (...) { doB();... doZ();} If we didn’t do any of the others, then we’ll do Z for sure If we didn’t do any of the others, we still might not do Z

Exercises n Write “Good morning” if time < 12, “Good afternoon” if 12  time < 17, “Good evening” otherwise n Write “Small” if size is 1, “Medium” if size is 2, “Large” if size is 3, “X-Large” if size is 4, and “XX-Large” if size is 5 don’t write anything if size is not 1..5don’t write anything if size is not 1..5

Using Braces n Always use braces on if controls if (…) { … }if (…) { … } if (…) { … } else { … }if (…) { … } else { … } n Indent the body of the conditional if (…) { // this part is called the “head” … // this part is called the “body” }if (…) { // this part is called the “head” … // this part is called the “body” }

Optional Braces n Sometimes braces can be left off n Braces can be left of if (and only if) there is exactly one conditional command if (thisIsTrue) doThat(); if (thisIsTrue) doThat(); n Use the braces then anyway it’ll save you from making some mistakes!it’ll save you from making some mistakes!

Mistake #1 n Thinking it’s the indentation that matters this code generates an error message:this code generates an error message: if (thisIsTrue) doThis(); andThat(); else thisOther();if (thisIsTrue) doThis(); andThat(); else thisOther(); “else without if”“else without if”

Mistake #1a n Putting braces around everything after if this code generates the same error message:this code generates the same error message: if (thisIsTrue) { doThis(); andThat(); else thisOther(); }if (thisIsTrue) { doThis(); andThat(); else thisOther(); } “else without if”“else without if”

Mistake #2 n Connecting else to wrong if if (n1 > 0) if (n2 > 0) if (n2 > 0) System.out.println(“Both positive”); System.out.println(“Both positive”);else System.out.println(“Ummm?”); System.out.println(“Ummm?”); when does “Ummm?” get printed?when does “Ummm?” get printed? »n1 <= 0? NO »n2 <= 0? YES

Use Braces! if (n1 > 0) { if (n2 > 0) { if (n2 > 0) { System.out.println(“Both positive”); System.out.println(“Both positive”); System.out.println(“For sure”); System.out.println(“For sure”); } } else { System.out.println(“n1 is negative”); System.out.println(“n1 is negative”);}

Exercise n What are the possible outputs of this code? System.out.print(“A”); if (…) { System.out.print(“B”); System.out.print(“B”); if (…) if (…) System.out.print(“C”); System.out.print(“C”); System.out.print(“D”); System.out.print(“D”); } else System.out.print(“E”); System.out.println(“F”); Are you sure? Maybe you should “format” the code and look again!

Loops inside Loops n Draw four lines with 20 stars on each line four lines all the same  a “for” loopfour lines all the same  a “for” loop each line:each line: »twenty stars, all the same  another “for” loop »line ends after the twenty stars ********************

Drawing One Line n Need a line of 20 (or whatever) stars 20 times (print out one star)20 times (print out one star) then end the linethen end the line // for each of the 20 stars for (int star = 1; star <= 20; ++star) { // print that star // print that star System.out.print(“*”); System.out.print(“*”);} // end that line (after all the stars) System.out.println(); ********************

Drawing Multiple Lines n Need 4 lines 4 times (print out one line)4 times (print out one line) // for each of the four lines for (int line = 1; line <= 4; ++line) { // print one line // print one line …// same code as before …// same code as before …// including the loop! …// including the loop!} ********************

Loops inside Loops n Draw four lines with 20 stars on each line // for each of the four lines for (int line = 1; line <= 4; ++line) { // for each of the 20 stars // for each of the 20 stars for (int star = 1; star <= 20; ++star) { for (int star = 1; star <= 20; ++star) { // print that star // print that star System.out.print(“*”); System.out.print(“*”); } // end that line (after all the stars) // end that line (after all the stars) System.out.println(); System.out.println();}

Inner and Outer Loops n One loop (inner) inside another (outer) // print a rectangle for (int line = 1; line <= height; line++) { // print a line of stars // print a line of stars for (int star = 1; star <= width; star++) { // print a star System.out.print(‘*’);}System.out.println();} inner loop outer loop Notice that it doesn’t matter what you name the loop control variable, so long as you stick with the one name for each loop.

Identify the Pieces and Patterns n Write nested loops to read numbers (integers) until a negative number is read print that many “Hello”s on one lineprint that many “Hello”s on one line How many hellos? 5 Hello! Hello! Hello! Hello! Hello! How many hellos? 3 Hello! Hello! Hello! How many hellos? 7 Hello! Hello! Hello! Hello! Hello! Hello! Hello! How many hellos? -1

Inner and Outer Loops Outer loop n Read numbers until a negative number is read n while loop: Inner loop n Print out a given number of “Hello”s told number before starttold number before start n for loop: S.o.p.(“How many Hellos? ”); num = kbd.nextInt(); kbd.nextLine(); while (num >= 0) { // inner loop goes HERE S.o.p.(“How many Hellos? ”); num = kbd.nextInt(); kbd.nextLine(); } for (int i = 1; i <= num; ++i) { S.o.p.(“Hello! ”); } System.out.println(); Change S.o.p. to System.out.print

Identify the Pieces and Patterns n Write nested loops to read three valid assignment scores and calculate their average valid = between 0 and 100 (inclusive)valid = between 0 and 100 (inclusive) Enter your A1 score: 67 Enter your A2 score: 789 That’s not valid. Try again! Enter your A2 score: 79 Enter your A3 score: 1000 That’s not valid. Try again! Enter your A3 score: 100 Your average: 82.0%

Declaring Variables in Loops n You can actually declare variables inside loop (and conditional) bodies for (a = 1; a <= NUM_ASGN; a++) { System.out.println(“Enter grade:”); System.out.println(“Enter grade:”); int grade = kbd.nextInt(); int grade = kbd.nextInt(); kbd.nextLine(); kbd.nextLine(); sum += grade; sum += grade;} that variable belongs to the loop!that variable belongs to the loop! cannot be used outside the loopcannot be used outside the loop

“Scope” of a Variable n Cannot use variable outside its scope for (a = 1; a <= NUM_ASGN; a++) { System.out.println(“Enter grade: ”); System.out.println(“Enter grade: ”); int grade = kbd.nextInt(); int grade = kbd.nextInt(); kbd.nextLine(); kbd.nextLine(); sum += grade; sum += grade;} S.o.pln(“Last grade was ” + grade); OutOfScope.java:30: Cannot find symbol symbol : variable grade The “scope” of grade

Declaring Loop Control Variable n Often declare LCV of for loop in the loop for (int aa = 1; aa <= NUM_ASGN; aa++) { System.out.println(“Enter grade: ”); System.out.println(“Enter grade: ”); int grade = kbd.nextInt(); int grade = kbd.nextInt(); kbd.nextLine(); kbd.nextLine(); sum += grade; sum += grade;} S.o.pln(“Last assignment was ” + aa); OutOfScope.java:41: Cannot find symbol symbol : variable aa The “scope” of aa

Using Local Variables n Good idea if the variable is not supposed to be used outside a narrow context outside the grade reading loop, who needs to know any particular grade?outside the grade reading loop, who needs to know any particular grade? after counting to 6 (or whatever), who needs to know what number we’re on?after counting to 6 (or whatever), who needs to know what number we’re on? n If you need the variable after the loop, then you must declare it before the loop and if you don’t need it after, declare it inside!and if you don’t need it after, declare it inside!

On Doing One of Many Things n Can use cascading if controls to do exactly one of many things or at most one of many things (leave off else)or at most one of many things (leave off else) n switch control can sometimes be used if the one thing to do depends on the value of only one variable, and the values are constants:if the one thing to do depends on the value of only one variable, and the values are constants: if (var == 1) { } else if (var == 2 || var == 3) { } else if (var == 4) { …

Switch Control This: if (var == 1) { Sop(“One”); } else if (var == 2 || var == 3) { Sop(“2-3”); } else { Sop(“More”); } Becomes this: switch (var) { case 1: Sop(“One”); break; case 2: case 3: Sop(“2-3”); break; default: Sop(“More”); break; } One variable all the way down (var) (var == n) becomes case n: else becomes default break after every action

One Final Loop Type n In addition to while and for: do-while used when loop must iterate at least onceused when loop must iterate at least once all the data must be processedall the data must be processed know you’re done when processingknow you’re done when processing »example: add up numbers till get to 100 Sop(“Enter some numbers:”); do { num = kbd.nextInt(); num = kbd.nextInt(); sum += num; sum += num; } while (sum < 100); Sop(“Sum is ” + sum); Enter some numbers: Sum is 102

On switch and do-while n People planning to go on to 1228 should learn how to use them n People going on to 1227 (or stopping) only need to know they exist

Questions