• c-
InterCourses
CoursesBlogs
← C Programming for Beginners
○What a program actually does○The computer and the operating system○From a C file to a running program○Compiled and interpreted programs○Demo: Trace One Tiny C Program○Exercise: Print the Opening Sign✦Module Quiz
○Your First C Program○Variables and Formatted Output○Arithmetic and Integer Division○Demo: Formatting and Numeric Conversion○Exercise: Calculate a Shop Total✦Module Quiz
○Demo: Declare and initialize variables○Exercise: Declare and initialize variables○Demo: Assign a new value○Exercise: Assign a new value○Demo: Choose int, double, and char○Exercise: Choose int, double, and char○Demo: Keep local names and constants clear○Exercise: Keep local names and constants clear✦Module Quiz
○Demo: Match printf placeholders and arguments○Exercise: Match printf placeholders and arguments○Demo: Build lines with escapes and multiple calls○Exercise: Build lines with escapes and multiple calls○Demo: Print percent signs and aligned values○Exercise: Print percent signs and aligned values○Demo: Evaluate expressions before printing○Exercise: Evaluate expressions before printing✦Module Quiz
○Comparisons and True or False○How if and else Choose a Path○Exercise: Choose a Ticket Message○Demo: Select a Fixed Choice with switch○Exercise: Choose a Service Desk✦Module Quiz
●For Loops: Repeat a Known Number of Times○Exercise: Add Daily Contributions○While Loops: Repeat While a Condition Holds○Exercise: Make a Countdown○Demo: Run Once with do while○Demo: Label a Two-Row Shelf○Exercise: Label Tool Drawers✦Module Quiz
○Define and Call a Function○Follow a Function Call and Its Return Value○Exercise: Return a Square○Demo: Declare Before a Function Call○Exercise: Declare and Convert✦Module Quiz
○Arrays and Zero-Based Indexes○Exercise: Add Array Elements○Demo: Count Array Slots Safely○Exercise: Average Workshop Ratings○Demo: Rows and Columns in Memory○Exercise: Total a Two-Row Grid✦Module Quiz
○C Strings and the Null Terminator○Exercise: Count a Label○Demo: Compare Complete Strings○Exercise: Verify a Pickup Code○Demo: Visit Each Character○Exercise: Count a Letter in a Label✦Module Quiz
○Pointers: Addresses and Dereferencing○Exercise: Update Through a Pointer○Demo: Pass an Address to a Function○Exercise: Swap Two Counts○Demo: Array Addresses and Offsets○Exercise: Total with an In-Bounds Pointer○Demo: Null, Validity, and Lifetime○Exercise: Handle an Optional Pointer✦Module Quiz
○Demo: Pass an Array and Its Length○Exercise: Total Readings in a Function○Demo: Change Array Elements Through a Parameter○Exercise: Update Each Stock Count✦Module Quiz
○Define a Supply Record○Exercise: Define a Supply Record○Update a Record Member○Exercise: Update a Record Member○Walk an Array of Records○Exercise: Walk an Array of Records○Pass a Record to a Function○Exercise: Pass a Record to a Function✦Module Quiz
○Demo: One Value in Shared Storage○Exercise: Record One Ticket Value○Demo: A Tag Tells You What Is Stored○Exercise: Read a Tagged Repair Ticket○Demo: Compare Union and Struct Storage○Exercise: Choose the Right Storage✦Module Quiz
○Demo: Read Bits with AND○Exercise: Check an Equipment Flag○Demo: Turn a Flag On with OR○Exercise: Mark a Kit Ready○Demo: Toggle and Clear Individual Flags○Exercise: Toggle a Warning Flag○Demo: Move a Bit into Position○Exercise: Build a Position Mask✦Module Quiz
○Include the Right Header○Exercise: Include the Right Header○Guard a Macro Default○Exercise: Guard a Macro Default○Demo: Include Code Only for Debug Builds○Exercise: Gate an Inventory Diagnostic○Demo: Parenthesize a Simple Macro○Exercise: Keep a Macro Calculation Grouped✦Module Quiz
○Name a Status with enum○Exercise: Name a Status with enum○Alias a Record Type with typedef○Exercise: Alias a Record Type with typedef✦Module Quiz
○Where the storage comes from○Demo: Request and Release an Array○Exercise: Store Three Repair Counts○Demo: Start Allocated Counters at Zero○Exercise: Start Station Counters at Zero○Allocate and Release Records○Exercise: Allocate and Release Records✦Module Quiz
○Print a Formatted Label○Exercise: Print a Formatted Label○Print Characters with putchar○Exercise: Print Characters with putchar○Format into a Bounded Buffer○Exercise: Format into a Bounded Buffer○Write a Line to stdout with fputs○Exercise: Write a Line to stdout with fputs○Input and Diagnostics in a Local Terminal✦Module Quiz

For Loops: Repeat a Known Number of Times

Label the boxes with a loop

Three boxes need labels. You could write three printf calls, but a for loop keeps the changing number in one place. In for (int box = 1; box <= 3; box++), C initializes box once, checks the condition before each pass, runs the body when it is true, and increments afterward. box++ adds one. When the condition fails, execution continues after the loop.

For-loop flow: initialize once, test before each pass, execute the body, update, then test again

Trace the arrows with box = 1: print 1, then update to 2. The same happens for 2 and 3. At 4, 4 <= 3 is false, so nothing else prints from the loop. Without an update, a condition that stays true can keep the program running forever.

Run the box labels and check where the final message appears:

c
#include <stdio.h>

int main(void) {
    for (int box = 1; box <= 3; box++) {
        printf("Box %d\n", box);
    }
    printf("All boxes labeled\n");
    return 0;
}

Set the upper limit to 2 and count the box lines. For a running total, keep a variable outside the loop and update it inside:

c
#include <stdio.h>

int main(void) {
    int sum = 0;
    for (int number = 1; number <= 4; number++) {
        sum = sum + number;
    }
    printf("Sum: %d\n", sum);
    return 0;
}

sum starts at zero, then keeps its previous value between passes. Trace it: as number goes 1, 2, 3, 4, sum becomes 1, 3, 6, 10.

Three passes, two starting points

Labels 1 through 3 use box <= 3. Array indexes for three elements are 0 through 2, so they use i < 3. Both loops run three times; the starting values differ. Keep that distinction in mind for the accumulator exercise.