Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Slides:



Advertisements
Similar presentations
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Advertisements

1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Some loop programs 1: Read in 10 integers and output their sum
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
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.
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.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CS1010 Programming Methodology
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Iteration statement while do-while
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Control Structures Lecture 7.
Arrays, For loop While loop Do while loop
Looping and Repetition
- Additional C Statements
Outline Altering flow of control Boolean expressions
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Lec 6.
Chapter 4 - Program Control
Chapter 2.1 Repetition.
Repetition Control Structure
Week 6 CPS125.
UMBC CMSC 104 – Section 01, Fall 2016
Computer Science Core Concepts
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
Introduction to Computing Lecture 04: Booleans & Selection
ECE 103 Engineering Programming Chapter 18 Iteration
Chapter 4 - Program Control
PROGRAM FLOWCHART Iteration Statements.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chap 7. Advanced Control Statements in Java
Assignment Operators Topics Increment and Decrement Operators
Dale Roberts, Lecturer IUPUI
The while Looping Structure
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
Week 3 – Repetition (ctd.)
More Loops Topics Counter-Controlled (Definite) Repetition
REPETITION Why Repetition?
Presentation transcript:

Introduction to Computing Lecture 06: Repetition and Loop Statements (Part I) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr

Topics Repetition in programs while statement do while statement for statement

Repetition in programs So far, the statements in the program body executes only once However, sometimes it is necessary to repeat a process many times Repetition steps in a program is called a loop

Repetition

Repetition int a1,a2,a3, … a50, sum; scanf(“%d”,&a1); scanf(“%d”,&a2); sum = a1 + a2 + a3 + … + a50; This is not feasible!

Repetition int a, sum=0; scanf(“%d”,&a); sum = sum + a; … This is not feasible either!

The while statement Implements the repetition in an algorithm Repeatedly executes a block of statements Tests a condition (Boolean expression) at the start of each iteration Terminates when condition becomes false (zero)

Example: addnum.c Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

Example: addnum.c (cont) Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Iteration Control Initialize Check condition Update

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count only the variables sum and count are initialized to 0

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); return 0; } . Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) } return 0; Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } return 0; Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Same as: count = count + 1; Decrement: count --; Same as: sum = sum + nextNum; Others: -=, *=, /=, etc.

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Example: addnum.c (cont) totalNumbers count nextNum sum ???? 0.0

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Example: addnum.c (cont) totalNumbers count nextNum sum ???? 0.0 3

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Example: addnum.c (cont) totalNumbers count nextNum sum ???? 0.0 3 4 1 4.0

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Example: addnum.c (cont) totalNumbers count nextNum sum ???? 0.0 3 4 -1 1 4.0 2 3.0

Example: addnum.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Example: addnum.c (cont) totalNumbers count nextNum sum ???? 0.0 3 4 -1 6.2 1 4.0 2 3.0 3 9.2

Syntax for while statement while (loop repetition condition) statement; OR { statement1; statement2; … statementn; }

Common Mistakes in while -- “one liners” while (num < minimum) scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); while (num < minimum) { scanf(“%d”, &num); } printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”);

Common Mistakes in while -- “one liners” (cont) while (num < minimum) scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); while (num < minimum) { scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); }

Common Mistakes in while -- extra semi-colon; while (num < minimum); { scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); } Marks the end of the while-block -- usual cause of infinite loops

Common Mistakes in while Loop control variable is not updated. “Infinite loop” while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; } while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; }

do … while Implements a post-tested loop do { printf(“Main menu:\n”); printf(“1. Account balance.\n”); printf(“2. Withdraw cash.\n”); printf(“3. Transfer funds.\n\n”); printf(“9. Exit\n\n”); printf(“Please enter your choice: “); scanf(“%d”, &choice); /* Insert selection instructions here. */ } while ( choice != 9 );

do … while (cont) do { printf(“Main menu:\n”); printf(“1. Account balance.\n”); printf(“2. Withdraw cash.\n”); printf(“3. Transfer funds.\n\n”); printf(“9. Exit\n\n”); printf(“Please enter your choice: “); scanf(“%d”, &choice); /* Insert selection instructions here. */ } while ( choice != 9 );

Syntax for do … while statement { statement1; statement2; … statementn; } while (loop repetition condition);

Notes on do … while do … while is executed at least one

The for statement Form of pre-tested loop which allows for initialization and iteration control Syntax: for (initialization; condition; update ) { instruction block } Careful! A semi-colon here marks the end of the instruction block!

Example: addfor.c Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

Example: addfor.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Example: addfor.c (cont) Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

Initialize Example: addfor.c (cont) Read in numbers, add them, and #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Initialize

Check condition Example: addfor.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Check condition

The Update is performed AFTER the body of the loop #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Example: addfor.c (cont) Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Update (aka Increment Step) IMPORTANT!! The Update is performed AFTER the body of the loop

Example: addfor.c (cont) #include <stdio.h> /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; Example: addfor.c (cont) Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

Notes on for What is the value of x when the following statement is complete? for (x = 0; x < 100; x++) ; What is the value of ctr when the following statement is complete? for (ctr = 2; ctr < 10; ctr += 3) ;

Notes on for and while An equivalent for statement for a while statement can be written and vice versa

while and for #include <stdio.h> int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; #include <stdio.h> int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0;

while and for (cont) Check condition Initialize #include <stdio.h> int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; #include <stdio.h> int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; Check condition Initialize

while and for (cont) Check condition #include <stdio.h> int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; #include <stdio.h> int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; Check condition

while and for (cont) Update #include <stdio.h> int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; #include <stdio.h> int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; Update