Connect with us

What are real example of " While Loop"

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Jan 14, 2018.

  1. vead

    vead

    473
    14
    Nov 27, 2011
    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: Jan 14, 2018
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    9,375
    1,906
    Nov 17, 2011
    These examples look fine.
     
    vead likes this.
  3. Gumby_Kevbo

    Gumby_Kevbo

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

    Nanren888

    121
    25
    Nov 8, 2015
    How's arduino go? Setup & loop?
    setup();
    while(1)
    loop();
    end
     
  5. ramussons

    ramussons

    342
    61
    Jun 10, 2014
    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
    }

    }
     
  6. Kabelsalat

    Kabelsalat

    107
    21
    Jul 5, 2011
    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.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-