1 Fall 2007ACS-1903 Ch 4 Loops and Files Increment & decrement statements while loop do-while loop Other topics later on …

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

CS0007: Introduction to Computer Programming
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
CS0004: Introduction to Programming Repetition – Do Loops.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Control Structures 2 Chapter 6. © Janice Regan 2003 Basic Loops  When one action is to be repeated a number of times a loop is used. Loops are repetition.
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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
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.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Intro to Programming Logic and Design CIS 115 Brought to you by: done by Angela Robinson for CSC 289.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
LOOPING What are the 3 structures of writing code? sequential decision repetition Def. Looping is the repetition of statements in a program. There various.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
CONTROLLING PROGRAM FLOW
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.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
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.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Fundamental Programming Fundamental Programming More on Repetition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
While ( number
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Topic 4: Looping Statements
The switch Statement, and Introduction to Looping
JavaScript: Control Statements I
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: Repetition Structures
CiS 260: App Dev I Chapter 4: Control Structures II.
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements I
Looping and Repetition
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Repetition Control Structure
Three Special Structures – Case, Do While, and Do Until
Chapter 6: Repetition Statements
More Loops Topics Counter-Controlled (Definite) Repetition
PROGRAM FLOWCHART Iteration Statements.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

1 Fall 2007ACS-1903 Ch 4 Loops and Files Increment & decrement statements while loop do-while loop Other topics later on …

2 Fall 2007ACS-1903 Incrementing and Decrementing Variables by 1 number = number + 1 ; number += 1; number = number - 1 ; number -= 1 ; number++; ++number; number --; --number;

3 Fall 2007ACS-1903 while loops while Pre-test (expression tested before the iteration) Loop could be executed zero times do-while Post-test (expression tested after the iteration) Loop executed at least once

4 Fall 2007ACS-1903 Flowchart for while Boolean Expression Loop statement(s) false true The statements comprising the loop body are executed while the expression is true. pre-test

5 Fall 2007ACS-1903 UML for while Loop statement(s) [expression false] [expression true]

6 Fall 2007ACS-1903 Reading keyboard input until a sentinel is reached countStudents = 0; while (studentNumber != 0) { countStudents++; studentNumber=keyboard.nextInt(); } Sometimes systems are designed to process some data until a “special” value is encountered. ReadUntilSentinel.java

7 Fall 2007ACS-1903 Flowchart for do-while Boolean Expression Loop statement(s) false true The statements comprising the loop body are executed repeatedly until the expression becomes false. post-test

8 Fall 2007ACS-1903 UML for while Loop statement(s) [expression false] [expression true]

9 Fall 2007ACS-1903 Summing the digits of a number do { sum +=(number % 10); number=number/10; } while (number!=0); Sometimes a process can be used that can or must execute at least once and so it can be structured with a loop with a post-test. SumDigitsOfInteger.java

10 Fall 2007ACS-1903 Example studentNumber=keyboard.nextInt(); // get student numbers until the sentinel of 0 is encountered while (studentNumber != 0) { mark = keyboard.nextDouble(); if (mark < 50) grade = "F"; else if (mark < 60) grade = "D"; else if (mark < 65) grade = "C"; else if (mark < 70) grade = "C+"; else grade = "B"; // fill in the rest of this System.out.println(studentNumber+" "+mark+" "+grade); studentNumber=keyboard.nextInt(); } Read student numbers and marks -until sentinel of 0 for student number -if there is a student number, there will be a mark too -assign grades based on marks What is the flowchart or activity diagram for this? marksAndGrades.java