C language while loop do while loop statement for

First, the structure of thought and meaning cycle:
know the loop structure, then the same in life, we are all doing the same thing repeated every day, such as: the process of inhaling and exhaling; and if the summer open fan, fan circle turn around, this is repeated. Now we play a game, a person is a person A to do B, a person A says the command "salute, Libi", another person B to do the movements ten times; that is to say at first thought execute commands let B after the operation, B after executing action, a command and then went on to say, B and then continue to do the movements, the same thing is repeated ten times. If you use the knowledge learned, let you output ten times "salute, Libi", how would you write procedures?

/* 输出敬礼礼毕十遍 */
#include "stdio.h"
void main()
{
    printf("1.敬礼、礼毕\n");
    printf("2.敬礼、礼毕\n");
    printf("3.敬礼、礼毕\n");
    printf("4.敬礼、礼毕\n");
    printf("5.敬礼、礼毕\n");
    printf("6.敬礼、礼毕\n");
    printf("7.敬礼、礼毕\n");
    printf("8.敬礼、礼毕\n");
    printf("9.敬礼、礼毕\n");
    printf("10.敬礼、礼毕\n");    
}

A B said it a thousand times to make it, obviously if you still use the above procedure to write, then today you an hour without doing anything else, you just copy and paste it slowly here! So certainly there are better ways: A command should be said that the implementation of the action "salute, Libi" ten times for B. The same problem we look at the second paragraph of program code:

/* 输出敬礼礼毕十遍 */
#include "stdio.h"
void main()
{
	int i;
	i=1;
    while(i<=10)
	{
printf("%d.敬礼、礼毕\n",i);
i=i+1;
	}   
}

Second, while the basic format
while basic format :( flowchart shown right)
while (expression)
{
statement 1;
......
statement n-;
}
where expression represents the cycle conditions, the entire composite loop statement.
while statement specification:
1: {} own line and
2: n statement Statement 1- automatically retracted
while loop belongs when circulating, i.e. first determine whether the value of the expression is true (may also be understood as the condition is satisfied) If the loop is true, otherwise exit the loop.
Its format is modified as follows;
expression 1;
the while (Expression 2)
{
statements;
expression 3;
}
wherein Expression 1 represents the initial value before the program loop, the expression control condition represents 2, 3 represents the expression incremental changes
take an example to analyze the output of 1-100.
Most initial value (assuming variables i) should be i = 1; with the proviso that i <101 (or i <= 100); statement is a variable corresponding output value printf ( "% d", i ); and Expression 3 after entering the output should be a next number, that is, i ++;
so it is easy to code it came out:

void main()
{
    int i;
    i=1;
    while(i<101)
{
  printf(“%d ”,i);
  i++;   
}
}

do-while loop
do-while statement
has the While loop, until there is circulating in the circulation. Next we have to learn until the next model cycle.
A, do-while statement format
1.do -while sentence format 1
expression 1;
do
{
statement;
expression. 3;
} the while (Expression 2);
wherein the expression represents an initial value before the program loop, the expression 2 represents the control condition, expression 3 represents the incremental change in
this format has been rarely used in the program. Mainly used in planting deformation format:
2.Do -while statement format deformation
do
{
statement;
} the while (conditional expression);
Second, the thought and meaning do-while statements
do translate English into Chinese is meant to do, then from the above structure we can see, the do statement evaluates the conditional expression if the condition expression is also true, execute the statement again until the conditional expression is not satisfied, continue down. So do-while central idea is to perform at least one loop.
Three, do the while-loop configuration example of
some of the following examples to explain the operation of our logical thought processes of the cyclic structure.
1. seek 1 + 2 + 3 + 4 ± -? + 100 and how much
(1) Step 1, first of all we should analyze is that we require and that is a number, and then a few initial value is it? 0 = SUM
(2) = SUM +. 1;
(. 3) SUM + = 2;
(. 4) SUM + =. 3;
if we changed as a variable i to represent the values, then we should be able to estimate to summation fact (. 5)
(. 6) SUM + = i; but i changes from 1 to 100
(. 7) following the while reference code Code

/*求1+2+3+---+100的和*/
#include "stdio.h"
void main()//求各
{
	//定义变量并初始化
	int i=1;
	int sum=0;
	while(i<=100)  //条件表达式
	{
		sum+=i;
		i+=1; //步长
	}	
	printf("和是%d",sum);
}

(8) do-while code reference code is as follows

/*求1+2+3+---+100的和*/
#include "stdio.h"
void main()//求各
{
	//定义变量并初始化
	int i=1;
	int sum=0;
	do
	{
		sum+=i;
		i+=1; //步长
	}	while(i<=100); //条件表达式
	printf("和是%d",sum);
}

2. Enter qq password; then enter the correct login successful, otherwise it may have to enter a password up to is, assuming the password is 123456
(1) we analyze the topic, it should be a judgment entered after the first round-robin fashion, then it should select the do-while statement
(2) requires us to program a password, the password can be changed, so as to give a variable password definition, we assume that the password int;
(. 3) because the password is entered, the initial value no
(4) enter after the password we should determine whether the same password and the default password
(5) the same as the login is successful
(6) is not the same as re
(7) do-while code reference code is as follows

/*判断qq密码*/
#include "stdio.h"
void main()//登录
{
	//定义变量并初始化
	int password;
    do	
	{
		printf("请输入qq登录密码:");
		scanf("%d",&password);
	}while(password!=123456);  	//条件表达式
	printf("登录成功");
}

(8) do-while statements can be while () replacement, in fact, the statements between the loop can be replaced with each other, which method is more suitable for you, choose the kind, that is to say one is must have their own expertise.

(9)参考代码如下
/*登录*/
#include "stdio.h"
#include "stdlib.h"
void main()//登录
{
	//定义变量并初始化
	int password;
    while(1)
	{
		printf("请输入qq登录密码:");
		scanf("%d",&password);
		if(password==123456)
		{
			printf("登录成功");
			break;
			}
	}
	
}

3.while summarize the similarities and differences of the do-while?
(1) the while statement is to calculate the value of the expression, and then execute the loop body, do ... while loop is executed once finished, then calculate the value of the expression
(2) when the first value of the expression is false, while the loop is not executed once, do ... while loop is executed once;
(3) when the expression is true, there was no difference between the
(4) If you are unsure of the number of conditions, using procedures generally while more

for loop
for loop is when circulating. easiest for loop counting cycle control method. use a for loop is the most flexible and suitable for application in the case of cycles determined.
ideas for loop is to first clear analysis of the initial value of the loop, the loop control condition, the loop variable step change, and then re-written directly to the statement in the loop body for the can, which would allow the problem to think clearly, process is also very clear.
Two, for loop application
for statement is c language provides more powerful, more widely used as a loop.
1.for general format of the statement
(1) Case 1
for (expression 1; expression 2; Expression 3)
statements;
(2) Case 2
for (expression 1; expression 2; Expression 3 )
{
statement block;
}
Note 2.for statement
(1) can not be omitted semicolons expression
(2) expression 1 and expression 2 and expression 3 can be omitted, generally not omitted.
① When the expression 1 is omitted, the equivalent of eliminating the initial value for the loop variables, this time should be given prior to the loop variable for given initial statement
② When the expression 2, and indicates that the loop condition is not determined, that is, 2 expression is always true, then it should seek end loop body of the loop, otherwise it will become a dead cycle
③ when the expression 3 is omitted, i.e., eliminating the need to modify the value of the loop variable, but this time by an end to be circulated in the circulation in vivo
( 3) Although the expression can be omitted, but when used for, or to normal use, that is, the others have.
Three, for logical thought process
for statements and while statements, as it was before the idea of cycling to do, is to have an initial value expression 1, and then determine the conditional expression 2 is established, if set up on the implementation of {} in the statement and then jumps to the expression 3, expression 3 jump to the expression 2, again to determine the expression 2, should set up again, do {} statement in the statement and then jumps to expression 3, expression 3 jump to the expression 2, if repeat again the establishment, if you do not set up the loop exits.
The flow chart for the direction and while statements exactly the same, but for the thought process, can be directly executed from a first analysis of the first, what is the execution condition, the step size is how much, and what the statement is executed, you can apply for construction .

Guess you like

Origin blog.csdn.net/SqrsCbrOnly1/article/details/91374659