STEP 4 Review: for/while, Arrays, and Strings

Review the key points with a cheat sheet, six code-reading questions, and a checklist of common mistakes.

① Key-point cheat sheet

Review the essentials of STEP 4 in these tables .

🔁 Three parts of a for loop

for (int i = 0; i < 3; i++) {
    printf("%d ", i);
}
// Output: 0 1 2 
  • Initialization / condition / update are separated by semicolons
  • A false condition ends the loop
  • Useful when the iteration count is known

🔄 while / do-while

// Check condition → execute 
while (cond) {
    // Zero or more iterations 
}

// Execute → check condition 
do {
    // At least one iteration 
} while (cond);
  • while: when the condition is false initially , the body never executes
  • do-while: at least one iteration executes (a trailing ; is required)
  • Useful when the number of iterations is unknown

🛑 break / continue

for (int i = 0; i < 5; i++) {
    if (i == 3) break;     // Exit the loop → 0 1 2 
    if (i == 2) continue;  // Skip this iteration only 
    printf("%d ", i);
}
  • break: the loop is exited completely
  • continue: skip the rest and move to the next iteration .

📦 Array basics

int a[5] = {10, 20, 30, 40, 50};
// Indices run from 0 to N-1 (0–4 here) 
printf("%d\n", a[0]); // 10
printf("%d\n", a[4]); // 50
// a[5] is out of bounds: undefined behavior 
  • Indices run from 0 to N-1 (for N elements, the last is a[N-1])
  • Out-of-bounds access causes undefined behavior (possible crashes or meaningless values)
  • Works well with for: for(i=0; i<N; i++) ... a[i] ...

🔤 Strings (char arrays)

char s[] = "Hi";
// Memory: 'H' 'i' '\0' — terminator added automatically 

printf("%s\n", s);          // Hi
printf("%zu\n", strlen(s)); // 2 (excluding \0) 
printf("%zu\n", sizeof s); // 3 (array size including \0) 
  • A C string is terminated by \0(a string literal includes it automatically)
  • strlen counts characters before \0; sizeof gives the array size in bytes
  • #include <string.h> declares strlen and related functions

② Six code-reading questions

Execute the code mentally before choosing an answer. Read the explanation even if you get it wrong.

Q1. A for loop's boundary (< versus <=)

for (int i = 1; i <= 5; i++) {
    printf("%d ", i);
}

What is the output?

1 2 3 4
1 2 3 4 5
0 1 2 3 4
2 3 4 5
Explanation: The condition i<=5 includes i=5. Starting at 1 gives five iterations: 1, 2, 3, 4, 5. With i<5 there would be four. Watch this off-by-one difference.

Q2. do-while executes at least once

int i = 10;
do {
    printf("%d ", i);
    i++;
} while (i < 3);

What is the output?

Nothing
10
10 11 12
Infinite loop
Explanation: do-while executes the body before checking the condition. It prints 10 and increments i to 11; 11<3 is false, so it stops. A while loop with the same initial condition would execute zero times.

Q3. Sum of array elements

int a[] = {2, 4, 6, 8};
int sum = 0;
for (int i = 0; i < 4; i++) sum += a[i];
printf("%d\n", sum);

What is the output?

8
14
20
24
Explanation: The running sum is 0 → 2 → 6 → 12 → 20. A standard array loop visits indices 0 through N-1.

Q4. Exiting with break

for (int i = 0; i < 5; i++) {
    if (i == 3) break;
    printf("%d ", i);
}

What is the output?

0 1 2 3 4
0 1 2
0 1 2 4
0 1 2 3
Explanation: At i=3, break exits the loop before printf runs. The output is 0 1 2. Replacing break with continue would skip only that iteration and print 0 1 2 4.

Q5. Accessing outside an array

int a[3] = {10, 20, 30};
for (int i = 0; i <= 3; i++) {
    printf("%d ", a[i]);
}

What happens in this code?

It reliably prints 10 20 30 without problems
Compilation error (bounds are checked)
It accesses a[3], causing undefined behavior (possible meaningless values or a crash)
a[3] is automatically treated as 0
Explanation: A three-element array has valid indices 0, 1, and 2. The condition i<=3 also accesses a[3], causing undefined behavior. C does not automatically check bounds; a crash, corrupted data, or apparently normal output are all possible. Use i < N.

Q6. String length

#include <stdio.h>
#include <string.h>

int main(void) {
    char s[10] = "Hello";
    printf("%zu\n", strlen(s));
    return 0;
}

What is the output?

10 (array capacity)
5
6 (including \0)
"Hello"
Explanation: strlen counts characters before the first null terminator. Hello contains five characters, so strlen(s) is 5. The terminator is excluded. In this example sizeof s is 10, the array capacity in bytes.

③ Common mistakes and tricky points

If an item feels familiar, return to its original lesson and review it.
  1. 1
    <and <=caused an off-by-one iteration count
    i<5 runs five times; i<=5 runs six. With an array, that can access outside its bounds.
  2. 2
    Omitted an update, causing an infinite loop
    while(i<5){...}inside i++ can leave the loop running forever if the update is omitted. Ensure something makes the condition false eventually.
  3. 3
    Counted array indices from 1
    C array indices start at 0 . For N elements, the valid indices are 0 〜 N-1.
  4. 4
    Accessed outside array bounds
    Compilation may succeed because C does not check bounds, but this causes undefined behavior . It may crash, corrupt another value, or appear to work.
  5. 5
    Confused break and continue
    break exits the loop; continue starts the next iteration. Confusing them changes the output.
  6. 6
    Forgot the semicolon after do-while
    do { ... } while (cond); needs its final semicolon, unlike an ordinary while loop.
  7. 7
    Confused strlen and sizeof
    strlen("Hi") is 2 (character count); sizeof "Hi" is 3 (bytes including \0). They serve different purposes.

④ Lessons to revisit when you get stuck

Use these links to return to the relevant explanation.
Lesson 16: for loops Review the three-part structure and iteration count Lesson 17: while and do-while Review when the condition is checked Quiz (Loops) Check understanding with related questions Lesson 19: Debugging techniques Diagnose infinite loops and indexing mistakes Lesson 20: Arrays Review index ranges and initialization Lesson 23: Strings Review \0, strlen, and sizeof Cheat sheet Review formats, arrays, and strings together