C Language Introduction (ix) cyclic statements and loop control

Source: Internet
Author: User
Tags greatest common divisor

loop statements and loop control

Objective:

Turbo C 2.0 provides three basic looping statements: For statements, while statements, and do-while statements.


first, the Loop statement
(a), for loop
For (< initialization >;< condition table over >;< increment >)
Statement
Initialization is always an assignment statement, which is used to assign an initial value to a loop control variable; a conditional expression is a relational expression that determines when the loop is exited, and the increment defines how the loop control variable changes after each loop. These three parts are separated by A;
For example:
for (i=1;i<=10;i++)
Statement
In the above example, I was given an initial value of 1, to determine whether I is less than or equal to 10, if the execution of the statement, then increased by 1. Then re-judge until the condition is false, i.e. i>10, the end loop.
Attention:
(1). The statement in the For loop can be a statement body, but enclose the statements that participate in the loop with {and}.
(2). initialization, conditional expressions, and increments in a for loop are selections, which can be defaulted, but not default. Initialization is omitted to indicate that the loop control variable is not assigned an initial value. If a conditional expression is omitted, it becomes a dead loop when no other processing is done. If an increment is omitted, the loop control variable is not manipulated, and a statement that modifies the loop control variable can be added to the body of the statement.
(3). For loops can have multiple layers of nesting.
For example:
for (;;) statement;
for (I=1;; i+=2) statement;
for (j=5;;) statement;
These for loop statements are correct.
The output is:
I J
0 0
0 1
0 2
1 0
1 1
1 2

Use for loop to find 1+2+......+100 and:
Main () {int sn=0,i;for (i=1;i<=100;i++) sn+=i;/*1+2+......+100*/printf (%D\N,SN);}

As you can see from the program, using loop statements can greatly simplify your code.
(b), while loop its general form is:
While (condition)
statement;
The while loop indicates that the statement executes when the condition is true. The loop ends until the condition is false. and continue to execute subsequent statements outside of the loop program.
For example:

In the above example, the while loop is to check whether C is a carriage return start, because it was initially initialized to NULL, so the condition is true, enter the loop to wait for the keyboard input characters, once the input enter, then c= ' \ n ', the condition is false, the loop will end. As with a For loop, the while loop always checks the condition in the loop's head, which means that the loop may exit without doing anything.
Attention:
(1). Empty statements are also allowed in the while loop body.
For example:
This loop until you type Enter.
(2). Can have multi-layer loop nesting.
(3). The statement can be a statement body, which must be enclosed in {and}.
Using while loop to find 1+2+......+100 and:
Main () {int sn=0,i=0;while (++i<=100) sn+=i;/* for 1+2+......+100*/printf (%D\N,SN);}


(c), Do--while cycle its general format is:
Do
{
statement block;
}
while (condition);
This loop differs from the while loop in that it executes the statement in the loop before judging whether the condition is true, and if it is true, the loop is terminated, or if it is false. Therefore, the Do-while loop executes at least one loop statement.
Also, when there are many statements in the loop, enclose them with {and}.

Using the Do--while cycle to seek 1+2+......+100 and:
Main () {int sn=0,i=1;dosn+=i;/* Seek 1+2+......+100*/while (++i<=100);p rintf (%D\N,SN);}
From the above three procedures, the use of for,while and do--while to solve the same problem, the basic idea is similar, but in the first calculation, pay attention to the initial value.


second, cycle control
(i), break statement
Break statements are typically used in loop statements and switch statements. When break is used in switch statements, the program can jump out of a switch and execute a switch statement, and if there is no break statement, it becomes a dead loop and cannot exit. The use of break in switch has been encountered in the example described earlier in the switch statement, which is no longer an example.
When the break statement is used in a do-while, for, and while Loop statement, the program terminates the loop and executes the statement following the loop, usually the break statement is always associated with the IF statement. That is, it jumps out of the loop when the condition is met.
For example:
Main () {int sn=0,i;for (i=1;i<=100;i++) {if (i==51) break;/* If I equals 51, then jump out of loop */sn+=i;/*1+2+......+50*/}printf (%D\N,SN);}
As you can see, the end result is1+2+......+50。 Because when I equals 51, I jump out of the loop. Write yourself how to add a break statement in the while and do--while loops.
Attention:
1. The break statement does not work for if-else conditional statements.
2. In a multilayer loop, a break statement jumps only one layer outward.
For example:
The output is:
I J
0 0
0 1
1 0
1 1
When i==0,j==2, executes the break statement, jumps out into the outer loop, and I becomes 1.
(b), continue statement
The continue statement acts by skipping the remaining statements in the loop and forcing the next loop to be executed.
The continue statement is used only in the loop body of for, while, Do-while, and often with an if condition statement to speed up the loop.
For example:
Main () {int sn=0,i;for (i=1;i<=100;i++) {if (i==51) continue;/* if I equals 51, end the loop */sn+=i;/*1+2+......+50+52+......+100*/} printf (%D\N,SN);}
As can be seen from the program, the continue statement is only the current value is not executed, that is, the current value jumps past, and then the next loop is executed.
The output is:
I J
0 0
0 2
1 0
1 2
(c), goto statement
A goto statement is an unconditional transfer statement similar to a goto statement in BASIC. The goto statement is used in the following format:
Goto marking;
Where the label is a valid identifier in Turbo C 2.0, which is appended with a: somewhere inside the function, after the goto statement is executed, the program jumps to the label and executes the subsequent statement. Since the label is an identifier, it is also necessary to satisfy the naming rules for identifiers. In addition, the label must be in a function with the goto statement, but it may not be in a loop layer. Usually a goto statement is used with an IF condition statement, and when a condition is met, the program jumps to the label. The goto statement is usually not used, mainly because it will make the program level unclear, and difficult to read, but in the multi-layered nesting exit, with a goto statement is more reasonable.
Main () {int sn=0,i;for (i=1;i<=100;i++) {if (i==51) goto loop;/* If I equals 51, jump out of loop */sn+=i;/*1+2+......+50*/}loop:;p rintf (%d \N,SN);}
As you can see, the goto statement here is similar to the break action.
Here's loop:;
printf (%D\N,SN);
Can also be written as loop:printf (%D\N,SN);
Main () {int sn=0,i;for (i=1;i<=100;i++) {if (i==51) goto loop;/* If I equals 51, then jump out of this loop */sn+=i;/*1+2+......+50+52+......+100*/loop : ;} printf (%D\N,SN);}
You can see that the loop statement here is similar to the continue.
However, in some cases, you must use a goto statement, or you will make the program much bloated. Such as:

The output is:
I j K
0 0 0
0 0 1
If you do not use the Goto statement and use the Break,continue statement, you should
Main () {int i,j,k;printf (i j\n); for (i=0;i<2;i++) {for (j=0;j<3;j++) {for (k=0;k<3;k++) {if (k==2) break;printf ( %d%d%d\n,i,j,k);} if (k==2) break;} if (k==2) break;}}

The output is:
I j K
0 0 0
0 0 1
So when you jump out of a multilayer loop, you should use a goto statement. Remember, all goto statements can be replaced with break,continue.


Here are a few examples:
1. Ask for a greatest common divisor of two integers. For example 10 and 15 of the greatest common divisor are 5.
Analysis: Greatest common divisor must be less than or equal to the smallest number of half, and can be divisible by two number.
Main () {int num1,num2,i,min;scanf (%D%D,&NUM1,&NUM2); Min=num1 for (i=min/2;i>0;i--) if (num1%i==0& &num2%i==0) break;printf (greatest common divisor for%d\n,i);}

2. Seeking 1!+2!+......+n! (N&LT;10)
Main () {int N,i;long temp=1,sn=0;/* from 9! Later, the resulting value exceeds the INT range */scanf (%d,&n); for (i=1;i<=n;i++) {temp*=i;sn+=temp;/* If this is not the case, ask for n!*/}printf (%LD\N,SN);}

3. Determine if an integer is a prime number (the primes are only divisible by itself and 1).

In all C language books, there are examples of judging primes. Its programming idea is: To use a variable as a flag variable, used to flag is not a prime number, the loop body is from 2 to sqrt (num), because if a count is not a prime, it must be decomposed into num=num1*num2, the minimum value must be less than sqrt (num), So just go to sqrt (num) when you're looping. Also pay attention to the problem of variable reset.

C Language Introduction (ix) cyclic statements and loop control

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.