Do-while and for Loops 10-1-2001. Opening Discussion zWhat did we talk about last class? zAt the end of last class I asked you to write a loop that would.

Slides:



Advertisements
Similar presentations
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Advertisements

Repeating Structures Do While Loops. Do While Loop While loops have a test condition before the loop –This means that java will test the condition before.
ITC 240: Web Application Programming
Computer Science 1620 Loops.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
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.
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.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
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.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Chapter 4 Program Control Statements
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
String Processing Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have questions.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
Controlling Execution Dong Shao, Nanjing Unviersity.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
MIPS Machine Assembly Language Opening Discussion zWhat did we talk about last class? zHave you seen anything interesting in the news? zDo you.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Fundamental Programming Fundamental Programming for Loops.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Fundamental Programming Fundamental Programming More on Repetition.
1 Iterative Structures --- LOOPS zYou can alter the sequential control of flow from 1 statement to the next by: yCalling a function yIterative Statements(loops)
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
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.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Computer Programming -1-
UCT Department of Computer Science Computer Science 1015F Iteration
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 4 – C Program Control
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
CS161 Introduction to Computer Science
Chapter 2.2 Control Structures (Iteration)
Expressions and Control Flow in JavaScript
Arrays, For loop While loop Do while loop
Chapter 4 - Program Control
Chapter 2.2 Control Structures (Iteration)
A LESSON IN LOOPING What is a loop?
JavaScript: Control Statements II
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

do-while and for Loops

Opening Discussion zWhat did we talk about last class? zAt the end of last class I asked you to write a loop that would find the sum of the squares between 1 and n. int i=1; int sum=0; while(i<=n) { sum+=i*i; }

do-while Loop Syntax zThere is another loop construct nearly identical to the while loop that you use if you know that the body of the loop should execute at least once: the do-while loop. zIt requires all the same “parts” as the while loop but the condition is not checked until after it has executed once. do { statements; } while(condition);

while vs. do-while zA while loop can be made to do anything you need from a loop as can a do-while loop. However, both exist in the language because different ones come in handy at different times. zIf you know that a loop should execute at least once the do-while structure typically makes the logic a bit easier and cleaner.

for Loop Syntax zIn C++ the for loop simply allows you to combine initialization and iteration into the basic loop structure. It has the following syntax. zIt behaves much like a while loop. for(initialization; condition; iterator) { statements; }

Comma “Operator” zNotice that in the for loop the semicolons play the role of separating the initializer from the condition and the condition from the iterator. That means if you want to have multiple expressions in them you have to separate then with something else. zThe comma works as an operator to separate expressions. The value of the complete expression is that of the last subexpression in the comma separated list.

Increment and Decrement zWhile you have seen these is my examples before we should take a closer look at the increment (++) and decrement (--) operators. zWhen placed before a variable the variable is either increased or decreased by one before the statement executes. If it is after then it happens after the statement executes. In most situations it doesn’t matter.

Examples zNow we will look at some sample code that illustrates how the three types of loops can be constructed and what the relative strengths of each are. yfor - Great for numeric loops because of compact structure. Makes it harder to forget things too. ywhile - General purpose when initilizer or iterator are complex. ydo-while - always executes once.

Minute Essay zWrite a for loop that computes x^n where n is an integer. Remember that ‘^’ is not exponentiation in C++. zNext class we will finish with loops before we start working on arrays.