Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 3: Control Flow S. M. Farhad. Statements and Blocks An expression becomes a statement when it is followed by a semicolon Braces { and } are used.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
While loops.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Fundamental of C programming
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and Final Review Topics Lecture.
Decisions If statements in C.
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
More on Algorithms and Problem Solving
Decision Making EE2372 Software Design I Dr. Gerardo Rosiles.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
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 3P. 1Winter Quarter Structured Engineering.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
CPS120 Introduction to Computer Science Iteration (Looping)
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
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,
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
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.
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.
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.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
COMP Loop Statements Yi Hong May 21, 2015.
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
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.
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.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 4 – C Program Control
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Programming Fundamentals Lecture #6 Program Control
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Repetition Control Structure
Selection Statements.
Alternate Version of STARTING OUT WITH C++ 4th Edition
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSC215 Lecture Control Flow.
Switch Case Structures
Presentation transcript:

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

Engineering H192 - Computer Programming The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 int main ( ) { int score ;

Engineering H192 - Computer Programming The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 int main ( ) { int score;

Engineering H192 - Computer Programming The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 The Ohio State University 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 The Ohio State University Gateway Engineering Education Coalition Lect 10P. 20Winter Quarter Example of For Loop #include int 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 The Ohio State University Gateway Engineering Education Coalition Lect 10P. 21Winter Quarter Another Example of For Loop #include int main ( ) { int k = 1, n = 12 ; for ( ; k 6 ; ) printf ("k=%d, n=%d\n", k++, n-- ) ; }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 22Winter Quarter A More Complex Example of For Loop #include int 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++ ; }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 23Winter Quarter Problem G09 Problem G09 combines the while loop, the for loop, and the switch/case structure plus other program elements that we have used previously. From the algorithm develop your own pseudo code for this program. Break the program into parts and then write, compile, and test the individual parts. Do NOT try to write the entire program without compiling Make variables mnemonic using three or more characters

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 24Winter Quarter General Structure of Program #include int main( ) { –/*declare variables */ –/* start loop */ }