Incrementing ITP © Ron Poet Lecture 8.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Helper Methods ITP © Ron Poet Lecture 11.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
 Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Looping Yong Choi School of Business CSU, Bakersfield.
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.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
5.05 Apply Looping Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
ITP © Ron Poet Lecture 7 1 Repetition. ITP © Ron Poet Lecture 7 2 Easing Repetitive Tasks  Many computing task are repetitive.  Checking all known foods.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Introduction To Repetition The for loop
Lecture 6 Repetition Richard Gesick.
The switch Statement, and Introduction to Looping
Introduction to Computer Science / Procedural – 67130
2. Java language basics (2)
Lesson 05: Iterations Class Chat: Attendance: Participation
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.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 07 More Repetition Richard Gesick.
While Loops Chapter 3.
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Lecture 4A Repetition Richard Gesick.
Building Java Programs
Iteration with While You can say that again.
MSIS 655 Advanced Business Applications Programming
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Outline Altering flow of control Boolean expressions
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Coding Concepts (Basics)
Do … Loop Until (condition is true)
Logical Operations In Matlab.
3.1 Iteration Loops For … To … Next 18/01/2019.
February , 2009 CSE 113 B.
More Loops Topics Counter-Controlled (Definite) Repetition
Loops.
Java LESSON 3 Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Another Example Problem
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
Chap 7. Advanced Control Statements in Java
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Lecture 3 More on Flow Control, More on Functions,
Software Development Techniques
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
More Loops Topics Counter-Controlled (Definite) Repetition
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Incrementing ITP © Ron Poet Lecture 8

Adding 1 To A Variable Adding 1 to a variable is a very common task. yearCount = yearCount + 1. It is so common that there is a special notation. yearCount++; The ++ operator means add 1. There is also a -- operator. ITP © Ron Poet Lecture 8

Adding Something to a Variable This is also very common prices = prices + inflation * prices. There is a special notation. prices += inflation * prices += means add the right hand side to the variable. There is also a -= operator. ITP © Ron Poet Lecture 8

Many Similar Operators Most operators have an = version. *=, /=, %= They are convenient and make your program easier to read. ITP © Ron Poet Lecture 8

Counting Loops ITP © Ron Poet Lecture 8

Counting Loops It is common to want to iterate for a given number of times. Here is a loop that counts from 1 to 10. The variable i takes the values 1, 2, 3, . . ., 10 inside the loop. int i = 1; while (i <= 10) { // do something with i; i++; } ITP © Ron Poet Lecture 8

For Loop This pattern is so common that there is a special loop for it. The previous code can be written as. for (int i = 1; i <= 10; i++) // do something with i; It is almost exactly the same as using a while loop. It is easier to read because the loop control is all in one place, at the start of the loop. ITP © Ron Poet Lecture 8

The Scope of i The difference is the scope of the loop variable i. It is restricted to the for loop and cannot be used after the loop has finished. If we want to use i after the loop, we must define it before the loop. int i; for (i = 1; i <= 10; i++) // do something with i; ITP © Ron Poet Lecture 8

Only ;; is Essential The for loop has three parts. Initial action: int i = 0 The test: i <= 10 Action at end of loop: i++ These do not have to be there. Omit them if they are not necessary. for (;;) is legal, an infinite loop! ITP © Ron Poet Lecture 8

Types of Loops ITP © Ron Poet Lecture 8

Test First Loops Continue Test false true Body of Loop After the Loop ITP © Ron Poet Lecture 8

Exit in Middle Loop First Part of Loop Exit Test true false Second Part of Loop After the Loop ITP © Ron Poet Lecture 8

Which Loop? while and for loops are test first loops. The first thing that is done is check the test to see if it is true. More complicated loops can do something first. test in the middle of the loop. do something else go round again ITP © Ron Poet Lecture 8

Exit in Middle Styles There are two main ways of doing an exit in the middle loop. Both have their advocates and detractors. One is the 'if - break out of middle' style. The other is the 'boolean variable – else' style. ITP © Ron Poet Lecture 8

Example Situation Here is a possible loop design for an example situation. Loop Get a response from the user. If user says NO exit process the user's response. ITP © Ron Poet Lecture 8

if – break Style Java has a break statement which means exit the loop. It is normally controlled by an if statement. We only exit the loop if the exit test is true. This is different from the while loop. while test true means go round again. if test true means stop loop. The actual loop statement is an infinite loop. Because we control the exit from the middle. ITP © Ron Poet Lecture 8

if – break code for (;;) // infinite loop { con.print("Continue? "); String response = con.getWord(); if (response.equals("NO")) break; // exit the loop con.println("You typed " + response); } ITP © Ron Poet Lecture 8

boolean – else Style We define a boolean variable, usually called done, which we test at the start of the loop. It is initialised to false. We set it to true if the exit test is true. The second part of the loop is in an else. ITP © Ron Poet Lecture 8

boolean - else code boolean done = false; while (!done) { con.print("Continue? "); String response = con.getWord(); if (response.equals("NO")) done = true; // exit the loop later on // so skip the second part of the loop else con.println("You typed " + response); } ITP © Ron Poet Lecture 8

Which is Best if – break is closest to the design. Reading the code tells you immediately that you are in an 'exit in middle' loop. Program proving works better if all loops are tested at the start. Automatic tools work better. ITP © Ron Poet Lecture 8