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); This is not feasible!
scanf(“%d”,&a2);
…
scanf(“%d”,&a50);
sum = a1 + a2 + a3 + … + a50;
Repetition
int a, sum=0;
scanf(“%d”,&a);
sum = sum + a; This is not feasible
scanf(“%d”,&a); either!
sum = sum + a;
…
scanf(“%d”,&a);
sum = sum + a;
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, Iteration Control
and
print their sum and average
set sum to 0 Initialize
set count to 0
input totalNumbers
while (count < totalNumbers)
{
input nextNum
Check condition
add nextNum to sum
add 1 to count
}
Update
output "Sum was" sum
output "Mean was" sum/count
Example: addnum.c (cont) #include <stdio.h>
/**********************************\
Read in numbers and add them up
Read in numbers, add them, Print out the sum and the average
and \**********************************/
print their sum and average int main()
{
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
return 0;
}
Example: addnum.c (cont) #include <stdio.h>
/**********************************\
Read in numbers and add them up
Read in numbers, add them, Print out the sum and the average
and \**********************************/
print their sum and average int main()
{
float nextNum, sum = 0.0;
set sum to 0 int count = 0, totalNumbers;
set count to 0
input totalNumbers
while (count < totalNumbers) only the variables sum and
{ count are initialized to 0
input nextNum
add nextNum to sum
add 1 to count
}
output "Sum was" sum
output "Mean was" sum/count
return 0;
}
Example: addnum.c (cont) #include <stdio.h>
/**********************************\
. Read in numbers and add them up
Read in numbers, add them, Print out the sum and the average
and \**********************************/
print their sum and average int main()
{
float nextNum, sum = 0.0;
int count = 0, totalNumbers;
set sum to 0
scanf("%d", &totalNumbers);
set count to 0
input totalNumbers
while (count < totalNumbers)
{
input nextNum
add nextNum to sum
add 1 to count
}
output "Sum was" sum
return 0;
output "Mean was" sum/count
}
Example: addnum.c (cont) #include <stdio.h>
/**********************************\
Read in numbers and add them up
Read in numbers, add them, Print out the sum and the average
and \**********************************/
print their sum and average int main()
{
float nextNum, sum = 0.0;
set sum to 0 int count = 0, totalNumbers;
set count to 0 scanf("%d", &totalNumbers);
input totalNumbers
while (count < totalNumbers)
while (count < totalNumbers) {
{
input nextNum
add nextNum to sum
add 1 to count }
}
output "Sum was" sum
output "Mean was" sum/count return 0;
}
Example: addnum.c (cont) #include <stdio.h>
/**********************************\
Read in numbers and add them up
Print out the sum and the average
Read in numbers, add them, \**********************************/
and int main()
print their sum and average {
float nextNum, sum = 0.0;
set sum to 0 int count = 0, totalNumbers;
set count to 0 scanf("%d", &totalNumbers);
input totalNumbers
while (count < totalNumbers)
while (count < totalNumbers) {
{ scanf("%f", &nextNum);
input nextNum sum += nextNum;
add nextNum to sum count++;
add 1 to count }
}
output "Sum was" sum
Same
Same
as:
as:count
sum == sum
count + nextNum;
+ 1;
Decrement:
output "Mean was" sum/countOthers: -=,count
*=,0;
return /=, etc.
--;
}
Example: addnum.c (cont) #include <stdio.h>
/**********************************\
Read in numbers and add them up
Print out the sum and the average
Read in numbers, add them, \**********************************/
and int main()
print their sum and average {
float nextNum, sum = 0.0;
set sum to 0 int count = 0, totalNumbers;
set count to 0 scanf("%d", &totalNumbers);
input totalNumbers
while (count < totalNumbers)
while (count < totalNumbers) {
{ scanf("%f", &nextNum);
input nextNum sum += nextNum;
add nextNum to sum count++;
add 1 to count }
}
printf("Sum was %f\n",sum);
output "Sum was" sum printf("Mean was %f\n",sum/count);
output "Mean was" sum/count return 0;
}
Example: addnum.c (cont) #include <stdio.h>
/**********************************\
Read in numbers and add them up
Print out the sum and the average
Read in numbers, add them,
\**********************************/
and
int main()
print their sum and
{
average
float nextNum, sum = 0.0;
int count = 0, totalNumbers;
set sum to 0 scanf("%d", &totalNumbers);
set count to 0
input totalNumbers while (count < totalNumbers)
{
while (count < totalNumbers) scanf("%f", &nextNum);
{ sum += nextNum;
input nextNum count++;
add nextNum to sum }
add 1 to count
} printf("Sum was %f\n",sum);
printf("Mean was %f\n",sum/count);
output "Sum was" sum return 0;
output "Mean was" sum/count }
#include <stdio.h>
/**********************************\ Example: addnum.c (cont)
Read in numbers and add them up
Print out the sum and the average totalNu count nextNu sum
\**********************************/ mbers m
int main()
{ ???? 0 ???? 0.0
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;
}
#include <stdio.h>
/**********************************\ Example: addnum.c (cont)
Read in numbers and add them up
Print out the sum and the average totalNu count nextNu sum
\**********************************/ mbers m
int main()
{ ???? 0 ???? 0.0
float nextNum, sum = 0.0;
int count = 0, totalNumbers;
scanf("%d", &totalNumbers); 3
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>
/**********************************\ Example: addnum.c (cont)
Read in numbers and add them up
Print out the sum and the average totalNu count nextNu sum
\**********************************/ mbers m
int main()
{ ???? 0 ???? 0.0
float nextNum, sum = 0.0;
int count = 0, totalNumbers;
scanf("%d", &totalNumbers); 3
while (count < totalNumbers) 1 4 4.0
{
scanf("%f", &nextNum);
sum += nextNum;
count++;
}
printf("Sum was %f\n",sum);
printf("Mean was %f\n",sum/count);
return 0;
}
#include <stdio.h>
/**********************************\ Example: addnum.c (cont)
Read in numbers and add them up
Print out the sum and the average totalNu count nextNu sum
\**********************************/ mbers m
int main()
{ ???? 0 ???? 0.0
float nextNum, sum = 0.0;
int count = 0, totalNumbers;
scanf("%d", &totalNumbers); 3
while (count < totalNumbers) 1 4 4.0
{
scanf("%f", &nextNum); 2 -1 3.0
sum += nextNum;
count++;
}
printf("Sum was %f\n",sum);
printf("Mean was %f\n",sum/count);
return 0;
}
#include <stdio.h>
/**********************************\ Example: addnum.c (cont)
Read in numbers and add them up
Print out the sum and the average totalNu count nextNu sum
\**********************************/ mbers m
int main()
{ ???? 0 ???? 0.0
float nextNum, sum = 0.0;
int count = 0, totalNumbers;
scanf("%d", &totalNumbers); 3
while (count < totalNumbers) 1 4 4.0
{
scanf("%f", &nextNum); 2 -1 3.0
sum += nextNum;
count++;
3 6.2 9.2
}
printf("Sum was %f\n",sum);
printf("Mean was %f\n",sum/count);
return 0;
}
Syntax for while statement
• while (loop repetition condition)
statement;
OR
• while (loop repetition condition)
{
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
do
{
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
#include <stdio.h>
Example: addfor.c (cont) /**********************************\
Read in numbers and add them up
Print out the sum and the average
Read in numbers, add them, \**********************************/
and int main()
print the sum and the {
float nextNum, sum = 0.0;
average int count, totalNumbers;
set sum to 0
scanf("%d", &totalNumbers);
set count to 0
input totalNumbers for ( count=0;
count < totalNumbers;
while (count < totalNumbers) count++ )
{ {
scanf("%f", &nextNum);
input nextNum
sum += nextNum;
add nextNum to sum }
add 1 to count
} printf("Sum was %f\n",sum);
printf("Mean was %f\n",sum/count);
output "Sum was" sum
return 0;
output "Mean was" sum/count }
#include <stdio.h>
Example: addfor.c (cont) /**********************************\
Read in numbers and add them up
Print out the sum and the average
Read in numbers, add them, \**********************************/
and int main()
print the sum and the {
average Initialize
float nextNum, sum = 0.0;
int count, totalNumbers;
set sum to 0
scanf("%d", &totalNumbers);
set count to 0
for ( count=0;
input totalNumbers count < totalNumbers;
count++ )
while (count < totalNumbers) {
scanf("%f", &nextNum);
{
sum += nextNum;
input nextNum }
add nextNum to sum
add 1 to count printf("Sum was %f\n",sum);
} printf("Mean was %f\n",sum/count);
return 0;
output "Sum was" sum }
output "Mean was" sum/count
#include <stdio.h>
Example: addfor.c (cont) /**********************************\
Read in numbers and add them up
Print out the sum and the average
Read in numbers, add them,
\**********************************/
and int main()
print the sum and the {
average Check condition
float nextNum, sum = 0.0;
int count, totalNumbers;
set sum to 0
set count to 0 scanf("%d", &totalNumbers);
for ( count=0;
input totalNumbers count < totalNumbers;
count++ )
while (count < totalNumbers) {
{ scanf("%f", &nextNum);
input nextNum sum += nextNum;
}
add nextNum to sum
add 1 to count printf("Sum was %f\n",sum);
} printf("Mean was %f\n",sum/count);
output "Sum was" sum return 0;
}
output "Mean was" sum/count
#include <stdio.h>
Example: addfor.c (cont) /**********************************\
Read in numbers and add them up
Print out the sum and the average
Read in numbers, add them,
\**********************************/
and int main()
print the sum and the {
average Update (aka Increment Step)
float nextNum, sum = 0.0;
int count, totalNumbers;
set sum to 0
set count to 0 scanf("%d", &totalNumbers);
for ( count=0;
input totalNumbers count < totalNumbers;
count++ )
while (count < totalNumbers) {
{ IMPORTANT!! scanf("%f", &nextNum);
sum += nextNum;
input nextNum
The Update is performed AFTER
add nextNum to sum
}
}
the body of the
add 1 to count
loop
printf("Sum was %f\n",sum);
printf("Mean was %f\n",sum/count);
output "Sum was" sum return 0;
}
output "Mean was" sum/count
#include <stdio.h>
Example: addfor.c (cont) /**********************************\
Read in numbers and add them up
Print out the sum and the average
Read in numbers, add them,
\**********************************/
and int main()
print the sum and the {
average float nextNum, sum = 0.0;
int count, totalNumbers;
set sum to 0
set count to 0 scanf("%d", &totalNumbers);
for ( count=0;
input totalNumbers count < totalNumbers;
count++ )
while (count < totalNumbers) {
{ scanf("%f", &nextNum);
input nextNum sum += nextNum;
}
add nextNum to sum
add 1 to count printf("Sum was %f\n",sum);
} printf("Mean was %f\n",sum/count);
output "Sum was" sum return 0;
}
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> #include <stdio.h>
int main() int main()
{ {
float nextNum, sum = 0.0; float nextNum, sum = 0.0;
int count, totalNumbers; int count, totalNumbers;
scanf("%d", &totalNumbers); scanf("%d", &totalNumbers);
count = 0; for ( count=0;
while (count < totalNumbers) count < totalNumbers;
count++ )
{ {
scanf("%f", &nextNum); scanf("%f", &nextNum);
sum += nextNum; sum += nextNum;
count++; }
}
printf("Sum was %f\n",sum); printf("Sum was %f\n",sum);
printf("Mean was %f\n", printf("Mean was %f\n",
sum/count); sum/count);
return 0; return 0;
} }
while and for (cont)
#include <stdio.h> #include <stdio.h>
int main() int main()
{
Check
Initialize
condition
float nextNum, sum = 0.0;
{
float nextNum, sum = 0.0;
int count, totalNumbers;
int count, totalNumbers;
scanf("%d", &totalNumbers);
scanf("%d", &totalNumbers);
count = 0;
while (count < totalNumbers) for ( count=0;
count < totalNumbers;
{ count++ )
scanf("%f", &nextNum); {
sum += nextNum; scanf("%f", &nextNum);
count++;
sum += nextNum;
}
}
printf("Sum was %f\n",sum);
printf("Mean was %f\n", printf("Sum was %f\n",sum);
sum/count); printf("Mean was %f\n",
sum/count);
return 0;
} return 0;
}
while and for (cont)
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
Check condition
float nextNum, sum = 0.0;
int count, totalNumbers;
float nextNum, sum = 0.0;
int count, totalNumbers;
scanf("%d", &totalNumbers);
scanf("%d", &totalNumbers);
count = 0;
while (count < totalNumbers) for ( count=0;
count < totalNumbers;
{ count++ )
scanf("%f", &nextNum); {
sum += nextNum; scanf("%f", &nextNum);
count++;
sum += nextNum;
}
}
printf("Sum was %f\n",sum);
printf("Mean was %f\n", printf("Sum was %f\n",sum);
sum/count); printf("Mean was %f\n",
sum/count);
return 0;
} return 0;
}
while and for (cont)
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
float nextNum, sum = 0.0; float nextNum, sum = 0.0;
int count, totalNumbers;
int count, totalNumbers;
scanf("%d", &totalNumbers);
scanf("%d", &totalNumbers);
count = 0;
while (count < totalNumbers) for ( count=0;
count < totalNumbers;
{ count++ )
scanf("%f", &nextNum); {
sum += nextNum; scanf("%f", &nextNum);
count++;
sum += nextNum;
}
}
printf("Sum was %f\n",sum);
printf("Mean was %f\n", printf("Sum was %f\n",sum);
sum/count); printf("Mean was %f\n",
sum/count);
return 0;
} Update return 0;
}