Arrays and Loops

Updated on January 9, 2026
2 min read

Why Arrays + Loops Go Together?

Arrays and loops are taught together because loops make arrays useful.

 Without loops, handling arrays is slow and messy.

1. Why Arrays + Loops Go Together

  • Array stores many values
  • Loop processes those values one by one

Think like this:

Array = data
 Loop = worker that visits each data item

2. Core Idea

A loop is used to access, input, output, and process array elements using their index.

3. Visual Idea: Loop Traversing an Array

  • Loop starts at index 0
  • Moves step by step
  • Stops at size - 1

4. Basic Example: Print Array Elements

c
1
2#include <stdio.h>
3
4int main() {
5    int a[5] = {2, 8, 7, 6, 0};
6    int i;
7
8    for (i = 0; i < 5; i++) {
9        printf("%d ", a[i]);
10    }
11
12    return 0;
13}

Output

2 8 7 6 0

5. Why i < 5 and NOT i <= 5

  • Valid indexes: 0 to 4
  • a[5] is out of range

Correct rule:

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

6. Input Array Using Loop

c
1
2#include <stdio.h>
3
4int main() {
5    int a[5], i;
6
7    for (i = 0; i < 5; i++) {
8        scanf("%d", &a[i]);
9    }
10
11    return 0;
12}

Loop automatically stores values in:

a[0], a[1], a[2], a[3], a[4]

7. Processing Array Using Loop

Example: Sum of Elements

int sum = 0;

for (i = 0; i < 5; i++) {
    sum = sum + a[i];
}

This is impossible without loops in a clean way.

8. Array + Loop = Logic Building

Common operations done using loops:

  • Sum
  • Maximum / Minimum
  • Searching
  • Sorting
  • Counting

All of these need loops.

9. Using Different Loops with Arrays

for loop (most common)

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

while loop

i = 0;
while (i < n) {
    printf("%d ", a[i]);
    i++;
}

do–while loop

i = 0;
do {
    printf("%d ", a[i]);
    i++;
} while (i < n);

Searching in Array

Searching is the next logical step after arrays + loops.

What is Searching?

Searching means finding a specific value inside an array.

Example:

  • Array: 2 8 7 6 0
  • Find: 7

Why Linear Search?

  • Simplest searching method
  • Uses loop + array
  • Works on unsorted arrays
  • Very important for beginners

How Linear Search Works?

  1. Start from index 0
  2. Compare each element with the key
  3. If match found → stop
  4. If end reached → not found

Algorithm (Easy Steps)

  1. Take array size n
  2. Take element to search (key)
  3. Loop from 0 to n-1
  4. Compare a[i] with key
  5. If equal → found

Example Program (C)

c
1#include <stdio.h>
2
3int main() {
4    int a[5] = {2, 8, 7, 6, 0};
5    int i, key = 7;
6    int found = 0;
7
8    for (i = 0; i < 5; i++) {
9        if (a[i] == key) {
10            printf("Element found at index %d", i);
11            found = 1;
12            break;
13        }
14    }
15
16    if (found == 0) {
17        printf("Element not found");
18    }
19
20    return 0;
21}

Output

Element found at index 2

Important Points for Students

  • Uses for loop
  • Starts from index 0
  • Stops early using break
  • Time complexity: O(n)
Arrays and Loops | C Programming | Learn Syntax