In the c language, there are two while(1), only the previous one is executed, or both are executed?

In the c language, there are two while(1), only the previous one is executed, or both are executed?

 Let me answer 

share it

 Report

4 answers

#热议# Zheng Shuang dared to laugh at himself on "Talk Show Conference", what do you think of the cool words?

furious_tauren 
2011-04-06

attention

If the second one is included in the first one, both will be executed. Otherwise, unless there is a goto or break statement in the first while(1), the second statement will be executed.

More questions and answers

Ask

The two are in parallel....... I want to use one while(1) as a clock signal, and the other while(1) for other operations... According to you, I don’t know how to generate it. That clock.....

Follow up

Is it under the window? This requires multi-threading, pthread_create under Linux.

 This answer is recommended by the questioner

 4   

 Comments ( 1

share it

 Report

leo421 
TA received more than 144 approvals 2011-04-06

attention

This depends on the code . Although while(1) is an infinite loop, it can be conditionally jumped out in the middle. So you can run the while behind.

 

Tan Haoqiang C language third edition introduced

break and continue statements

break statement

The break statement is usually used in loop statements and switch statements. When the break switch for a switch statement, the program can be executed after the switch out of the switch statement; as if no break statement, an endless loop will be unable to quit . The usage of break in switch has been encountered in the previous example when the switch statement was introduced, so I will not give an example here.

When the break statement is used in do-while, for, and while loop statements, the program can terminate the loop and execute the statements following the loop. Usually, the break statement is always linked with the if statement. That is, when the conditions are met, the loop is jumped out.

[Example 6.8]

main()

{

   int i=0;

   char c;

   while(1) /*Set up loop*/

     {

      c='\0'; /*Assign initial value to variable*/

      while(c!=13&&c!=27) /*The keyboard receives characters until you press Enter or Esc*/

        {

         c=getch();

         printf("%c\n", c);

        }

      if(c==27)

         break; /*Judging if you press Esc key to exit the loop*/

      i++;

      printf("The No. is %d\n", i);

      }

    printf("The end");

 }

 

 note:

The break statement has no effect on the if-else conditional statement.    What does it mean? 2020.3.23

In a multi-level loop, a break statement only jumps one level outward. 

 continue statement

The function of the continue statement is to skip the remaining statements in the loop and forcibly execute the next loop. The continue statement is only used in loop bodies such as for, while, do-while, and is often used with if conditional statements to speed up the loop. The execution process can be represented by the following figure.

1) while (expression 1)

     { ……

       if (expression 2) break;

       ……

}

 

2) while (expression 1)

     { ……

       if(expression 2)continue;

       ……

}

  

[Example 6.9]

main()

{

  char c;

  while(c!=13) /*If it is not a carriage return, then loop*/

      {

         c=getch();

         if(c==0X1B)

            continue; /*If you press the Esc key without output, the next cycle will be performed*/

         printf("%c\n", c);

       }

Guess you like

Origin blog.csdn.net/qq_25814297/article/details/108844238