INTERMEDIATE PROGRAMMING USING JAVA

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Chapter 5 Loops.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Chapter 4: Control Structures II
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Slides by Evan Gallagher
Java Fundamentals 4.
INTERMEDIATE PROGRAMMING USING JAVA
Loops.
The switch Statement, and Introduction to Looping
Chapter 5: Control Structures II
Loop Structures.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Topic 5 for Loops -Arthur Schopenhauer
Repetition-Counter control Loop
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5: Control Structures II
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
LOOPS.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Module 4 Loops.
Control Statements Loops.
Chapter 6: Repetition Statements
Loop Strategies Repetition Playbook.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Repetition Statements (Loops) - 2
Control Statements Loops.
Repetition Statements
Java LESSON 3 Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Outline Boolean Expressions The if Statement Comparing Data
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

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