Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Assist.Prof.Dr. Nükhet ÖZBEK Ege University"— Presentation transcript:

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

2 Topics Repetition in programs while statement do while statement
for statement

3 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

4 Repetition

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

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

7 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)

8 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

9 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

10 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

11 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

12 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

13 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

14 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.

15 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

16 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

17 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

18 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

19 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

20 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

21 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

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

23 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”);

24 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”); }

25 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

26 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++; }

27 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 );

28 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 );

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

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

31 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!

32 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

33 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

34 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

35 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

36 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

37 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

38 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) ;

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

40 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;

41 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

42 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

43 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


Download ppt "Assist.Prof.Dr. Nükhet ÖZBEK Ege University"

Similar presentations


Ads by Google