Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Looping Yong Choi School of Business CSU, Bakersfield.
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.
Copyright © Texas Education Agency, Computer Programming For Loops.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Chapter 5 Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Repetition Statements while and do while loops
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
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.
CS 100 Introduction to Computing Seminar October 7, 2015.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
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.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Learning About the Loop Structure (continued)
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.
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
3 Control Statements:.
Chapter 6: Repetition Statements
Computing Fundamentals
Repetition Statements
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Building Java Programs
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
While Loops in Python.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts for incrementing and accumulating: += add and assign y += 2 y = y+ 2; -= subtract and assign y -=2 y = y-2; *= multiply and assign y*=2 y=y*2 /= divide and assign y/=2 y=y/2 %= remainder and assign y%=2 y=y%2 1 Java Programming, Sixth Edition

Increasing and Decreasing a Variable Different ways to increase by 1 int count = 0; count = count + 1; used as an accumulator count +=1; used as an accumulator count++ ; increment operator ++ Different ways to subtract 1 count = count -1 count -= 1 count-- decrement operator --

When would you use an accumulator? When you want to average a set of grades. totalGrades += grade; totalDeposit += deposit; sum+= num You would use a while statement that will let the user continually enter numbers until you type a certain value.

Categories of loops definite loop: Executes a known number of times. – The for loops are definite loops. – Examples: Print "hello" 10 times. Find all the prime numbers up to an integer n. Print each odd number between 5 and 127. indefinite loop: One where the number of times its body repeats is not known in advance. – Examples: Prompt the user until they type a certain value. Print random numbers until a prime number is printed. Repeat until the user has types "q" to quit.

5-5 Repetition Statements Repetition statements or loops allow us to execute a statement multiple times Like conditional statements, they are controlled by boolean expressions Java has three kinds of repetition statements: – the while loop – the do loop // not tested on AP exam – the for loop

" while " loops A while statement has the following syntax: while (condition) statement; If the condition is true, the statement is executed; then the condition is evaluated again The statement is executed over and over until the condition becomes false statement condition false true

© 2004 Pearson Addison- Wesley. All rights reserved 5-7 The while Statement An example of a while statement: int count = 7; while (count < 5){ System.out.println (count); count++; } If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times

© 2004 Pearson Addison- Wesley. All rights reserved 5-8 Infinite Loops The body of a while loop eventually must make the condition false If not, it is called an infinite loop, which will execute until the user interrupts the program int count = 1; while (count <= 25){ System.out.println (count); count = count - 1; } An example of an infinite loop:

Two types of loops 1. Count Controlled Loop There are three parts to a count-controlled loop: 1) initialize the variable 2) test the variable 3) increment the variable

While loop int x = 1; // initialize the variable while(x < 5) // test the variable condition { // begin loop x++; // increment variable System.out.print(x); // print statement is inside loop } // loop ends

Trace loop int x = 1; while(x < 5) { x++; // increment x System.out.print(x ); } Starting x = 1 (1 < 5) X Is true? x++ print 1Yes Yes Yes Yes no stop Print 2345

accumulator int m = 2; total = 0; // initialized outside the loop while(m < 6) { total += m; // accumulator total = total + m; m++; // increment control variable } System.out.println(total); // print statement is outside the loop

scope int m = 2, total = 0; while(m < 6) { // body of loop total = total+m; m++; } // loop ends System.out.println(total); total needs to be accessed outside the loop Variables need to be declared outside the loop that need to be seen or used outside the loop Any variable created inside { } of method, if statement, for statement, while statement is not visible outside the curly braces. { }

Trace int m = 2; total = 0; while(m < 6) { total = total+m; m++; } System.out.println(total); Check condition 2 < 6 m is true? total 2 yes 2 3 yes 5 4 yes 9 5 yes 14 6 no get out of loop total = 14 print 14

Decrement int num = 10; while ( num > 0 ) // decrease until no longer > 0 { System.out.println( num); num--; //decrement } // end while loop System.out.println("Loop ended");

accumulator variable int b = 5, ans = 0; while(b<11) { b=b+2; ans=ans+b; } System.out.println(ans); 27 m b+2 ans=ans+b = = = = = = stop An accumulator is a variable that keeps a running total. It will add something to itself.

accumulator variable int k=3, tot = 0; while(k<11) { tot = tot+k; k++; } System.out.println(tot); 52 k tot+k k = = = = = = = = stop An accumulator is a variable that keeps a running total. It will add something to itself.

accumulator variable int z=2, sum = 0; while(z<9) { z++ sum = sum+z } System.out.println(sum); 42 z zz++ sum=sum+z = = = = = = 42 9 stop An accumulator is a variable that keeps a running total. It will add something to itself.

Decrement Check condition(5>=0) Start loop num = 5; num is true? print 5 yes 5 4 yes 4 3 yes 3 2 yes 2 1 Yes 1 0 no get out of loop Print Loop ended int num = 5; while ( num > 0 ) { System.out.print( num); num--;} // end System.out.println("Loop ended");

Writing while conditions If what you want is to execute the loop 10 times, write the condition number < 10 and not as number <= 9 In Java generally you would more likely want to loop not from 1 to 10, but from 0 to 9. All counting in Java tends to start at zero rather than one. This is a convention that most Java programmers adopt.

Trace int b=2, sum=0; while(b<9) { b++; sum=sum+b; } System.out.print(sum); b=sum=

Nested Loop If you have a nested loop, it passes through the first loop and will continue to execute the second loop until finished, then goes back to the first loop int x = 6; int y, q; while (x < 10) { y = 1; while (y <= 10) { y++; q = x + y; } x += y; } System.out.println (q); x=Outer loop condition y=Inner loop condition y<=10 y++q=x+yx+=y;