what is while?

0 votes
asked Jul 28 by Adrian (240 points) 1 flag

2 Answers

+3 votes
answered Jul 28 by Peter Minarik (4,550 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])
    [expression]

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);
        seconds--;
    }
    printf("Ignition!\n");

    return 0;
}

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

0 votes
answered 22 hours ago 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 :

while(condition)
{
///statements;
}

-> 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.
...