0% found this document useful (0 votes)
20 views15 pages

CLang Lect07

The document discusses different types of loops in C including for, while, and do-while loops. It provides examples of using while and for loops to calculate the sum of integers input by the user. Key points include: (1) while and for loops can be used to repeat a block of code a specified number of times; (2) the while loop checks the condition at the start of each iteration while for initializes, checks, and updates on each pass; (3) common mistakes involve issues with loop syntax and control variables.

Uploaded by

Thành
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

CLang Lect07

The document discusses different types of loops in C including for, while, and do-while loops. It provides examples of using while and for loops to calculate the sum of integers input by the user. Key points include: (1) while and for loops can be used to repeat a block of code a specified number of times; (2) the while loop checks the condition at the start of each iteration while for initializes, checks, and updates on each pass; (3) common mistakes involve issues with loop syntax and control variables.

Uploaded by

Thành
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Loops (1)

Department of Information System


SoICT, HUST

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

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

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;

printf(“Enter the total number of


the array:");
scanf("%d", &totalNumbers);

while (count < totalNumbers)


{
scanf("%d", &nextnum);
sum += nextnum;
count++;
}
printf(“The sum is %d\n",sum);
return 0;
}
5
Common mistakes
while (count < totalNumbers)
scanf("%d", &nextnum); only scanf is
sum+= nextnum; repeated many times
count++;

while (count < totalNumbers);


{ the loop is emply
scanf("%d", &nextnum); (statements are only
sum+= nextnum; ;)
count++;
}
while (count < totalNumbers)
{ print command is
scanf("%d", &nextnum); repeated many times
sum+= nextnum;
count++;
printf(“The sum is %d\n",sum);
}
6
End-of-Input: EOF
Checking for End-of-Input:
• In the example before of calculating the sum of a given
array, in order to determine the end of the array, we have to
enter the total numbers of the array before enter the array.
• Instead of entering the total of numbers for inputting we can
mark the end of the integer number sequence by pressing
Ctrl+D in Unix or Ctrl+Z in DOS.
• The return value of scanf is the number inputted values.
scanf returns EOF if the end of input is detected.

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!

You might also like