Week 3 – Repetition (ctd.)

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

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.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 4 Client Side Scripting JavaScript Looping.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop : for Condition is tested first Loop is controlled by.
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
Repetition (Loops) Want to do some repetitive sequence of actions: print vertical line of *s * Corresponding program: printf(“*\n”);
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
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 /
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
CISC105 – General Computer Science Class 4 – 06/14/2006.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
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.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
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.
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.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
ECE Application Programming
Chapter 4 Repetition Statements (loops)
ECE Application Programming
while Repetition Structure
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Week 4 – Chapter 3 Repetition.
Chapter 5: Loops and Files.
Iteration statement while do-while
Programming Fundamentals
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
The while Looping Structure
Looping and Repetition
Outline Altering flow of control Boolean expressions
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
The while Looping Structure
Chapter 2.1 Repetition.
Repetition and Loop Statements
What output is produced by the following fragment?
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
A LESSON IN LOOPING What is a loop?
More Loops Topics Counter-Controlled (Definite) Repetition
EECE.2160 ECE Application Programming
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Statements in C Programming
More Loops Topics Counter-Controlled (Definite) Repetition
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
More Loops Topics Counter-Controlled (Definite) Repetition
Looping and Repetition
Presentation transcript:

Week 3 – Repetition (ctd.) While, Do-While, For statements

Repetition: Looping Allows execution of statement or block of statements more than once Often used in programs to validate inputs or to process (transform) inputs In C programming language, can use While, Do-While, For statements A single statement or block of statements contained within {} can be repeatedly executed

While Statement (Pre-test Loop) Often want to test the condition before even executing a loop the first time For this use a While or For statement is appropriate; if you don’t know how many times code is to be repeated use a While statement While statement syntax: while (condition) stmt; /* statement is executed as long as condition is true (i.e. until condition is false) */

While Statement Example main() { int numstudents, mark, numread = 0; int total = 0; double avg; printf("Enter number of students: "); scanf("%d", &numstudents); while (numread < numstudents) { printf("Enter mark:"); scanf ("%d", &mark); total = total + mark; numread = numread + 1; } avg = total / numstudents; printf("Average of %d students is: %.2lf\n",numstudents,avg);

For Statement Used if we know how many times a block of code is to be repeated; for example if we want to calculate an average for each student in a class of 10 students we want to repeat it 10 times For statement syntax: for (execute once before entering loop; condition; execute before evaluating condition) stmt;

For Statement Example main() { int i, mark, total = 0; double avg; for (i = 1; i <= 5; i = i+1) { printf("Enter mark %d:",i); scanf ("%d", &mark); total = total + mark; } avg = total / 5; printf("Average of 5 students is: %.2lf\n",avg);

Do-While Statement (Post-test Loop) Often want to execute the statement(s) before testing the condition, even for the first time; i.e. the block of code in the do-while loop is always executed unconditionally the first time Often used for validating input Do - While statement syntax: do stmt; while (condition) /* statement is executed as long as condition is true (i.e. until condition is false) */

Do - While Statement Example main() { int numstudents = 0; do { if (numstudents < 0) printf("Invalid! Number must be >= 0\n"); printf("Enter number of students: "); scanf("%d", &numstudents); } while (numstudents < 0); printf("Number of students in class: %d\n", numstudents); }