Nearby lessons

60 of 124

C - Array with Loop

Arrays and loops are made for each other. Learn the standard traversal patterns in C — filling, printing, searching, summing, copying, shifting and simple sorting — using for, while and do while.

The Three Loops, One Job

Any of C's loops can walk an array. The for loop wins because it keeps the counter, the limit and the step together:

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[5] = {10, 20, 30, 40, 50};
6 int i;
7 
8 printf("for : ");
9 for (i = 0; i < 5; i++) printf("%d ", a[i]);
10 
11 printf("\nwhile : ");
12 i = 0;
13 while (i < 5) { printf("%d ", a[i]); i++; }
14 
15 printf("\ndo while : ");
16 i = 0;
17 do { printf("%d ", a[i]); i++; } while (i < 5);
18 printf("\n");
19 return 0;
20}
Output
for       : 10 20 30 40 50
while     : 10 20 30 40 50
do while  : 10 20 30 40 50 

Filling an Array

A loop can fill from a formula or from user input. Here both, side by side:

Example02
CCode Cell
1#include <stdio.h>
2 
3#define SIZE 6
4 
5int main()
6{
7 int squares[SIZE], evens[SIZE];
8 int i;
9 
10 for (i = 0; i < SIZE; i++)
11 {
12 squares[i] = (i + 1) * (i + 1);
13 evens[i] = (i + 1) * 2;
14 }
15 
16 printf("Squares: ");
17 for (i = 0; i < SIZE; i++) printf("%d ", squares[i]);
18 printf("\nEvens : ");
19 for (i = 0; i < SIZE; i++) printf("%d ", evens[i]);
20 printf("\n");
21 return 0;
22}
Output
Squares: 1 4 9 16 25 36
Evens  : 2 4 6 8 10 12 

Copying an Array

b = a; is not legal C. Copy element by element — this is why the loop matters:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[5] = {1, 2, 3, 4, 5};
6 int b[5];
7 int i;
8 
9 /* b = a; INVALID in C */
10 
11 for (i = 0; i < 5; i++)
12 b[i] = a[i];
13 
14 b[0] = 99; /* proves they are independent copies */
15 
16 printf("a: ");
17 for (i = 0; i < 5; i++) printf("%d ", a[i]);
18 printf("\nb: ");
19 for (i = 0; i < 5; i++) printf("%d ", b[i]);
20 printf("\n");
21 return 0;
22}
Output
a: 1 2 3 4 5
b: 99 2 3 4 5 

break and continue

break leaves the loop entirely; continue skips to the next element:

Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[8] = {4, 7, 12, 0, 9, 15, 3, 20};
6 int i, sum = 0;
7 
8 /* continue - skip odd numbers */
9 printf("Even values: ");
10 for (i = 0; i < 8; i++)
11 {
12 if (a[i] % 2 != 0) continue;
13 printf("%d ", a[i]);
14 }
15 
16 /* break - stop at the first zero */
17 printf("\nSum until zero: ");
18 for (i = 0; i < 8; i++)
19 {
20 if (a[i] == 0) break;
21 sum += a[i];
22 }
23 printf("%d\n", sum);
24 return 0;
25}
Output
Even values: 4 12 0 20
Sum until zero: 23

Shifting Elements

Deleting from the middle means shifting everything after it one place left — a classic loop exercise:

Example05
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[6] = {10, 20, 30, 40, 50, 60};
6 int size = 6, pos = 2, i; /* remove index 2 (the 30) */
7 
8 for (i = pos; i < size - 1; i++)
9 a[i] = a[i + 1]; /* pull the next one back */
10 size--;
11 
12 printf("After removing index 2: ");
13 for (i = 0; i < size; i++) printf("%d ", a[i]);
14 printf("\n");
15 return 0;
16}
Output
After removing index 2: 10 20 40 50 60 

Nested Loops — Bubble Sort

Comparing every pair needs two loops. Bubble sort repeatedly swaps neighbours until the array is ordered:

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[6] = {45, 12, 89, 33, 67, 8};
6 int size = 6, i, j, temp;
7 
8 for (i = 0; i < size - 1; i++)
9 for (j = 0; j < size - 1 - i; j++)
10 if (a[j] > a[j + 1])
11 {
12 temp = a[j];
13 a[j] = a[j + 1];
14 a[j + 1] = temp;
15 }
16 
17 printf("Sorted: ");
18 for (i = 0; i < size; i++) printf("%d ", a[i]);
19 printf("\n");
20 return 0;
21}
Output
Sorted: 8 12 33 45 67 89 

Merging Two Arrays

Two loops, one destination index that keeps counting across both:

Example07
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[3] = {1, 3, 5};
6 int b[4] = {2, 4, 6, 8};
7 int merged[7];
8 int i, k = 0;
9 
10 for (i = 0; i < 3; i++) merged[k++] = a[i];
11 for (i = 0; i < 4; i++) merged[k++] = b[i];
12 
13 printf("Merged: ");
14 for (i = 0; i < k; i++) printf("%d ", merged[i]);
15 printf("\n");
16 return 0;
17}
Output
Merged: 1 3 5 2 4 6 8 

Frequency Counting

Use the value itself as the index of a counter array — a fast, loop-driven counting technique:

Example08
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[10] = {2, 5, 2, 8, 5, 2, 9, 8, 5, 2};
6 int count[10] = {0}; /* counters for values 0-9 */
7 int i;
8 
9 for (i = 0; i < 10; i++)
10 count[a[i]]++; /* the value IS the index */
11 
12 printf("Value : Frequency\n");
13 for (i = 0; i < 10; i++)
14 if (count[i] > 0)
15 printf(" %d : %d\n", i, count[i]);
16 return 0;
17}
Output
Value : Frequency
  2   :   4
  5   :   3
  8   :   2
  9   :   1

Common Mistakes

MistakeEffect
i <= sizeReads one element past the end
Forgetting i++ in a whileInfinite loop
b = a; to copyCompiler error — use a loop
Shifting with i < sizeReads a[size], out of bounds
Declaring i inside and using it outsideOut-of-scope error
Trainer's Note: In the frequency-count example, count[a[i]]++ only works if every value in a is a valid index into count. A single value of 15 in a 10-element counter array corrupts memory silently. Always know your value range before using this trick.
📝 Key Takeaways
  • The for loop is the natural choice: init, condition, update in one line.
  • Always loop with i < size.
  • Copy arrays element by element — b = a does not work.
  • Nested loops handle sorting and comparison of pairs.
  • break exits early; continue skips one element.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4