while(1) statement

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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 ?
 

Amar Dhore

Dec 2, 2015
129
Joined
Dec 2, 2015
Messages
129
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

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
This is wrong: while (j=1)
it should be while (j== 1);
--
Amar
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;
    }
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
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)
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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)
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
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
sorry but I don't understand what do you mean

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