Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Repeating Actions While and For Loops
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.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 5: Control Structures II (Repetition)
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 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Iterations Very Useful: Ability to repeat a block of code Example:
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.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Follow up from lab See Magic8Ball.java Issues that you ran into.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Introduction To Repetition The for loop
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Chapter 5: Control Structures II
Conditionals, Loops, and Other Kinds of Statements
Looping.
Outline Altering flow of control Boolean expressions
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Chapter 2.1 Repetition.
Repetition Control Structure
Repetition and Loop Statements
Control Structures Part 1
Seating “chart” Front - Screen rows Back DOOR.
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 18 Iteration
Repetition Statements (Loops) - 2
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
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.
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Repetition and Iteration ANSI-C

Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as desired. All the programs we have written look like the one below:

#include /* * pre-condition: int year, represents a year. * post-conditions: returns 1 if leap, 0 if not leap. */ int leap(int year){ return (year %100) && (year%400 == 0) || !(year % 100 == 0) && (year % 4 == 0); } int main(void){ int value; printf("Enter an integer representing a year>>"); scanf("%d", &value); if (leap(value)) printf(" %d is a leap year", value); else printf(" %d is not a leap year", value); return (0); }

Lousy main structure This program calls on the function leap once, and then it terminates. We will need to execute the program every time we want to try it with a different set of data.

While control structure while (condition) statement; Or while (condition){ statement1; statement2; …. } Loop condition: a boolean expression Semantics: a.evaluates condition b. if true executes all statement Continues doing a. and b. until condition becomes false. When condition evaluates to false, while is then terminated.

Condition in the while What kind of condition to write? It depends on the number of times we want the while to execute. But MORE importantly it depends on the computation the while is designed to performed. In others words, we often do not know the number of times we want the while to execute must establish: –The condition that will control the execution of loop –How the body of loop relates to the condition so that the condition becomes false only when the desired computation is finished and conversely.

Writing whiles to Repeat execution If we just want to write a while to repeat the execution of a set of statements a certain number of times: 10 must keep track of the number of times the loop executes via a count loop condition must be true as long as count has not reached correct number body of the loop must increment count every time it executes. int count = 0; while(count ?? 10){/* = != < <= */ statement1; statement2; …. count = count + 1; }

Writing whiles to Repeat execution Or if we want to execute a set of statements up until we externally stop the program: while (1){ statement1; statement2; …. }

#include /* * pre-condition: int year, represents a year. * post-conditions: returns 1 if leap, 0 if not leap. */ int leap(int year){ return (year%400 == 0) || !(year % 100 == 0) && (year % 4 == 0); } int main(void){ int value; while (1) { printf("Enter an integer representing a year, or ^C return to stop program>>"); scanf("%d", &value); if (leap(value)) printf(" %d is a leap year", value); else printf(" %d is not a leap year", value); } return (0); }

#include /* * pre-condition: int year, represents a year. * post-conditions: returns 1 if leap, 0 if not leap. */ int leap(int year){ return (year%400 == 0) || !(year % 100 == 0) && (year % 4 == 0); } int main(void){ int value; int count = 0; while (count < 10) { printf("Enter an integer representing a year, or ^C return to stop program>>"); scanf("%d", &value); if (leap(value)) printf(" %d is a leap year", value); else printf(" %d is not a leap year", value); count = count + 1; } return (0); }

#include /* * pre-condition: int year, represents a year. * post-conditions: returns 1 if leap, 0 if not leap. */ int leap(int year){ return (year%400 == 0) || !(year % 100 == 0) && (year % 4 == 0); } int main(void){ int value; int count = 1; while (count <= 10) { printf("Enter an integer representing a year, or ^C return to stop program>>"); scanf("%d", &value); if (leap(value)) printf(" %d is a leap year", value); else printf(" %d is not a leap year", value); count = count + 1; } return (0); }

scanf This is a function included from the library As a function it is specified to return the number of tokens it succesfully read. scanf(“%d”, &value1); scanf(“%d%d”, &value1, &value2); In particular it will return 0 when it fails reading at all. use this to structure loop: –place as condition of loop to read an integer for year, –to stop loop, do not enter an integer, enter a letter.

#include /* * pre-condition: int year, represents a year. * post-conditions: returns 1 if leap, 0 if not leap. */ int leap(int year){ return (year%400 == 0) || !(year % 100 == 0) && (year % 4 == 0); } int main(void){ int value; printf("Enter an integer representing a year, or a non-blank character to indicate no more data>>"); while (scanf("%d", &value)!= 0){ if (leap(value)) printf(" %d is a leap year\n", value); else printf(" %d is not a leap year\n", value); printf("Enter an integer representing a year, or a non-blank character to indicate no more data>>"); } printf("Program stop by user."); return (0); }

Indicating no more data Terminating program by entering a letter is fine as we expect “legal data” to be integers. So, entering something different from a digit, or blanks scanf fails to read it and returns 0 as the number of successful reads and our loop terminates. But what if we are dealing with a program where numbers as well as characters are legal data? How to tell the loop that there is no more legal data to be entered? User indicates no more data by ^Z return. scanf returns –1 when there is NO MORE data. represents –1 using the constant EOF.

#include /* * pre-condition: int year, represents a year. * post-conditions: returns 1 if leap, 0 if not leap. */ int leap(int year){ return (year%400 == 0) || !(year % 100 == 0) && (year % 4 == 0); } int main(void){ int value; printf("Enter an integer representing a year, or ^z return to indicate no more data>>"); while (scanf("%d", &value)!= EOF){ if (leap(value)) printf(" %d is a leap year\n", value); else printf(" %d is not a leap year\n", value); printf("Enter an integer representing a year, or ^z return to indicate no more data>>"); } printf("Program stop by user."); return (0); }

Performing computations with whiles Now for the most typical use of while loops. Example: write a function that computes n! int fact(int number){ int result = 1; int value = 2; while ( value <= number){ result = result * value; value = value + 1; } return result; }

Performing computations with whiles Write a function that computes …+n 2 int squareAddition(int number){ int result = 1; int value = 2; while ( value <= number){ result = result + value*value; value = value + 1; } return result; }

Performing computations with whiles Write a function that computes the integer logarithm in base two of an integer number: Log 2 (n) = y 2 y n. Looking for y. int naturalLog(int number){ int result = 0; int value = 1; while ( value*2 <= number){ value = value*2; result = result + 1; } return result; } int naturalLog(int number){ int result = 0; int value = 1; while ( (value = value*2) <= number) result = result + 1; return result; } Typical C-style of writing

While loop code structure The while loop uses variables These variables have to be appropriately initialized The body of the loop has to make sure loop condition will eventually become false, otherwise you have an infinite loop. And it should terminate exactly when desired computation is finished! … While (condition){ … } Variable initialization Update appropriate variable(s) to eventually exit loop when computation is achieved

Writing code is easy Writing correct, well-written code is not easy Writing loops is easy Writing correct loops is not. Therefore be: DISCIPLINED!!!!!