 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.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Chapter 8: Arrays.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repeating Actions While and For Loops
if (condition) { // always either true or false then clause } if (condition) {// always either true or false then clause } else { // optional else clause.
Mock test review Revision of Activity Diagrams for Loops,
2  An instruction or group of instructions need to be repeated several times.  2 types of iteration: 1.You do not know how many times you will need.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
1 Repetition structures Overview while statement for statement do while statement.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
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.
1 Repetition structures Overview while statement for statement do while statement.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
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.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 5: Control Structures II
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Java Review if Online Time For loop Quiz on Thursday.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Introduction to Computers and Programming Lecture 7:
Computer Programming1 Computer Science 1 Computer Programming.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Sophomore Scholars Java
Slides by Evan Gallagher
Slides by Evan Gallagher
CSC111 Quick Revision.
Control Structures.
Chapter 5: Control Structures II
Repetition-Counter control Loop
Incrementing ITP © Ron Poet Lecture 8.
Iteration with While You can say that again.
Module 4 Loops.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Java Programming Loops
Repetition Statements
Java LESSON 3 Loops.
Presentation transcript:

 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 your test data (use numbers that will be easy to check for the correct answer): ◦ 10, 20, 30, 40, 50, 60 ◦ Expected total is 210.  Design and code a solution to the problem.

while (condition) { // if false then loop not executed statement(s) inside the loop } // end while do { // always done at least once statement(s) inside the loop } while (condition) // end do for (control initial value; (condition) ; control update) { // if false then loop not executed statement(s) inside the loop } // end for Condition is always either true or false. while and do: something must be updated inside the loop which affects the condition (so that it is not an endless loop). for: something is updated inside the “for” statement which affects the condition (so that it is not an endless loop). e.g. while (i < 4) { System.out.println("i "= " + i); i = i + 1; } // end while

for (it is raining; while it rains; check if it is raining) { I will need my umbrella } // end for (the for loop has ended therefore the condition must now be false) // I do not need my umbrella because it is not raining. The for loop will not be executed if the condition is false, just like the while loop. condition then clause Remember the while loop example? while (it is raining) { then I will need my umbrella } // end while // Now I do not need my umbrella // (because it must have stopped raining)

for (it is raining; while it rains; check if it is raining) { I will need my umbrella } // end for // (now the condition must be false) // Now I do not need my umbrella because it must not be raining. condition then clause initialisation statement loop condition increment statement Normally we use a for loop when we know how many times we want to execute the loop.

for (i = 0; i < 5; i++) { statement(s) } // end for Has the same effect as: condition then clause loop control variable is i initialisation statement i = zero loop condition while i < 5 is true int i = 0; while (i<5){ statement(s) i++; // must not forget to update the control variable } // end while increment statement add 1 to i

if (it is raining) { then I will need my umbrella for (it is raining; while it is raining; check for rain) { then I will need my umbrella } // end for } // end if Now I do not need my umbrella if (your_age < 65) for (the_age = your age; the_age < 65; the_age++) { then you cannot get your state pension } // end for } // end if Now you can get your state pension condition then clause initialisation statement loop condition increment statement

for (it is raining or snowing; it is raining or snowing; check weather) then I will need my umbrella } // end while // I will not need my umbrella //(because it is not raining and it is not snowing) for (it is sunny and hot; it is sunny and hot; check weather) { then I will need a sunshade and a cold drink } // end while // I will not need a sunshade and a cold drink // (this means it is not both sunny and hot // it might be sunny but cold, or it might be cloudy but hot, // either way I have no sunshade and get no drink) initialisation statement loop condition increment statement

import javax.swing.JOptionPane; public class week9 { public static void main (String[] args) { int i=0; int j=0; for (i = 1; i < 5 && j < 5; i++) { // statements } System.out.println("i = " + i + " j = " + j); } initialisation statement loop condition increment statement

import javax.swing.JOptionPane; public class week9 { public static void main (String[] args) { int i=0; int j=0; for (i = 1; i < 5 || j < 5; i++) { // statements } System.out.println("i = " + i + " j = " + j); } What would happen if we ran this program? initialisation statement loop condition increment statement

for (i = 1; i < 5; i = i + 1) { statement(s) for (j = 1; j < 5; j = j + 1) { statement(s) } // end for initialisation statement loop condition increment statement

 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 your test data (use numbers that will be easy to check for the correct answer): ◦ 10, 20, 30, 40, 50, 60 ◦ Expected total is 210.  Design and code a solution to the problem. Think about the meaning. How can we use a for loop here? Normally we use a for loop when we know how many times we want to execute the loop. We could use a loop to access each element of the array one at a time. We will need a variable to hold the total, and a variable to count the elements of the array.

1. Get unknown data and create variables. 1.1 create an array with values 10, 20, 30, 40, 50, create and zeroise total. 1.3 create and zeroise counter. 2. Processing. 2.1 count for six times 2.2 add current array value to total 2.3 end for 3. Output. 3.1 Output the total.

22 [count >= 6] Zeroise total Total = total + array[count] [count < 6] Output total Array = 10, 20, 30, 40, 50, 60 Zeroise counter Add 1 to count

//1. Get unknown data and create variables. //1.1 create an array with values 10, 20, 30, 40, 50, 60. //1.2 create and zeroise total. // 1.3 create and zeroise counter. //2. Processing. //2.1 count for six times //2.2 add current array value to total //2.3 end for //3. Output. //3.1 Output the total.

//1. Get unknown data and create variables. //1.1 create an array with values 10, 20, 30, 40, 50, 60. int [] number = {10, 20, 30, 40, 50, 60}; //1.2 create and zeroise total. int total = 0; // 1.3 create and zeroise counter. int count = 0; //2. Processing. //2.1 for six times for (count=0;count<6;count++) { //2.2 add current array value to total total = total + number[count]; //2.3 end for } // end for //3. Output. //3.1 Output the total. System.out.println("The total is " + total); Remember in Java the first element of an array is referred to as position zero.