Week 4 – Chapter 3 Repetition.

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Looping Structures: Do Loops
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Repetition and while loops. Assignments Due – Lab 4.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Control Structures for Loops.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Chapter 5 Repetition and Loop Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Decisions – Chapter 3 IPC144 Week 2. IPC144 Introduction to Programming Using C Week 2 – Lesson 2 (Pages 12 to 18 in IPC144 Textbook)
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
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.
Previously Repetition Structures While, Do-While, For.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Iterations Very Useful: Ability to repeat a block of code Example:
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.
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;
How to design and code functions Chapter 4 (ctd).
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
while Repetition Structure
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Structured Program Development
Iterations Programming Condition Controlled Loops (WHILE Loop)
Logical Operators and While Loops
Control Structures Lecture 7.
Looping.
The while Looping Structure
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Control Structure Senior Lecturer
Repetition Statements
Lec 7.
Example: Finding the Mode
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
The while Looping Structure
The ‘while’ loop ‘round and ‘round we go.
Chapter 2.1 Repetition.
Repetition and Loop Statements
Looping III (do … while statement)
Let’s all Repeat Together
A LESSON IN LOOPING What is a loop?
Logical Operators and While Loops
CPS125 Week
Repetition Statements (Loops) - 2
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.
EECE.2160 ECE Application Programming
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
The while Looping Structure
The while Looping Structure
EECE.2160 ECE Application Programming
ICS103: Programming in C 5: Repetition and Loop Statements
Week 3 – Repetition (ctd.)
Presentation transcript:

Week 4 – Chapter 3 Repetition

Input Validation using If statement To process only valid inputs you could use an if statement to test for invalid inputs For example an invalid input for number of doughnuts sold would be any negative number if ( quantity <= 0 ) printf("Number of doughnuts must be > 0!\n"); else { … } Have to rerun program if input is invalid Would be better if program allows repetition: to repeatedly asks user to enter input until it is valid

Loop – Repetition of Code Often need to repeat execution of a group of statements – for example, for input validation or calculations In programming this repetition is called looping Can be accomplished in C by: while, do while and for statements while statement syntax: while ( condition ) statement; statement is repeatedly executed provided condition is true

While Example : Input validation … printf("Enter number of doughnuts: "); scanf("%d", &quantity); while ( quantity <= 0 ) { printf(“Must have 1 or more doughnuts!\n"); scanf ("%d", &quantity); }

While Example: Calculation of average … int count = 0; printf(“How many students? "); scanf("%d", &numberStudents); while ( count < numberStudents ) { printf("Enter mark:"); scanf("%d", &mark); classTotal = classTotal + mark; count = count + 1; } average = classTotal / numberStudents; printf("Class average: %.1lf\n", average); count++; warning! – must not be integer division