CS201 - Repetition loops.

Slides:



Advertisements
Similar presentations
Repetition and while loops. Assignments Due – Lab 4.
Advertisements

Chapter 5 Repetition and Loop Statements Instructor: Alkar & Demirer.
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 ICS103 Programming in C Lecture 7: Repetition Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 CS 201 Repetition Debzani Deb. 2 Overview Loops  Endfile-Controlled loop  Nested loop  Do-While loop  Flag-Controlled loop Hand Tracing the code.
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5 Repetition and Loop Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Introduction to Computer Programming in c
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Program Errors and Debugging Week 10, Thursday Lab.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
Repetition Statements while and do while loops
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
PGT C Programming1 Week 4 – Repetition Structures / Loops.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Chapter 4 – C Program Control
Week 4 – Repetition Structures / Loops
Lecture 13 & 14.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Control Structures Lecture 7.
Chapter 6 Decision Making and Looping
Chapter 6: Repetition Statements
Repetition and Loop Statements
Computer programming Lecture 3.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

CS201 - Repetition loops

Types Of Loops Counting Loop (while, for) Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation Loop (do-while) General Conditional Loop (while, for)

While Loop (same as Java) while (condition) { Do Something } When condition is true, Do Something and return to while. When condition is false, skip and exit loop. i = 0; while ( i<10 ) { printf(“%d “, i); i=i+1;}

1 2 3 4 5 6 7 8 9

For Loop (almost like Java) No internal type statement allowed! for (initialization; test; update) { Do Something } for(i=0; i<10; i=i+1) { printf(“%d\n”,i); } (same output) for (int j=0; j<10; j=j+1) { … } NOT ALLOWED IN C !

Interpretation First the initialization expression(s) is(are) executed. Then the test expression(s) is(are) evaluated. If true, statements are executed, update expression(s) is(are) executed, and test is evaluated again. If false, loop exits. Last expression controls multiple expressions.

What Prints? int i=0, j=0; for (i=0, j=-2; i<8, j<2; i=i+1, j=j+2) printf(“%d %d\n”, i,j);

What Prints? int i=0, j=0; for (i=0, j=-2; i<8, j<2; i=i+1, j=j+2) printf(“%d %d\n”, i,j); 0 -2 1 0

Do While Loop (just like Java) Do {something} while ( expression); Always does “something” at least once! Test expression. If true, return and to something again. If false, exit loop at that point.

What prints? int i=0; do { i=i+3; printf(“%d\n”, i); } while (i<12);

What prints? int i=1; do { i=i+3; printf(“%d\n”, i); } while (i<12); 4 7 10 13

Compound Assignment Like Java. x += 3; is the same as x = x + 3; In general: var op = expression; Is the same as var = var op expression; += -= *= /= %=

Increment and Decrement Operators Like Java Prefix and postfix versions ++i i++ i++; is the same as i = i + 1; j--; is the same as j = j – 1; However k=++i; is different than k=i++;

Prefix increment What prints? int i=0, j=0; printf(“%d\n”, i++); j = i++; printf(“%d\n,j);

Prefix increment What prints? 2 int i=0, j=0; printf(“%d\n”, i++); 2 int i=0, j=0; printf(“%d\n”, i++); printf(%d\n”,++i); j = i++; printf(“%d\n,j); What’s the value of i here?

Prefix increment What prints? 2 int i=0, j=0; printf(“%d\n”, i++); 2 int i=0, j=0; printf(“%d\n”, i++); printf(%d\n”,++i); j = i++; printf(“%d\n,j); What’s the value of i here? 3

Sentinel-Controlled Loops Loops that run as long as needed. Signal the end of the loop with a special value (negative for ages, etc.) Get a line of data While the sentinel value has not been encountered Process the data line. Get another line of data.

What prints? int i, sum=0; scanf(“%d”,&i); while(i!=-9) { sum+=i; } printf(“%d\n”, sum); /* input = 1 3 5 7 -9 */

What prints? int i, sum=0; scanf(“%d”,&i); while(i!=-9) { sum+=i; } printf(“%d\n”, sum); /* input = 1 3 5 7 -9 */ 16

What prints? int i, sum=0; scanf(“%d”,&i); while(i!=-9) { sum+=i; } printf(“%d\n”, sum); /* input = -9 */

What prints? int i, sum=0; scanf(“%d”,&i); while(i!=-9) { sum+=i; } printf(“%d\n”, sum); /* input = -9 */

EOF Many library functions return helpful data as the value of the function? For example, scanf returns the number of successful conversions. File functions can return a value equal to EOF (a special defined variable). You can send this to scanf with ctrl-D (linux)

What prints? int i, sum=0,status=0; status = scanf(“%d”,&i); while(status!=EOF) { sum+=i; } printf(“%d\n”, sum); /* input = */ 2 4 6 ctl-d

What prints? int i, sum=0,status=0; status = scanf(“%d”,&i); while(status!=EOF) { sum+=i; } printf(“%d\n”, sum); /* input = */ 2 4 6 ctl-d 12

Nested Loops Usually used to work with two dimensional arrays (later). Simply put the definition for one loop in the body of another loop.

What Prints? int a=0, b=0, sum=0; for(a=0; a<6; a+=2) for(b=0; b>4; b--) sum=sum+1; printf(“%d”,sum);

What Prints? int a=0, b=0, sum=0; for(a=0; a<6; a+=2) for(b=0; b>-4; b--) sum=sum+1; printf(“%d”,sum);

Debugging Use a source level debugger as part of your IDE. Use a command line debugger like gdb. Add printf statements to trace execution. (Be careful when removing the printf statements that you don’t introduce errors.)

∞ Loops It’s hard to distinguish between a complex calculation loop and an infinite loop without debugging statements.