← Back to list

C Programming Minimalist Guide | Part 7.3: For Loop

The for loop is suitable choice when the number of iterations is known, simplifying decision-making among loops.

Miro in Level Up Coding · 2024-10-28 00:58 · 56 claps · 2.5 min read paywalled
#learn-c #coding-tutorial #for-loop-in-c #tech #startup
Open on Medium ↗
Wiki topics: STP · Startups & Venture 💻 · Programming 🏠 · Home & Living

C Programming Minimalist Guide | Part 7.3: For Loop

The for loop is suitable choice when the number of iterations is known, simplifying decision-making among loops.

Photo by Aidan Granberry on Unsplash

Photo by Aidan Granberry on Unsplash

Sign up for a free Medium plan and read the full article at no cost

Integer to Character Conversion in For Loop

Since for loops excel in scenarios with a known number of iterations, we'll use it to convert a set of integers corresponding to ASCII values into the alphabet. ASCII (American Standard Code for Information Interchange) values are numerical representations of characters in computing, such as letters, numerals, and symbols. Each character corresponds to a number, making it easier for computers to store and manipulate text. Along the way, we'll also introduce the key keyword continue.

Justification for Using a For Loop in the Integer to Character Conversion Example

Based on numbers of letters in alphabet, we know beforehand how many times we want the code in the body to be executed. This predictable iteration makes the for loop an ideal choice.

For Loop in Action

The for loop manages initialization, condition checking, and variable incrementing all in one line. As defined, our loop iterates from ASCII value 97 to 122, representing letters a and z respectively, using casting. When the loop encounters the value 104, it uses continue to skip it, simplifying control flow by simulating that in our alphabet the letter h does not exist.

#include <stdio.h>

int main() {

    for (int i = 97; i <= 122; i++) {
        if (i == 104) { 
            continue; // Skip the letter 'h' entirely, do not print anything
        } else if (i < 122) {
            printf("%c, ", (char)i); // Print the current letter followed by a comma and space
        } else {
            printf("%c\n", (char)i); // Print the last letter followed by a newline
        }
    }
    return 0; // Indicate successful program termination
}

Pseudocode

for each number from 97 to 122:
    if number equals 104 then
        skip this iteration via continue
    else if number is less than 122 then
        print converted value with comma and space
    else 
        print converted value followed by new line

Why to use keyword continue?

The continue keyword is used to skip the current iteration of a loop and proceed to the next iteration. It is useful for omitting specific cases within a loop's range without breaking out of the loop entirely. In our example, it helps in excluding the letter h from the output sequence.

Expected output in terminal

Alphabet simulating that letter h does not exist, we did skip it for the purpose with the keyword continue.

a, b, c, d, e, f, g, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z

Conclusion

Understanding the for loop's structure for known iteration ranges allows for precise control over program flow. By exploring character conversion from ASCII values, we demonstrate how for loops are suitable for tasks that require repeated, predictable operations. This example also illustrates the use of continue, showcasing its ability to refine output by excluding specific elements. Now, closing the loop on our loops section, see you next time.

Thank you for reading! Feel free to check out the C Programming Minimalist Guide series code on GitHub. If you have thoughts on this, be sure to leave a comment.

Resources and Acknowledgments

ChatGPT: Assistance with reviewing technical explanations and ensuring code 
logic accuracy was provided by ChatGPT, an AI language model developed by 
OpenAI.

DevDocs: Combines multiple API documentations into a fast, organized, 
and searchable interface.

The GNU C Reference Manual: A detailed reference manual for the C programming 
language.

메타데이터
post_id
bf4dae836bdb
slug
c-programming-minimalist-guide-part-7-3-for-loop-bf4dae836bdb
url
https://levelup.gitconnected.com/c-programming-minimalist-guide-part-7-3-for-loop-bf4dae836bdb
canonical_url
https://levelup.gitconnected.com/c-programming-minimalist-guide-part-7-3-for-loop-bf4dae836bdb
author_url
https://medium.com/@mmairo
status
ok
fetched_at
2026-06-22 07:15:07