Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
While loops.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
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”
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.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
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.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
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
Java Programming: Guided Learning with Early Objects
Looping.
Loop Control Structure.
Programming Fundamentals Lecture #6 Program Control
- Additional C Statements
Chapter 4 - Program Control
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Relational, Logical, and Equality Operators
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
ECE 103 Engineering Programming Chapter 18 Iteration
Repetition Statements (Loops) - 2
Chapter 4 - Program Control
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
Switch Case Structures
Presentation transcript:

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 2Winter Quarter Repetition Structures Repetition structures are used to repeat statements or blocks of code. The decision whether to repeat the code is based on the evaluation of a logical expression. If the expression is true, the code is executed. If false, the code is not executed. Repetition structures may repeat the code a definite number of times (usually for loops) or an indefinite number of times (usually while or do while loops).

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 3Winter Quarter Repetition Structures Both for loops and while loops are top test loops. They evaluate the logical expression before executing any of the code in the loop. If the expression is false, the block of code does not get executed. A do while loop is a bottom test loop. It executes the block of code once and then evaluates the logical expression to determine whether or not to execute it again.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 4Winter Quarter While Loops (Syntax) The syntax of a while loop is: while ( logical expression is true ) statement ; or while ( logical expression is true ) { statement ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 5Winter Quarter While Loops /* This program associates letter grades with numeric test scores. It continues to request input until the user types in a 0 and then it terminates with a message. */ #include void main ( ) { int score ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 6Winter Quarter While Loops score = -1 ; while (score != 0) { printf ("enter a test score (0 to quit) >") ; scanf ("%d", &score) ; if (score >= 90) printf ("Your score of %d is a A\n", score) ; else if (score >= 80 && score <90) printf ("Your score of %d is a B\n", score) ; else if (score >= 70) printf ("Your score of %d is a C\n", score) ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 7Winter Quarter While Loops else if (score >= 60) printf ("Your score of %d is a D\n", score) ; else if (score != 0) printf ("Your score of %d is an E\n", score) ; else printf ("Terminating at your request.\n\n") ; }/* End of While loop */ }/* End of "main" function */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 8Winter Quarter Do-While Loops (Syntax) do statement ; while ( logical expr is true ) ; or, more commonly do { statement ; } while ( logical expression is true ) ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 9Winter Quarter Do-While Loops /* This program associates letter grades with numeric test scores. It continues to request input until the user types in a 0 and then it terminates with a message. */ #include void main ( ) { int score;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 10Winter Quarter Do-While Loops do { printf ("enter a test score (0 to quit) >") ; scanf ("%d", &score) ; if (score >= 90) printf ("Your score of %d is a A\n", score) ; else if (score >= 80 && score <90) printf ("Your score of %d is a B\n", score) ; else if (score >= 70) printf ("Your score of %d is a C\n", score) ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 11Winter Quarter Do-While Loops else if (score >= 60) printf ("Your score of %d is a D\n", score) ; else if (score != 0) printf ("Your score of %d is an E\n", score) ; else printf ("Terminating at your request.\n\n") ; } while (score != 0) ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 12Winter Quarter For Loops (Syntax) for (expr1 ; expr2 ; expr3) { statement(s) ; } Expression1 sets initial conditions. It can be a single math expression or multiple math expressions separated by commas(,). It gets executed only once before the loop executes.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 13Winter Quarter For Loops for ( expr1 ; expr2 ; expr3 ) { statement(s) ; } Expression2 is the logical expression to be evaluated as true or false. It does NOT need to contain any of the variables that are in expression1. It gets evaluated after expression1 and again after expression3.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 14Winter Quarter For Loops for ( expr1 ; expr2 ; expr3 ) { statement(s) ; } Expression3 defines changes to conditions. It can be multiple expressions separated by commas. It gets executed after the statements in the loop are executed. The expressions do NOT have to be related to either those in expression1 or in expression2 (but they often are).

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 15Winter Quarter Expressions in For Loops Any (or all) of the expressions (i.e., expression1, expression2, or expression3) can be eliminated, but the semicolons MUST be used. Eliminating expression1 and expression3 will cause the for loop to behave like a while loop since only expression2 (the logical one) will be left. Eliminating expression2 will create a loop that will never terminate unless there is a break or exit statement inside the loop (or someone pulls the plug.)

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 16Winter Quarter Simple For Loop Example int counter; for (counter = 1; counter <= 10; counter++) { printf ("The counter value is: %2d\n", counter); }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 17Winter Quarter Equivalency of While Loop vs. For Loop The Loop: expression1 ; while ( expression2 ) { statement ; expression3 ; } Is Equivalent to: for ( expr1 ; expr2 ; expr3 ) statement ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 18Winter Quarter Equivalency of While Loop vs. For Loop The Loop: expression1 ; while ( expression2 ) expression3 ; Is Equivalent to: expression1 ; for ( ; expression2 ; ) expression3 ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 19Winter Quarter Simple While Loop Example int counter ; counter = 1 ; while ( counter <= 10 ) { printf ("The counter value is: %2d\n", counter) ; counter++ ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 20Winter Quarter Example of For Loop #include void main ( ) { int k, n ; for (k = 1, n = 12 ; k 6 ; k++, n--) printf ("k=%d, n=%d\n", k, n ) ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 21Winter Quarter Another Example of For Loop #include void main ( ) { int k = 1, n = 12 ; for ( ; k 6 ; ) printf ("k=%d, n=%d\n", k++, n-- ) ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 22Winter Quarter A More Complex Example of For Loop #include void main ( ) { int rand_num, seed, k = 1 ; printf ("Enter a seed for the generator >") ; scanf ("%d", &seed) ; srand (seed) ; for ( ; ; ) { rand_num = * rand ( ) / RAND_MAX ; printf ("The number is %d and k = %d\n", rand_num, k) ; if (rand_num == 6 || k == 2000) break ; k++ ; }