CGS 3460 Program looping n Why we need loop lMake code concise for repetitive processes n When to use loop lRun a block of code repetitively lProcess multiple.

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Nested Loops. Problem Print The only printfs you can use are: –printf(“*”); –printf(“\n”); *****
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
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.
Lecture 6: for Loops Yoni Fridman 7/6/01 7/6/01. OutlineOutline  The for statement  The for statement’s format  The for statement’s flow  Comparing.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
ICS103 Programming in C Lecture 9: Functions I
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
Guidelines for working with Microsoft Visual Studio 6.
Introduction to Computer Programming in c
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Loop Control Structures L11-L14. OBJECTIVES To learn and appreciate the following concept The for Statement Relational Operators Nested for Loops for.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
do - while  while: Execute the body of the loop at least once
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 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Chapter 5: Structured Programming
Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.
Introduction to Programming Lecture 7: Repeating Statements.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Program Looping Why we need loops in our code –Make code concise for repetitive processes When to use loops –Run a block of code repetitively –Process.
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
CGS 3460 Program looping n Why we need loop lMake code concise for repetitive processes n When to use loop lRun a block of code repetitively lProcess multiple.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Control Structures (Repetition structure) Jump Statements
CSE 220 – C Programming Loops.
REPETITION CONTROL STRUCTURE
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Repetition-Counter control Loop
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Functions.
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Repetition and Loop Statements
Control Structure Senior Lecturer
Lec 7.
Chapter 6 Decision Making and Looping
Lec 6.
Loops in C.
A function with one argument
Program Control Topics While loop For loop Switch statement
Incremental operators
Computing Fundamentals
Alternate Version of STARTING OUT WITH C++ 4th Edition
Computer programming Lecture 3.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
EECE.2160 ECE Application Programming
REPETITION Why Repetition?
Chapter 4: Loops and Iteration
Presentation transcript:

CGS 3460 Program looping n Why we need loop lMake code concise for repetitive processes n When to use loop lRun a block of code repetitively lProcess multiple data using same procedure n How to use loop lfor lwhile ldo

CGS 3460 Example – Calc Factorial n Goal: computing factorial N (N!) F(N) = N! = 1 * 2 * 3 * …… N n Fact F(1) = 1; F(2) = 1 * 2 = F(1) * 2; F(3) = 1 * 2 * 3 = F(2) * 3; … F(N) = 1 * 2 * … * N = F(N-1) * N;

CGS 3460 Example – Calc Factorial (cont.) F(N) = F(N-1) * N; F = F * M;F = 1;M = 1;M = M +1; When to stop? M equals to N

CGS 3460 Calc Factorial – Data Flow F(N) = F(N-1) * N; n Initial setting: M = 1; F = 1; n Main calculation F = F * M; n Stop criteria M = N n What else increase M by 1 M = M + 1; F = F * M; F = 1;M = 1; M = M +1;

CGS 3460 Flow Chart Components n Help to document program logic * Not required.

CGS 3460 Calc Factorial – Flow chart F(N) = F(N-1) * N; n Initial setting: M = 1; F = 1; n Main calculation F = F * M; n Stop criteria M = N n What else increase M by 1 M = M + 1;

CGS 3460 for loop n Format: for( init_expression; loop_condition; loop_expression ) { program statement; } n Flow: Condition satisfied? No Initial Expression Yes Program statement loop expression

CGS 3460 Calculate factorial for( init_expression; loop_condition; loop_expression ) { program statement; } n Correspondingly when F(N) init_expression: loop_condition: loop_expression: program statement: M = 1; M <= N; M = M + 1; F = F * M;

CGS 3460 Calc Factorial – code #include int main(void) { int F, N, M; F = 1; N = 10; for(M=1; M<=N; M=M +1) { F = F * M; } printf(“result is: %i \n”, F); return 0; }

CGS 3460 init_expression n Set initial values before the loop begins lCan be multiple valid C program statements, separated by comma (,) for( i = 0, j = 0; i < 10; ++i ) lMay be omitted if initial values have been set before Make sure to put an empty statement with only semicolon (;) for(; i<10; i++) lDeclaring variables in the loop

CGS 3460 loop_expression n Change values after the program statements in the for loop lCan be multiple valid C program statements, separated by comma (,) for(i = 0; i < 10; j++,++i ) lMay be omitted put nothing for(; i<10; ) Make sure the value for i has been changed within the for loop!

CGS 3460 loop_condition n Relational expression stating when loop continues OperatorMeaningExample ==Equal toCount == 10 !=Not equal toCount != 10 <Less thanCount < 10 <=Less than or equal toCount <= 10 >Greater thanCount > 10 >=Greater than or equal toCount >= 10

CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count == 5; count++) { … } count 1 Loop condition satisfied? no

CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count != 5; count++) { … } count Loop condition satisfied? Yes no

CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count < 5; count++) { … } count Loop condition satisfied? Yes no

CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count <= 5; count++) { … } count Loop condition satisfied? Yes no

CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count > 5; count++) { … } count 1 Loop condition satisfied? no

CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count >= 5; count++) { … } count 1 Loop condition satisfied? no

CGS 3460 loop_condition – examples 1.for(count = 1; count == 5; count++) { … } 2.for(count = 1; count != 5; count++) { … } 3.for(count = 1; count <5; count++) { … } 4.for(count = 1; count <=5; count++) { … } 5.for(count = 1; count >5; count++) { … } 6.for(count = 1; count >=5; count++) { … } What is the value of count after the for loop? count

CGS 3460 Nested for Loops n Insert a loop within the loop of another for( i=1; i<10; i++) { for(j=1; j<10; j++) { …; } …; }

CGS 3460 Example n If we want to print following pattern * ** *** **** ***** ****** ******* ******** ********* ********** Print n stars at the nth line Print 1 star at the 1st line Print 2 stars at the 2nd line Print 3 stars at the 3rd line

CGS 3460 Code #include int main(void) { int row, col; for (row = 1; row <= 5; row++) { for (col = 1; col <= row; col++) { printf("*"); } printf("\n");

CGS 3460 Print Factorial F(1) … F(10) #include int main(void) { int F, N, M; F = 1; //variable that holds the result N = 10; /*what to calculate the factorial of*/ printf("num \t factorial \n"); for(M=1; M<=N; M=M+1) { F = F * M; printf("%i \t %i \n", M, F); } return 0; }

CGS 3460 Calculate Fibonacci Numbers (0, 1, 1, 2, 3, 5, 8, 13, …) initial value: init_expression: loop_condition: loop_expression: program statement: N = 2; N <= M; N = N + 1; Fn = Fnm1 + Fnm2; Fnm1 = 1; Fnm2 = 0; What else? Fnm2 = Fnm1; Fnm1 = Fn;

CGS 3460 Pseudo code M = 6; n; Fn, Fnm1=1, Fnm2=0; for(n = 2; n <= M; n++) { Fn = Fnm1 + Fnm2; Fnm2 = Fnm1; Fnm1 = Fn } nn <= MFnFnm2Fnm1 ***01 2T111 3T212 4T323 5T535 6T858 7F

CGS 3460 #include int main(void) { int m = 0; int Fn = 0; int Fnm1 = 1; int Fnm2 = 0; /* print out the first two numbers */ printf("F(%i) = %i\n", 0, 1); printf("F(%i) = %i\n", 1, 1); /* print out the next 38 numbers */ for (n = 2; n < 40; n++) { /* calculate the next number and print it */ Fn = Fnm1 + Fnm2; printf("F(%i) = %i\n", n, Fn); /* update the old two numbers for next time through the loop */ Fnm2 = Fnm1; Fnm1 = Fn; } /* no error */ return 0; }