Connect with us

while(1) statement

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Jun 9, 2017.

  1. vead

    vead

    473
    14
    Nov 27, 2011
    Hello
    I need help to use of while(1) statement in C programming. Look at following example
    Code:
    #include <REG51.h>
    #define LED              P2
    #define LED_ON      0xff
    #define LED_OFF     0x00
    
    void delay (unsigned int j)
    {
        unsigned int j;
        for (j = 0; j < 40000; j++);
        {
       
        }
    }
    
    void main()
    {
     
        while (1)
        {
            LED = LED_ON;
            delay(500);
            LED = LED_OFF;
            delay(5000);
            LED = LED_OFF;
       
        }
    }
    I don't understand exact use of while (1) statement in above program
    In the C programming language,
    Code:
    while (condition)
    {
       statements;
    }
    
    In first program there is condition (1) . why don't we write like below program
    Code:
    void main()
    {
     
        while (j=1)
        {
            LED = LED_ON;
            delay(500);
            LED = LED_OFF;
            delay(5000);
            LED = LED_OFF;
       
        }
    please clear my doubt ?
     
  2. Amar Dhore

    Amar Dhore

    129
    12
    Dec 2, 2015
    This is wrong: while (j=1)
    it should be while (j== 1);

    while(1) => run forever.
    while(j==1); run until J=1;

    I guess you are asking; instead of while(1), can you use while(j==1). Ofcourse you can, but then you will have to declare J and you have to make sure its always 0 to make sure the loop run forever. You can also use for( ; ; )

    --
    Amar
     
    vead and Arouse1973 like this.
  3. vead

    vead

    473
    14
    Nov 27, 2011
    you have added semi ; I think it's not valid
    Code:
     while (j==1)
        {
            LED = LED_ON;
            delay(500);
            LED = LED_OFF;
            delay(5000);
            LED = LED_OFF;
        }
     
  4. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,220
    2,695
    Jan 21, 2010
    You could save yourself a lot of time by googling for the answer.

    Also @Amar Dhore is incorrect in his explanation insofar as his description of while(j==0) and the value of j required to make the loop continue (hint, it is 0)
     
  5. vead

    vead

    473
    14
    Nov 27, 2011
    sorry but I don't understand what do you mean

    I am confused between two statement
    while (number)
    while (0) // while is set to 0
    while (1) // while is set to 1

    while (variable name = =value)
    while (i==1) // if condition is true i==1 then run forever loop other wise don't run
    while (i==5) if condition is true i==5 then run forever loop other wise don't run
     
  6. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,220
    2,695
    Jan 21, 2010
    I MEAN YOU NEED TO LEARN TO GOOGLE

    In C++ a number has a logical value. A logical value is also a number.

    0 (zero) is False. All other values are True.

    1==1, which is True, typically has the value -1. Generally speaking -1 (which is all bits set to 1 as a signed integer) is returned by logic functions for True -- although it is implementation dependent.

    While(1) means While(True) which means "run forever"

    While(0) means While(False) which means "do not run the loop at all"

    While(i==1) keeps looping as long as i is 1. If i is not 1 the very first time, it won't run at all. Note that you can change i in the loop, indeed this is typically what you do.

    This is all extraordinarily BASIC C++ that you can google for without wasting our time explaining it. The fact that you don't understand the basics of the language suggests to me that you need to learn to use the language before you attempt to write operating system level code.
     
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

-