Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 6 Iteration Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
Chapter 6  Iteration 1 Chapter 6 Iteration. Chapter 6  Iteration 2 Chapter Goals  To be able to program loops with while and for (sometimes do ) statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Computer Science A 5: 17/2. JCreator IDE: Integrated Development Editor Features: Compile and run from editor Manage multiple files Can look up api documentation.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
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.
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Java Prepared by Gary Langner Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled.
If Statement if (amount
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Loop Structures.
Selected Topics From Chapter 6 Iteration
LOOPS.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Chapter Six - Iteration
Chapter 7 Iteration.
Repetition Control Structure
Control structures to direct program flow
Chapter 5: Control Structures II (Repetition)
Repetition Statements
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
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.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

Datalogi A 5: 6/10

while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most commonly, the statement is a block statement (set of statements delimited by { })

Calculating the Growth of an Investment Invest $10,000, 5% interest, compounded annually YearBalance 0$10,000 1$10,500 2$11,025 3$11, $12, $12,762.82

While loop When has the bank account reached a particular balance? while (balance < targetBalance) { year++; double interest = balance * rate / 100 balance = balance + interest; }

Flow of control

Common Error: Infinite Loops int years = 0; while (years < 20) { double interest = balance * rate / 100; balance = balance + interest; } int years = 20; while (years > 0) { years++; // Oops, should have been years-- double interest = balance * rate / 100; balance = balance + interest; } Loops run forever–must kill program

Java int years = 0; while (balance < 2 * initialBalance) { years++; double interest = balance * rate / 100; balance = balance + interest; } System.out.println("The investment reached the target after " + years + " years."); Should years start at 0 or 1? Should the test be < or <=?

do Loops Executes loop body at least once: do statement while (condition); Example: Validate input double value; do { System.out.print( "Please enter a positive number: "); value = in.nextDouble(); } while (value <= 0);

do loop

Spaghetti code – not in java years=1; goto a: b: years++; a: balance+=interest; if(balance<targetBalance) goto b;

for Loops for (initialization; condition; update) statement Example: for (int i = 1; i <= n; i++){ double interest = balance * rate / 100; balance = balance + interest; }

for loops for (initialization; condition; update) statement Equivalent to initialization; while (condition) { statement; update; } Other examples: for (years = n; years > 0; years--)... for (x = -10; x <= 10; x = x + 0.5)...

for loops

Common Errors: Semicolons A semicolon that shouldn't be there sum = 0; for (i = 1; i <= 10; i++); sum = sum + i; System.out.println(sum);

Nested loops Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i <= n; i++) { // make triangle row } Make triangle row is another loop for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "\n"; Put loops together → Nested loops

Processing Sentinel Values Sentinel value: Can be used for indicating the end of a data set 0 or -1 make poor sentinels; better use Q System.out.print("Enter value, Q to quit: "); String input = in.next(); if (input.equalsIgnoreCase("Q")) We are done else { double x = Double.parseDouble(input);... }

Loop and a half Sometimes termination condition of a loop can only be evaluated in the middle of the loop Then, introduce a boolean variable to control the loop: boolean done = false; while (!done){ Print prompt String input = read input ; if ( end of input indicated ) done = true; else{ // Process input }

JCreator IDE: Integrated Development Editor Features: Compile and run from editor Manage multiple files Can look up api documentation Selective hiding of parts of the code

Workspace Concepts: Workspace: the editor window. A workspace may have several projects. A project: a java program. A project may consist of several java files. A file. A single java file. A file may contain several classes.

Workspace

Make a java program Create a new workspace Create a project – give it a name, specify where to save its files, and a type (Basic Java application). It generates a ”*.java” file and a ”*Frame.java” file (forget about the last one) Click on the first java file Replace the body of the main method to System.out.println(”Hello world”);

Compile and run Compile (called ”build”): F7 Run (”execute”): F5 If there were errors click on red cross in ”Build output” and the line is marked in the program. Click on ”System” in ”System.out….” Press ”Ctrl-F1” and you will get context-sensitive help

Editor features Word completion: ”ctrl-space” Match bracket, parentheses: ”ctrl-egu” Hide imports, comments, method body, class content. ’ctrl-f1’ on classes to get api-documentation ’ctrl-f1’ on method name to list methods