Basic for Loop Program

CCBeginner
Practice Now

Introduction

A loop is a sequence of instructions that is executed repeatedly either until a certain condition is reached or forever. A for loop is a control flow statement that executes a piece of code repeatedly until the specified condition is met. In this lab, we will create a simple program using for loop.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123257{{"`Basic for Loop Program`"}} c/variables -.-> lab-123257{{"`Basic for Loop Program`"}} c/data_types -.-> lab-123257{{"`Basic for Loop Program`"}} c/operators -.-> lab-123257{{"`Basic for Loop Program`"}} c/for_loop -.-> lab-123257{{"`Basic for Loop Program`"}} c/function_parameters -.-> lab-123257{{"`Basic for Loop Program`"}} c/function_declaration -.-> lab-123257{{"`Basic for Loop Program`"}} end

Launch the Terminal

To get started with the lab, first, you need to launch the terminal. You can do this either by searching the terminal through the activities tab or by using the shortcut keys "Ctrl + Alt + T" for Ubuntu.

Create a new C file

Now, in the terminal, create a new C file named main.c in the ~/project/ directory by executing the following command:

touch ~/project/main.c

Open the file in the text editor

Open the main.c file in your preferred text editor. For instance, you can use the nano editor by running:

nano ~/project/main.c

Write the code

In the text editor, type the following code:

#include <stdio.h>

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int i = 0;

    for(i = 0; i < 10; i++)
    {
        printf("i = %d\n", i);
    }

    printf("\n\The value of i after exiting the loop is %d\n\n", i);

    printf("\nRemember that the loop condition checks the conditional statement before it loops again.\n\n");

    printf("Consequently, when i equals 10, the loop breaks.\n\n");

    printf("i is updated before the condition is checked- hence the value of i after exiting the loop is 10 .\n\n");

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

This code will print the numbers from 0 to 9 using a for loop and provide an explanation of the loop condition.

Compile and run the code

Compile the code using the following command:

gcc -o main ~/project/main.c

Then, run the program with the following command:

./main

Understand the output

The output will be similar to the following:

                Studytonight - Best place to learn


i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9

The value of i after exiting the loop is 10

Remember that the loop condition checks the conditional statement before it loops again.

Consequently, when i equals 10, the loop breaks.

i is updated before the condition is checked- hence the value of i after exiting the loop is 10 .


                        Coding is Fun !

Modify the code

Experiment with the code by adjusting the loop initialization, condition and updation. Rerun the code to observe how the output changes.

Summary

In this step-by-step lab, we have learned how to create a basic for loop program in C. You should now have a better understanding of how to work with for loops, how they are defined, and how to modify the code to extract different output.

Other C Tutorials you may like