0% found this document useful (0 votes)
13 views19 pages

Looping (II)

Uploaded by

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

Looping (II)

Uploaded by

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

Looping (II)

Outline
• Motivation

1.for statement
2. Further Examples on Array Processing
3. More For-Loop Examples

2
Motivation
• In our previous lectures and lab, we have learned
how to use while loop
• We have also seen that there are two Basic Cases
of looping:
– Finite repetition
– Indefinite repetition
• Today we are going to look at for loop, a feature
that generally help us do finite repetition

3
1. for statement (Syntax)
• The initialization (init) statement
– Executes once before the condition for (init; condition;
update)
statement.
statement1;
statement2;
• The condition statement
– is the same as in the while-loop condition.
init

• The loop body (statement1) update


– Repeats until the condition statement condition
becomes false. true statement1
false

• The update statement statement2

– Executes after statement1 in each iteration,


and
– is usually for updating the loop condition.
4
1.1. for Statement (Example #1)
1 int i;
2
3 // A simple loop that iterates 5 times
4 for (i = 1; i <= 5; i++) {
5 printf("%d\n", i);
6 }
7
8 printf("Lastly, i = %d\n", i);
9
1
2
3
4
5
Lastly, i = 6

5
1.1. for-loop vs. while-loop

While-Loop 1 int i;
Version 2
3 i = 1;
4 while (i <= 5) {
5 printf("%d\n", i);
6 i++;
7 }

For-Loop 1 int i;
Version 2
Comparison
3 for (i = 1; i <= 5; i++) {
4 printf("%d\n", i);
5 }

6
1.1. for-loop vs. while-loop
• They are "equivalent" in terms of what you can
accomplish with them.
– Any task you can accomplish with one of these loop structures,
you can also accomplish the task with the other loop structure.

• for-loop is more expressive for tasks to be repeated a


finite number of times, i.e. finite repetition.

7
1.2. Example #2 – Multiple ways of writing a for loop
1 int i, num, N; 1 int i, num, N;
2 scanf("%d", &N); 2 scanf("%d", &N);
3 // i increases from 0 to 3 // i increases from 1 to
4 N-1 4 N
5 for (i = 0; i < N; i++) { 5 for (i = 1; i <= N; i++)
6 num = N - i; 6 {
7 printf("%d\n", num); 7 num = N - i + 1;
} printf("%d\n", num);
1 int i, num, N;
Different
} ways to output numbers from
2 scanf("%d", &N);
N to 1 using a for loop.
3 // i decreases from N to
4 1
The numbers we want to generate
5 for (i = N; i >= 1; i--)
inside a loop can usually be expressed
6 {
in terms of the loop variable.
7 num = i;
printf("%d\n", num);
} 8
1.3. Example #3: Arrays and for loop
1 int list[4], i;
We usually use for loops
2
3 printf("Enter 4 #'s: "); to process array elements.
4 for (i = 0; i < 4; i++)
5 scanf("%d", &list[i]);
6
7 // Print the input values in reverse order
8 printf("You have entered:");
9 for (i = 0; i < 4; i++)
10 printf("%d", list[ 4 – i - 1 ]);
11 printf("\n");

Enter 4 #'s: 7 11 45 23
You have entered: 23 45 11 7

Note: Input values can be separated by any white space character.


9
2. Further Examples on Array Processing

• The following few slides are some examples


illustrating how we can process data in arrays
using for loops. The examples include:
– Reading N numbers from the user and store the
numbers in an array
– Computing average of all the numbers in an array
– Finding the largest value in an array

10
1 int N, // # of data
2 num[100], // To store up to 100 numbers
3 i;
4
5 printf("# of data:");
6 scanf("%d", &N); // Assuming 1 <= N <= 100
7
8 for ( i = 0; i < N; i++ )
9 scanf("%d", &num[ i ]);
10

Example 4: Read N integers (N <= 100) from the user and store the
integers in the array num[].

11
1 // Suppose num[] contains N numbers.
2 // (Please refer to Example 4 for the code)
3
4
5 double sum;
6
7 sum = 0.0;
8 for ( i = 0; i < N; i++ )
9 sum += num[ i ];
10
11 printf("Average = %.2f\n", sum / N);
12
13
14

Example 5. Computing the average of the numbers in an array.

12
1 // Suppose num[] contains N numbers.
2 // (Please refer to Example 4 for the code)
3
4 int max;
5
6 max = num[0]; // Assume the 1st element is
7 largest
8
9 for ( i = 1; i < N; i++ ) {
10
11 // Update max if we encounter a larger
12 value
13 if (num[i] > max)
14 max = num[i];
15 }
Example 6: Finding the largest number in an array.
printf("The largest # is %d\n", max);
13
2.1. A Common Mistake
• Typical cause of array out-of-bound exception when using arrays in a
for-loop:
int A[10], i;

for ( i = 0; i <= 10; i++ )


A[i] = i;

Remember, the index of an array element ranges from 0 to “array


size - 1”!

14
3. More For-Loop Examples
• For-loops can be used in solving many general
problems that may not involve arrays.
• A reminder though: it is usually NOT
recommended to use them in indefinite
repetitions.

15
Example #7
• Objective: To output the first N numbers in the
following number series:
1 2 -3 -4 5 6 -7 -8 9 10 -11 -12 …

Notice that these numbers follow the following pattern:


+ve +ve -ve -ve +ve +ve -ve -ve …

and the pattern "+ve +ve -ve -ve" repeats after every four
numbers.

16
Example #7: Solution
1 int i, N;
2
3 printf("N = ? ");
4 scanf("%d", &N);
5
6 for (i = 1; i <= N; i++) {
7 if (i % 4 == 1 || i % 4 == 2)
8 printf("%d ", i);
9 else
10 printf("%d ", -i);
11 }

i 1 2 3 4 5 6 7 8 9 10 11 12 13
i % 1 2 3 0 1 2 3 0 1 2 3 0 1
4
The remainders, i % 4, also repeat every four numbers

17
Summary
• Syntax of for loops
• Comparison of for loops and while loops
• More examples on array processing

18
Reading Assignment
• C: How to Program, 8th ed, Deitel and Deitel
• Chapter 3 Structured Program Development in C
– Sections 3.7 – 3.9
• Chapter 4 C Program Control
– Sections 4.1 – 4.6, 4.9

19

You might also like