INTERMEDIATE PROGRAMMING USING JAVA CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2016 © 2016 www.paulobrasko.com – All Rights Reserved
The While Loop © 2016 www.paulobrasko.com – All Rights Reserved
Store data via Identifiers and Variables Get data in Your program Control Structures Get data out Statements and Expressions © 2016 www.paulobrasko.com – All Rights Reserved
Control Statements Linear Execution Conditional Execution Iterative Execution © 2016 www.paulobrasko.com – All Rights Reserved
The While Loop A Real-life example of WHILE loop 2012 Presidential Towers Stair Climb Video (http://paulobrasko.com/cs-0401-fall-2016-pitt/course-material/videos#video07) © 2016 www.paulobrasko.com – All Rights Reserved
The While Loop While Loop Syntax: Example: while(boolean condition is true) { execute commands inside } Example: int currentFloor = 10; int topFloor = 50; while(currentFloor < topFloor) { System.out.println(“Almost there! Floor: “ + currentFloor); currentFloor++; System.out.println(“Finally here! Top floor!”); © 2016 www.paulobrasko.com – All Rights Reserved
Pay attention to infinite loops While Loop Pay attention to infinite loops int number = 0; while (number <=5); { System.out.println(“Hello”); number++; } What is wrong here ? © 2016 www.paulobrasko.com – All Rights Reserved
While Loop – A Simple Program Program main tasks: User will enter some scores After all the scores have been entered, it computes the average value flow diagram (In)valid scores When to stop collecting #s? How to compute average? significant digits? How to keep the # of entries? © 2016 www.paulobrasko.com – All Rights Reserved
Solutions for the program Possible implementations for this problem Ex5a.java Ex5b.java Remember Grasshopper: there are many possible ways to go through a forest full of trees… © 2016 www.paulobrasko.com – All Rights Reserved
The Do-While Loop © 2016 www.paulobrasko.com – All Rights Reserved
The Do-While Loop Syntax: statement(s); } while (condition); Example: int number = 5; Scanner scan = new Scanner(System.in); int numberOfGuesses = 0; System.out.println(“Enter your guess: ”); int userGuess= scan.nextInt(); if (number != userGuess) { System.out.println(“Sorry, wrong number! Try again!”); } numberOfGuesses++; } while (number != userGuess); © 2016 www.paulobrasko.com – All Rights Reserved
The “Conventional” FOR loop © 2016 www.paulobrasko.com – All Rights Reserved
Control Statements Linear Execution Conditional Execution Iterative Execution © 2016 www.paulobrasko.com – All Rights Reserved
See forexamples.java, ex5c.java, ex5d.java The for Loop The for loop takes the form: for(initialization; test; update) { statement(s); } Example: int maxNumber = 5; for(int i = 0; i < maxNumber; i++) System.out.println(“The square of “ + i + “ is “ + i*i); See forexamples.java, ex5c.java, ex5d.java © 2016 www.paulobrasko.com – All Rights Reserved
Nested Loops © 2016 www.paulobrasko.com – All Rights Reserved
Nested Loops Example: for(int i = 0; i < 3; i++) for(int j = 0; j < 4; j++) loop statements; Let’s generate the following 2D matrix 1 2 3 4 5 6 7 8 9 10 11 12 © 2016 www.paulobrasko.com – All Rights Reserved
The “Enhanced” FOR loop © 2016 www.paulobrasko.com – All Rights Reserved
The enhanced for Loop The for loop takes the form: Example: for(type tempVar : listOfVariables){ statement(s); } Example: int[] listOfGrades = { 5, 10, 8, 6, 2}; int sum = 0; for(int grade : listOfGrades) { sum += grade; double avg = sum / listOfGrades.length; © 2016 www.paulobrasko.com – All Rights Reserved
Interviewing marathon runners For all people walking on the street If it is a runner, then interview If it is not a runner, then skip to the next person walking Do I know how many people will pass? Do it until the race starts So I’ll use the While loop for it! Emergency Stop Interviews © 2016 www.paulobrasko.com – All Rights Reserved
update while condition // start // reporter greets the TV viewers boolean raceHasStarted = false; while(!raceHasStarted) { if (isARunner()) { performInterview(); } else { continue; // let’s talk about this if (isThereAnEmergency) { break; if (currentTime == raceStartTime) { raceHasStarted = true; // finish // reporter gives her final remarks. Start while (condition) Is a runner? Interview runner Any emergency? Stop Interviewing update while condition Finish © 2016 www.paulobrasko.com – All Rights Reserved
Syntax Errors Logic Errors Run-time Errors © 2016 www.paulobrasko.com – All Rights Reserved
double pi = 3.1415, volume, ratio; // or we can use Math.PI // computig the volume of a sphere V= 4/3*PI*Radius^3 double pi = 3.1415, volume, ratio; // or we can use Math.PI double radius = keyboard.nextDouble(); // computing the volume volume = 4/3 * pi * Math.pow(radius, 3); // logic error here // computing ratio between volume and radius ratio = volume / radius; // potential run-time error // computing ratio square double ratioSquare = ratio * raito; // syntax error if (ratioSquare == 3.0) { // logic error, why? // some code here } © 2016 www.paulobrasko.com – All Rights Reserved
Coding errors Syntax Errors: easy to spot either during typing (if you are using an IDE) or during compilation time. Logic Errors: sometimes very hard to find. The use of debugging tools inside an IDE can help your task in finding the problem. Run-time Errors: As a developer you must think what could go wrong and try to circumvent it with internal checks in your code before using the data (IF statements, Exceptions, etc) © 2016 www.paulobrasko.com – All Rights Reserved
Any Questions? © 2016 www.paulobrasko.com – All Rights Reserved