Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTERMEDIATE PROGRAMMING USING JAVA

Similar presentations


Presentation on theme: "INTERMEDIATE PROGRAMMING USING JAVA"— Presentation transcript:

1 INTERMEDIATE PROGRAMMING USING JAVA
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2016 © – All Rights Reserved

2 The While Loop © – All Rights Reserved

3 Store data via Identifiers and Variables
Get data in Your program Control Structures Get data out Statements and Expressions © – All Rights Reserved

4 Control Statements Linear Execution Conditional Execution
Iterative Execution © – All Rights Reserved

5 The While Loop A Real-life example of WHILE loop
2012 Presidential Towers Stair Climb Video ( © – All Rights Reserved

6 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!”); © – All Rights Reserved

7 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 ? © – All Rights Reserved

8 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? © – All Rights Reserved

9 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… © – All Rights Reserved

10 The Do-While Loop © – All Rights Reserved

11 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); © – All Rights Reserved

12 The “Conventional” FOR loop
© – All Rights Reserved

13 Control Statements Linear Execution Conditional Execution
Iterative Execution © – All Rights Reserved

14 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 © – All Rights Reserved

15 Nested Loops © – All Rights Reserved

16 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 © – All Rights Reserved

17 The “Enhanced” FOR loop
© – All Rights Reserved

18 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; © – All Rights Reserved

19 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 © – All Rights Reserved

20 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 © – All Rights Reserved

21 Syntax Errors Logic Errors Run-time Errors
© – All Rights Reserved

22 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 = , 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 } © – All Rights Reserved

23 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) © – All Rights Reserved

24 Any Questions? © – All Rights Reserved


Download ppt "INTERMEDIATE PROGRAMMING USING JAVA"

Similar presentations


Ads by Google