While Loop In C

While loops is simpler to write than a for loop, because it requires a bit more work on your part.

Instead of defining all the loop data up front when you start the loop, like you
do in the for loop, using while you just check for a condition:

while (i < 10) {

This assumes that i is already defined and initialized with a value. And this loop will be an infinite loop unless you increment the i variable at some point inside the loop.

An infinite loop is bad because it will block the program, nothing else can happen.
This is what you need for a “correct” while loop:

int i = 0;
while (i < 10) {
/* do something */

There’s one exception to this, and we’ll see it in one minute. Before, let me
introduce do while .

while loop may be used where we don’t know how many times loop must be run.

Syntax of while loop in c


Print Codeverb.com message for 5 times by using while loop

#include <stdio.h>
void main(){
int i;
printf(“Codeverb.com %dn”,i);

Codeverb.com 1
Codeverb.com 2
Codeverb.com 3
Codeverb.com 4
Codeverb.com 5

This article is written by our awesome writer
Comments to: while loop in C programming Language

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Explore Tutorials By Categories


Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
    Strength indicator
    Log In | Lost Password