1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i <= 10) { sum += i; i++; } Initialization Condition.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Computer Science 1620 Loops.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
Objectives You should be able to describe:
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.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Chapter 4 Program Control Statements
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.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
CPS120 Introduction to Computer Science Iteration (Looping)
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
CS101 Computer Programming I Chapter 4 Extra Examples.
Chapter 5: Structured Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CPS120 Introduction to Computer Science Iteration (Looping)
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
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.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
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.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope.
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.
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.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
CSE 220 – C Programming Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 7: Repeating a Known Number of Times
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Loop Structures.
CS1010 Programming Methodology
Chapter 8 Repetition Statements
Arrays, For loop While loop Do while loop
CSC215 Lecture Control Flow.
Program Control Topics While loop For loop Switch statement
Computing Fundamentals
Homework Any Questions?.
CMPE212 – Reminders The other four assignments are now posted.
Objectives You should be able to describe: The while Statement
ECE 103 Engineering Programming Chapter 18 Iteration
More Loops Topics Relational Operators Logical Operators for Loops.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Lec 6 Loop Statements Introduction to Computer Programming
CSC215 Lecture Control Flow.
Presentation transcript:

1 Loops II

2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i <= 10) { sum += i; i++; } Initialization Condition Update “while” loops typically follow a predictable pattern. There is a “control variable” such as i in the loop below.

3 The for Loop A more compact way to express the loop control information. All control information in a single line at the top of the loop. for (i = 1; i <= 10; i++) { sum += i; } Most widely used looping construct in C Initialization Condition Update Loop body

4 The for Loop for (i = 1; i <= 10; i++) { sum += i; } Initialization is done only once, before the loop body is executed the first time. Condition is tested before loop body is executed. (Same as a “while” loop. Loop body is executed if condition is true) Update is done after the loop body is executed each time, regardless of the condition. Repeat the loop body if condition is true. (Like “while”.) Continue after loop body if condition is not true.

5 The for Loop for (i = 1; i <= 10; i++) { sum += i; } Things to notice The three sections of the control unit are separated by semicolons. Loop body is a block of code, delimited by curly brackets. Same as for while or if. No semicolon!

6 The for Loop It is legal to write a single statement as the loop body: for (i = 1; i <= 10; i++) sum += i; or even: for (i = 1; i <= 10; i++) sum += i; Don’t! At least not for this course. (Even though our book approves.) Same reasons as for “if” and “while”.

7 The for Loop Caution: Watch out for this mistake: for (i = 1, i <= 10, i++) { sum += i; } Should be semicolons!

8 The for Loop Programming Style for (i = 1; i <= 10; i++) { sum += i; } Align curly brackets with the “for”. (Differs from book.) Indent the code four spaces. The usual “code block”

9 The for Loop Legally you can modify the loop control variable inside the loop for (i = 1; i <= 10; i++) { i += 3; sum += i; } This is always a bad idea! Treat the control variable as a read-only variable inside the loop.

10 Example: squares.c #include int main() { int i = 0; printf ("This program outputs a table of squares\n"); for (i = 0; i <= 10; i++) { printf ("%3d %6d\n", i, i*i); } getchar(); // Keep window open getchar(); return 0; }

11 Program Running

12 Common Mistakes See what happens if you put a semicolon after the control statement for (i = 0; i <= 10; i++); { printf ("%3d %6d\n", i, i*i); }

13 Program Running

14 Common Mistakes See what happens if you increment the control variable inside the loop body for (i = 0; i <= 10; i++) { printf ("%3d %6d\n", i, i*i); i++; }

15 Program Running

16 Common Mistakes See what happens if you use commas rather than semicolons in the control statement for (i = 1, i <= 10, i++) { printf ("%3d %6d\n", i, i*i); i++; }

17 Compile Error

18 Alternative Test You could test for inequality rather than less than. Usually NOT a good idea! #include int main() { int i = 0; printf ("This program outputs a table of squares\n"); for (i = 0; i != 10; i++) { printf ("%3d %6d\n", i, i*i); } getchar(); // Keep window open getchar(); return 0; }

19 Works Fine

20 Variations What if we want squares of just the even numbers? for (i = 0; i != 10; i+=2) { printf ("%3d %6d\n", i, i*i); }

21 Works Fine!

22 Variations How about just the odd numbers? for (i = 1; i != 10; i+=2) { printf ("%3d %6d\n", i, i*i); }

23 Not so good! Have to stop with Ctrl-C

24 Lesson Learned On tests for inequality, the limit value must be exactly right. Prefer tests for less than over tests for not equal to stop a loop. Be sure the loop stops even if the limiting value is not exactly right! End of Section

25 Modifying Control Flow C provides several statements that modify the normal flow of control within a loop: break continue goto break and continue are OK. Use as appropropriate to break out of a loop or immediately start the next iteration. goto is bad. A relic of the olden days. Kept mainly for compatibility. Forget that it is there.

26 C99 C99 permits the loop control variable to be defined in the control statement: for (int i = 0; i <= 10; i++) { printf ("%3d %6d\n", i, i*i); } i will not be visible outside the loop. This construct was invented in C++ Adopted for C in C99

27 Using C99 Circe supports C99 gcc -Wall -std=c99 xxx.c but Visual Studio does not.

28 Compile and Run on Circe