CLang Lect07
CLang Lect07
1
Loops
• is used to repeat a statement or a block of code
several times
• C supports the iteration by different ways to
determine the terminating time of the loop.
• Types of loop in C:
• for
• while
• do…while
2
while statement
while ( expression )
statement
3
Example
#include <stdio.h>
• read in integer numbers and print
out their sum int main(){
int aNum, sum = 0;
sum = 0 int count = 0, totalNumbers;
count = 0 scanf("%d", &totalNumbers);
while (count < totalNumbers)
input totalNumbers
{
while (count < totalNumbers) do scanf("%d", &aNum); There is
sum += aNum; no do
{
count++; here
input next number
add next number to sum }
add 1 to count printf("Sum is %d\n",sum); return 0;
} }
output sum
4
Example (con’t)
#include <stdio.h>
int main()
{
int sum=0, count=0, totalNumbers,
nextnum;
7
Example
#include <stdio.h>
• read in integer numbers
and print out their sum (ver int main()
2) {
int aNum, sum = 0;
Algorithm: (version 2) while
(scanf("%d",&aNum)!=EOF)
sum = 0 {
while (not end of input) sum += aNum;
{ }
input aNum printf("Sum is %d\n", sum);
add aNum to sum return 0;
} }
output sum
8
for statement
for ( initialization; condition; update )
statement
• Form of loop which allows for initialization and
iteration control
• parts of for statement is optional. When the loop
condition is not mentioned explicitly, it takes the
default value (true)
• Update is always done after statement of the loop.
9
Example
• read in integer numbers and print #include <stdio.h>
out their sum int main()
{
sum = 0 int aNum, sum = 0;
count = 0 int count, totalNumbers;
input totalNumbers scanf("%d", &totalNumbers);
for (count=0; count<totalNumbers;
while (count < totalNumbers) do count++)
{
{
input next number
scanf("%d", &aNum);
add next number to sum
add 1 to count sum += aNum;
} }
printf("Sum is %d\n",sum);
output sum return 0;
}
10
Compare while and for
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int sum=0, count=0,
totalNumbers, nextnum;
int aNum, sum = 0;
int count, totalNumbers;
printf(“Enter the total number scanf("%d", &totalNumbers);
of the array:"); for (count=0;
scanf("%d", &totalNumbers); count<totalNumbers;
count++)
while (count < totalNumbers) {
{ scanf("%d", &aNum);
scanf("%d", &nextnum);
sum += aNum;
sum += nextnum;
}
count++;
} printf("Sum is %d\n",sum);
printf(“The sum is %d\n",sum); return 0;
return 0; }
}
11
Common mistakes
for (count=0; count<totalNumbers;)
{ count variable is not updated
scanf("%d", &aNum); after each iteration
sum += aNum;
}
for (count=0;
count<totalNumbers;count++);
{ scanf("%d", &aNum);
; must not be here
sum += aNum;
}
for (count=0,
count<totalNumbers,count++)
{ scanf("%d", &aNum);
sum += aNum; ; not , here
}
12
Comma
• In the for statement initialization; condition; update are optional. If no
condition is given, we have an infinitive loop.
• for (;;) and while(1) are infinitive loops
• Some statements can be given in initialization and update. These
statements must be separated by a comma.
• Example:
for (i=0, j=100; i<=j; i++, j--)
printf(”(%d, %d\n)”, i, j);
Output:
(0, 100)
(1, 99)
…
(49, 51)
(50, 50)
13
Exercises
(i) Write a program that prints all 2-digits numbers
where their sum = 10, for instance 19, 28,…
(ii) Write a program that prints 100 first numbers in
the following sequence: 1 2 3 5 8 13 21…
(iii) Write a program that receives as input a positive
integer n ( n 9), and prints out a triangular as
following if n = 5
1
12
123
1234
12345
14
Thank you
for your
attentions!