Maker Pro
Maker Pro

What are real example of " While Loop"

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
I do not know whether this question should be asked or not but I am confuse. This is reason reason I am asking here.
I wanted to know how do we use while Loop in c language I wrote program and read about while loop in my book.
I thought I should write my own programs

so, I made some examples for while loop
  • Keep light On until user closes it
  • Enter password until the user enters correct password
  • Do not stop the machine until its temperature is 30* c
  • Fill the water in the bottle until it is empty
  • Play the songs until the user stop it
Code:
while ( user doesn't close it)
{
    Keep light On ;
  
}


while ( user doesn't enters correct password)
{
    promote user to enter correct password repeatedly
  
}

while (temperature is 30* c)
{
    run the machine continuously
  
}

while (bottle is empty)
{
    fill water in bottle contentiously
  
}

while ( user doesn't stop)
{
    play all songs repeatedly
  
}

I think all these examples are correct but I am not sure weather its false or true. I just want to know, Am I getting right point about while loop. Is all these examples correct? Do I need to improve example for "while loop"?
 
Last edited:

Gumby_Kevbo

Jan 25, 2018
11
Joined
Jan 25, 2018
Messages
11
Many embedded control programs are contained in a " while(1)" loop....the main loop that keeps repeating as long as the power is on.
 

ramussons

Jun 10, 2014
462
Joined
Jun 10, 2014
Messages
462
Its good practice to include a "break" statement to exit from the "while loop" routine without having to power off.

while(......)
{ do the following
.
.
{if (.............)
break
}

}
 

Kabelsalat

Jul 5, 2011
182
Joined
Jul 5, 2011
Messages
182
Ther is the while loop, and also much used do/until (terms may differ in other programming languages).

The while loop executes zero or more times until a condition is true. A do/until (or do/while) loop runs one or several times until a condition is true.

It may be easier to explain using labels / goto, like this:

while (condition) {
do_something()
}

; is the same as using labels like this
label:
If (condition) {
do_something()
GoTo label
}



Do
{
do_something()
} until (condition)

; is the same as using labels like this
label:
do_something()

If !(condition) {
GoTo label
}



So if I decide to pick up a phone call from Indian Microsoft (scam) and decide to waste their time, it'll probably look something like this:

while !(phone_is_ringing) {
just_do_whatever()
}

If ( caller() = scammer() )
{
say("hello")

Do
{
Do
{
listen_to_scammer()
convince_yourself_that_its_all_lie_and_dont_care_to_listen()
} while( scammer_still_talks() )

If !( scammer_hang_up )
{
spend_time_giving_away_awasteof_nonuseful_information()
}
} Until ( scammer_hang_up )
}

Edit: won't get indents to appear right, after posting.
 
Top