6. LOOPS. Example: Summing a Series of Numbers #include int main(void) { int n, sum = 0; printf("This program sums a series of numbers.\n"); printf("Enter.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

Introduction to Computers and Programming Lecture 9: For Loops New York University.
Control Flow C and Data Structures Baojian Hua
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
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.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 4 Program Control Statements
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
Overview Go over parts of quiz? Another iteration structure for loop.
Chapter 6: Loops Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 6 Loops.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
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.
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.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
Chapter 5: Selection Statements Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 5 Selection Statements.
1 Chapter 6 Loops. Iteration Statements C’s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
Chapter 6: Loops.
Repeating the Execution of Statements
Lecture 7: Repeating a Known Number of Times
Chapter 4 - Program Control
CS1010 Programming Methodology
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company.
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company. 1
Flow of Control.
Looping.
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
Arrays, For loop While loop Do while loop
FLOW OF CONTROL.
Flow of Control.
- Additional C Statements
A First Book of ANSI C Fourth Edition
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 2.1 Repetition.
Program Control Topics While loop For loop Switch statement
Project 1 questions and re-grading
while while (condition) { statements }
Dale Roberts, Lecturer IUPUI
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company.
Chapter 4 - Program Control
Flow of Control.
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

6. LOOPS

Example: Summing a Series of Numbers #include int main(void) { int n, sum = 0; printf("This program sums a series of numbers.\n"); printf("Enter numbers (0 to terminate): "); scanf("%d", &n); while (n != 0) { sum += n; scanf("%d", &n); } printf("The sum is: %d\n", sum); return 0; }

while Statements The while statement has the form while ( expression ) statement The body of a while statement is executed repeatedly as long as the expression is true (has a nonzero value). The expression is tested before each execution of the body. Example: i = 10; while (i > 0) { printf("T minus %d and counting\n", i); --i; }

while Statements An alternative: i = 10; while (i) printf("T minus %d and counting\n", i--); Using a nonzero constant as the controlling expression creates an infinite loop: while (1) {... }

do Statements The do statement has the form do statement while ( expression ) ; The do statement is the same as the while statement, except that the expression is evaluated after each execution of the loop. Example: i = 10; do { printf("T minus %d and counting\n", i); --i; } while (i > 0);

for Statements The for statement has the form for ( expr1 ; expr2 ; expr3 ) statement The following code expresses the meaning of the for statement: expr1; while (expr2) { statement /* exception: */ expr3; /* continue statement */ } /* (later) */

for Statements For example, the statement for (i = 10; i > 0; --i) printf("T minus %d and counting\n", i); is equivalent to i = 10; while (i > 0) { printf("T minus %d and counting\n", i); --i; }

for Statements Any or all of the three expressions in a for statement may be omitted: i = 10; for (; i > 0; --i) printf("T minus %d and counting\n", i); for (i = 10; i > 0;) printf("T minus %d and counting\n", i--); i = 10; for (; i > 0;) printf("T minus %d and counting\n", i--);

for Statements Omitting the middle expression creates an infinite loop. The statement for (;;) {... } is equivalent to while (1) {... }

Comma Operator The for statement is normally controlled by three expressions. The comma operator allows us to have more than three: for (sum = 0, i = 1; i <= N; i++) sum += i; In general, a comma expression has the form expr1, expr2 expr1 is first evaluated and its value discarded; the value of expr2 is the value of the entire expression. The comma operator is handy whenever C requires a single expression, but we’d like to have two or more expressions. In practice, it appears primarily in for statements and macros.

General Rules for Loops When designing a loop, try to use a for statement instead of a while or do statement, especially if the loop “counts up” or “counts down.” Become familiar with the patterns that most often appear in for statements: Counting up from 0 to N–1: for (i = 0; i < N; i++)... Counting up from 1 to N: for (i = 1; i <= N; i++)... Counting down from N–1 to 0: for (i = N-1; i >= 0; i--)... Counting down from N to 1: for (i = N; i > 0; i--)...

General Rules for Loops If a for statement would be awkward, use a while statement instead. Use a do statement instead of a while statement if the controlling condition must be tested after the loop body has been executed.

break Statement Can be used to jump out of a while, do, for, or switch statement. Example: sum = 0; while (1) { printf("Enter a number: "); scanf("%d", &i); if (i == 0) break; sum += i; }

continue Statement Can appear in while, do, and for statements. Causes the program to skip the rest of the current loop iteration. Example: n = 0; sum = 0; do { scanf("%d", &i); /* read (and sum) */ if (i == 0) continue; /* ten numbers, */ ++n; /* skipping zeroes */ sum += i; } while (n < 10);

Null Statement The null statement is just a semicolon: i = 0; ; j = 1; The null statement is primarily good for one thing: writing loops whose bodies are empty. For example, the statement for (div = 2; div < n; div++) if (n % div == 0) break; can be rewritten as a loop with an empty body: for (div = 2; div < n && n % div != 0; div++);

Null Statement Warning: Accidentally putting a semicolon after the parentheses in an if, while, or for statement will not be detected by the compiler. if (i < 0); /* error */ printf("Error: i is less than zero\n"); i = 10; while (i > 0); /* error */ { printf("T minus %d and counting\n", i); --i; } for (i = 10; i > 0; --i); /* error */ printf("T minus %d and counting\n", i);