C Language Learning Tutorial Chapter III-C language programming preliminary (7)

Source: Internet
Author: User
Tags execution expression printf

Do-while statement

The general form of the Do-while statement is:
Todo
Statement
while (expression);
Where the statement is a loop body, the expression is a looping condition.
The semantics of the Do-while statement are:
The loop body statement is executed first, then the value of the expression is evaluated, and if True (not 0), the loop continues, otherwise the loop is terminated.
The difference between a do-while statement and a while statement is that Do-while is executed first, so do-while must execute at least one loop body at a time. While the while is first judged after execution, if the condition is not satisfied, then the loop body statement is not executed.
While statements and Do-while statements can generally be rewritten to each other.
void Main () {
int a=0,n;
printf ("\ n input N:");
scanf ("%d", &n);
Do printf ("%d", a++*2);
while (--N);
}

In this case, the loop condition is changed to--n, otherwise the loop is executed more than once. This is due to the first implementation of the judgement after the result.
The following points should also be noted for Do-while statements:
1. In the IF statement, in the while statement, the expression cannot be followed by a semicolon, and after the expression of the Do-while statement, you must add a semicolon.
2.do-while statements can also compose multiple loops, and they can also be nested with the while statement.
3. When the loop body between do and while is composed of multiple statements, it must also be enclosed in {} to form a compound statement.
When 4.do-while and while statements replace each other, be careful to modify the loop control condition.

For statement

The For statement is a more powerful, more widely used loop statement than the C language provides. Its general form is:
for (expression 1; expression 2; expression 3)
Statement
Expression 1 is usually used to assign an initial value to a cyclic variable, typically an assignment expression. Also allows you to assign an initial value to a loop variable outside of a for statement, which can be omitted at this time.
Expression 2 is usually a loop condition, typically a relational expression or a logical expression.
Expression 3 can often be used to modify the value of a loop variable, typically an assignment statement.
Each of these three expressions can be a comma expression, meaning that each expression can consist of multiple expressions. All three expressions are optional and can be omitted.
The "statement" in general form is a circular body statement. The semantics of the For statement are:
1. The value of expression 1 is calculated first.
2. Then calculate the value of expression 2, if the value is true (not 0) then execute the loop body once, otherwise jump out of the loop.
3. Then calculate the value of the expression 3 and return to the 2nd step for repeated execution. In the entire for loop, expression 1 is evaluated only once, expression 2 and expression, and 3 may be computed multiple times. The loop body may execute multiple times, or it may not execute at once. The For statement is executed as shown in the figure.
void Main () {
int n,s=0;
for (n=1;n<=100;n++)
S=s+n;
printf ("s=%d\n", s);
}
Calculating s=1+2+3+...+99+100 with a for statement

int n,s=0;
for (n=1;n<=100;n++)
S=s+n;
printf ("s=%d\n", s);
In this case, the expression 3 in the For statement is n++ and is actually an assignment statement, equivalent to n=n+1, to change the value of the loop variable.
void Main () {
int a=0,n;
printf ("\ n input N:");
scanf ("%d", &n);
for (; n>0;a++,n--)
printf ("%d", a*2);
}
Modify an example with a for statement. Starts with 0, outputting n consecutive even numbers.
int a=0,n;
printf ("\ n input N:");
scanf ("%d", &n);
for (; n>0;a++,n--)
printf ("%d", a*2);
In the For statement of this example, expression 1 is omitted, the initial value of the loop variable is obtained by the SCANF statement before the For statement, and expression 3 is a comma expression consisting of two expressions of a++,n--. One time per cycle a self increase 1,n 1. The change of a makes an even increment of the output, and the change of N controls the number of times.
Note the following points in using the For statement
Each expression in a 1.for statement can be omitted, but the semicolon spacer must not be less. For example: for (; expression; expression) omit expression 1. for (expression;; expression) omit expression 2.
for (expression; expression;) omit expression 3. for (;;) omitted all expressions.
2. When the cyclic variable has been assigned an initial value, the expression 1 can be omitted, as in example 3.27 that is the case. Omitting the expression 2 or expression 3 will cause an infinite loop, in which case you should try to end the loop within the loop. The example is the case.
void Main () {
int a=0,n;
printf ("\ n input N:");
scanf ("%d", &n);
for (;n>0;)
{a++;n--;
printf ("%d", a*2);
}
}
In this example, the expression 1 and expression 3 are omitted, and the loop variable n is decremented by the n--statement in the loop body to control the number of loops.
void Main () {
int a=0,n;
printf ("\ n input N:");
scanf ("%d", &n);
for (;;) {
a++;n--;
printf ("%d", a*2);
if (n==0) break;
}
}
In this case, the expression of the For statement is omitted. By the statement in the circulation body to realize the decline of the cyclic variable and the judgment of the cyclic condition. When the n value is 0 o'clock, the break statement aborts the loop and goes to execute the for later program. In this case, the for statement is equivalent to a while (1) statement. If there is no corresponding control means in the circulation body, it causes the dead circulation.
3. The loop body can be an empty statement.
#include "stdio.h"
void Main () {
int n=0;
printf ("Input a string:\n");
for (; GetChar ()!= ' \ n '; n++);
printf ("%d", n);
}
In this case, the expression 1 of the For statement is omitted, and expression 3 is not used to modify the loop variable, but rather as a count of the input characters. In this way, the count that should have been done in the loop body is completed in the expression. So the loop body is an empty statement. It should be noted that the semicolon after the empty statement is not small, such as the absence of this semicolon, the following printf statement as a loop body to execute. Conversely, if the loop body is not an empty statement, it must not be followed by a semicolon in the parentheses of the expression, so that the loop body is an empty statement and cannot be executed repeatedly. These are common mistakes in programming, so be careful.
4.for statements can also be nested with while,do-while statements to form multiple loops. The following forms are all legitimate nesting.
(1) for () {...
while ()
{...}
...
}
(2) do{
...
for ()
{...}
...
}while ();
(3) while () {
...
for ()
{...}
...
}
(4) for () {
...
for () {
...
}
}
void Main () {
int i,j,k;
for (i=1;i<=3;i++)
{for (j=1;j<=3-i+5;j++)
printf ("");
for (k=1;k<=2*i-1+5;k++)
{
if (k<=5) printf ("");
else printf ("*");
}
printf ("\ n");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.