Creating While Loop Programs in C

CCBeginner
Practice Now

Introduction

In this lab, we will learn how to create a while loop program in C language. A while loop is used in programming to execute a block of code repeatedly until the specified condition becomes false. This loop is used when we don't know beforehand how many times the loop will run.


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/while_loop("`While Loop`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123356{{"`Creating While Loop Programs in C`"}} c/variables -.-> lab-123356{{"`Creating While Loop Programs in C`"}} c/data_types -.-> lab-123356{{"`Creating While Loop Programs in C`"}} c/operators -.-> lab-123356{{"`Creating While Loop Programs in C`"}} c/while_loop -.-> lab-123356{{"`Creating While Loop Programs in C`"}} c/function_parameters -.-> lab-123356{{"`Creating While Loop Programs in C`"}} c/function_declaration -.-> lab-123356{{"`Creating While Loop Programs in C`"}} end

Open your Code Editor

You can use any code editor or IDE of your choice. In this lab, we will be using the online Code Compiler.

Create a main.c file

Create a main.c file in the ~/project/ directory. This is where we will write our code.

Add C Standard Library

We need to include the standard input and output header file stdio.h in our program using the #include preprocessor directive. This header file provides input-output functionalities for the C program.

#include <stdio.h>

Declare variables

Declare the loop variable i that will be used in the while loop. Initialize it to zero.

int i = 0;

Write the code for the while loop

Write the code for the while loop that executes until the specified condition becomes false. In this loop, we will print the numbers from 0 to 9 using printf function.

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

The above code will execute as long as the value of i is less than 10. Inside the loop body, we are printing the value of i on the console, followed by incrementing the value of i by 1.

Output a message after the loop

Outside of the loop, add a printf function to output a message after the loop completes.

printf("Printing numbers 0-9 using While Loop!\n");

Full code of the main.c file

#include <stdio.h>

int main()
{
   int i = 0;
   while(i < 10)
   {
      printf("%d\n", i);
      i++;
   }
   printf("Printing numbers 0-9 using While Loop!\n");
   return 0;
}

Summary

In this lab, we have learned how to create a while loop program in C language. The while loop is used to execute a block of code repeatedly until the specified condition becomes false. Remember, a while loop executes as long as the condition inside the loop is true.

Other C Tutorials you may like