Home > Back-end >  The use of C language while
The use of C language while

Time:10-11

Beg of bigwigs doubt!!!!!!
If only a while statement, alone without the loop body, and judge the condition is true, has been performing in front of the while, the program is not behind the execution?
Such as
Statements (1)
Statement (2)
while(1);
Statement (3)
Statement (4)

CodePudding user response:

Yes, has been in the infinite loop

CodePudding user response:

Don't repeat in front of the while statement
An you see
While (1)
{
;
}

Will always be performed while the inside of the empty statement, also is the execution of the statement which 1 and 2, stop in the while (1), and do nothing,

CodePudding user response:

This is typical of death cycle, the program execution process is like this:
Statements (1) - & gt; Statement (2) - & gt; While (1);
Here, have not been out of the while loop, has been in the implementation of an empty statement
So while loop the following statement (3) (4) no execution, and the above statement (1) (2) performed only once,

CodePudding user response:

Only a few lines of code, you can write code to run,

CodePudding user response:

Not performed before and after, will only have been executed while (1); This is an infinite loop,

Suggested the building Lord oneself write a test program ~

CodePudding user response:

# include "reg52. H"//this file defines the MCU some special function register
Typedef unsigned int under-16;//to statement to define data types typedef unsigned char u8;
Sbit k1=P3 ^ 1;
Sbit k2=P3 ^ 0;
Sbit beep=P1 ^ 5;
U8 start=0;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * letter of several: delay * function function: delay function, I=1, delay about 10 us * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void delay (under-16 I)
{
While (I -);
}
Void sound ()
{
U8 I=100;
While (I -)
{beep=~ beep;
Delay (100); }
}
Void keypros ()
{
If (k1==0)//
{
delay(1000);
If (k1==0)
{
Start=1;
}
while(! K1);
}
If (k2==0)//stop
{
delay(1000);
If (k2==0)
{start=0; }
while(! K2);
}
}/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * letter of several: the main function: * function main function * entered: no * output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void main ()
{
While (1)
{
Keypros ();
If (start==1)
{sound (); }
The else {beep=1; }
}
}

CodePudding user response:

This is a button control buzzer code, according to the k1 buzzer sounded, according to k2 buzzer will not ring, but if I don't send open has been according to k2, according to the above said: the code has been in a while (! K2); Position, also will not run down, a buzzer is loud, but I long press k2 or short press, buzzer will not ring
  • Related