# Output the number of while loops

### How many times is the while loop?

Program segment intk=0;while (k=1) k++; the number of times the while loop body is executed is infinite.

Rationale: In the execution of while (k=1), the assignment statement will be executed first, so that the value of k is equal to 1, and then determine the loop condition of while, at this time, while (k=1) is equivalent to while(1), the loop condition is always true, and an infinite number of times the loop is executed.

Another example with the above condition is almost the same:

#include<stdio.h>

intmain(intargc,char*argv[])

{

intk=0;

while(k=1)

{

++k;

printf(“%d\n”,k);

};

return0;

}

The console will keep on outputting 2, as follows:

### What statement should be added to the program in order to be able to tell how many times the program written by the while statement has looped? Thank you all!

1, first go into the python software and define a count variable as shown.

2, next, define the loop program while statement, while after the definition of the conditions for the establishment of the loop, as shown in the figure.

3, and then defined, when the loop statement conditions are established, to be executed, as shown in the figure.

4, and finally deal with the previously defined counting variable, as shown in the figure (Note: the counting variable must be processed, otherwise there will be a dead loop).

5, the final execution of the program, the final output of the program as shown in the figure is complete.

### C while do loop how to count the number of loops ah

Generally the number of loops have been given, for example:

intn;

cin>>n;

while(n–)

{

statement 1;

statement 2;

}

But there are other conditions, for example:

inti=0;

while(condition satisfied)//for the given condition

{

Statement 1;

Statement 2;

i=i+1;

}

The i inside while acts as a counting loop!

### Can for and while statements directly determine the number of loops?

In C, `for` and `while` loops are most commonly used to control the number of loops by setting a loop condition, but they can also be used in situations where the number of loops is known.

For example, use a `for` loop to print the values 1 through 10:

“c

for(inti=1;i<=10;i++){

printf(“%d”,i);

}

This `for` loop explicitly specifies that the number of loops is 10, i.e., it outputs every number from 1 to 10.

Another example is to use a `while` loop to read 5 numbers and sum them:

“c

inti=1,sum=0;

while(i<=5){

intnum;

printf(“Enteranumber:”) ;

scanf(“%d”,&num);

sum+=num;

i++;

}

printf(“Thesumis:%d\n”,sum);

This `while` loop explicitly specifies that the number of loops is 5, i.e., it loops to read 5 values for summation.

So, `for` and `while` loops can be used when the number of loops is known, and are not necessarily needed for loop control when the number of loops is unknown.

### C while(a–), while(–a) loop times

Change to while(–a), a first self-subtracted 1, and then determine the loop conditions

First: a=a-1=1a!=0 conditions met to continue the loop

Second: a=a-1=0a!=0 conditions are not met to exit the loop

< p>So the final output: 0

### How many times does while(3) loop

while(3) loops 3 times. And each of those three times the inner loop is 2. In fact, once in the outer loop, the value will be +2, which is 3 2s, and the result is 6. The number of times while loops depends on the qualification. while itself is a loop, and can be used in nested ways.

### Number of while loops

Inner loop 2 times. First time j=1, s=0, s++,j=1,j++.

The second time, s=1, j=2, then jump out of the loop, this time i is equal to 1. The second loop, j is equal to 1 again, so the loop is 3 times.

The main thing is that after the inner loop is over, it waits until the outer layer loops again, and then reassigns j to a new value.