what is while?

–3 votes
asked Jul 28 by Adrian (250 points)

2 Answers

+3 votes
answered Jul 28 by Peter Minarik (7,160 points)
edited Jul 28 by Peter Minarik

In most modern programming langues, there is a variant of while.

while is a keyword to create a loop, where generally the syntax is something like this:

while ([condition])

Which is to say, that as long as [condition] evaluates to true, [expression] (or the body of the loop) is kept executed over and over again.

The following C code counts backwards before a rocket is launched.

#include <stdio.h>

int main()
    printf("Rocket is launched in...\n");
    int seconds = 5;
    while (seconds > 0)
        printf("%d...\n", seconds);

    return 0;

The while loop terminates, when seconds will not be larger than zero anymore.

0 votes
answered Aug 4 by Amar Kulkarni (180 points)
while is an iterative control statement that is used to run a piece of code in a loop / n times.

syntax :


-> mainly used when the programmer doesn't know how many times to run a code/statements
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.