Iterations Very Useful: Ability to repeat a block of code Example:

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CS0004: Introduction to Programming Repetition – Do 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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
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 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
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.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
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.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
The Repetition control structure using while loop.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
ECE Application Programming
ECE Application Programming
REPETITION CONTROL STRUCTURE
ECE Application Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CiS 260: App Dev I Chapter 4: Control Structures II.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Programming Fundamentals
Chapter 5: Control Structures II
Control Structures Lecture 7.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
TOPIC 4: REPETITION CONTROL STRUCTURE
Introduction to Programming
Loops in C.
The while Looping Structure
Week 6 CPS125.
REPETITION STATEMENTS
More Loops Topics Counter-Controlled (Definite) Repetition
EECE.2160 ECE Application Programming
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
EECE.2160 ECE Application Programming
ICS103: Programming in C 5: Repetition and Loop Statements
Week 3 – Repetition (ctd.)
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Iterations Very Useful: Ability to repeat a block of code Example: summing numbers entered at the keyboard Example: Could do int sum; int x1,x2,x3,x4; printf(“Enter 4 numbers: ”); scanf(“%d%d%d%d%d”,&x1,&x2,&x3,&x4); sum = x1 + x2 + x3 + x4; OK, but what if we had 100 numbers! Prefer: sum =0 No get x Use while statement sum = sum + x are we done ? Yes 142 J -1

While Statement int sum=0,x,count=1; printf(“Enter 4 numbers: ”); while(count<=4) { scanf(“%d”,&x); sum = sum + x; count = count + 1; } test expression body of the loop incrementing the counter variable Or more generally: int sum=0,x,count=1; int number_of_entries; printf(“How many numbers? ”); scanf(“%d”,&number_of_entries); while (count<=number_of_entries) { printf(“Enter entry %d: ”,count); scanf(“%d”,&x); sum = sum + x; count = count + 1; } 142 J -2

While Syntax while (condition) { statement1; statement2; ... } the loop body is executed as long as the condition is true Flow Chart True condition ? statement1; statement2; False Note: if the condition is FALSE, the body of the loop is NOT executed (not even once) 142 J -3

Examples Computing factorial n: n! = n(n-1)(n-2)(n-3)...1 e.g. 9! = 9*8*7*6*5*4*3*2*1=362880 How? i>1 get n fact_n = 1 Yes No i=n fact_n = fact_n*i i = i - 1 142 J -4

n! in C int n; int i; int fact_n; printf(“Enter a positive integer: ”); scanf(“%d”,&n); i=n; fact_n=1; while(i>1) { fact_n = fact_n * i; i = i - 1; } 142 J - 5

Another example Goal: compute the time needed to double the amount of money on a 5% interest account With a while loop get the amount goal = 2*amount year = 0 Yes 5% increase year = year + 1 money<goal No 142 J -6

In C double money; double goal; int year=0; printf(“Enter the amount of money: ”); scanf(“%lf”,&money); goal = 2.*money; while(money<goal) { money = money + 0.05*money; year = year + 1; } printf(“My money will double in %d years”, year); 142 J -7

Sentinel controlled loop Motivation: e.g, a program inputs grades from a class with a loop (e.g.a scanf statement within a while loop). How to end the loop when we don’t know the number of students in the class? input a value that can’t be a grade (e.g. -1) and have the condition tests for it scanf(“%lf”,&grade); while(grade != -1) { ... } sentinel value Get data Process data Get new data No sentinel value? Yes 142 J -8

sentinel controlled loop Example of a sentinel controlled loop Find the average of a list of positive numbers: printf (“Enter a positive number (or -1 to end): ”); scanf(“%d”,&n); sum = 0; count = 0; while(n!=-1) { sum = sum + n; count = count + 1; (Enter a positive number (or -1 to end): ”); } if (count!=0) printf(“average=%f”, (double)sum/(double)count); sentinel 142 I -9

A command interpreter Read in commands and execute them input command (single character) if a, execute command a if b, execute command b if q, exit (quit) How? have a variable done set to FALSE while not done read the user’s input select the proper execution if quit, set done to TRUE 142 J -10

Command interpreter in C #define FALSE 0 #define TRUE 1 int main(void){ char command; int done = FALSE; while(!done){ printf(“Input command: ”); scanf(“%c”,&command); switch(command){ case ‘a’: case ‘A’: command_a(); break; case ‘b’: case ‘B’: command_b(); case ‘q’: case ‘Q’: done = TRUE; default: printf(“What?\n”); } /*end of switch */ }/*end of while */ return 0; } 142 J -11