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

Source: Internet
Author: User
Tags continue execution goto printf

Transfer statement

Statements in a program are usually performed in sequential direction, or in the direction defined by the statement function. If you need to change the normal flow of your program, you can use the transfer statement described in this section. There are 4 kinds of transfer statements in the C language:
Goto,break, continue and return.
The return statement can only appear in the called function, which returns the keynote function, which we will describe in the function chapter. This section describes the first three transfer statements.

1.goto statement

A goto statement is also called an unconditional transfer statement, and its general format is as follows: Goto statement label, where the statement label is a symbol written according to the identifier, placed in a statement line
Preceded by a colon (:) after marking. The statement label acts as an identifying statement and is used in conjunction with the GOTO statement.
such as: label:i++;
Loop:while (X<7);
C language does not restrict the use of the number of labels in the program, but each label must not duplicate. The semantics of a goto statement is to change the flow of the program and to execute the statement identified by the statement label.
Goto statements are usually used in conjunction with conditional statements. can be used to achieve conditional transfer, forming a cycle, jump out of the circulation body and other functions.
However, the use of GOTO statements in structured programming is not generally advocated, so as to avoid the confusion of program flow, which makes it difficult to understand and debug the program.
Counts the number of line characters entered from the keyboard.
#include "stdio.h"
void Main () {
int n=0;
printf ("Input a string\n");
Loop:if (GetChar ()!= ' \ n ')
{n++;
Goto Loop;
}
printf ("%d", n);
}
This example uses an if statement and a goto statement to form a looping structure. When the input character is not ' \ n ', the n++ is counted and then transferred to the IF statement for loop execution. The loop is not stopped until the input character is ' \ n '.

Break statement

A break statement can only be used in a switch statement or Loop statement, which is to jump out of a switch statement or loop out of this layer, and then go to the following program. Because the transfer direction of the break statement is clear, it does not need to match the statement label. The general form of the break statement is: break; The break statement is used as a jump in the switch statement and the For statement, respectively, in the example above. Use the break statement to make the loop statement have multiple exits, making programming more flexible and convenient in some cases.

Continue statement

The continue statement can only be used in the loop body, and its general format is:
Continue
Its semantics is: to end this cycle, that is, no longer execute the sentence after the continue statement in the circulation body, to the judgment and execution of the next cyclic condition. It should be noted that this statement only ends this layer of the loop, does not jump out of the loop.
void Main () {
int n;
for (n=7;n<=100;n++)
{
if (n%7!=0)
Continue
printf ("%d", n);
}
}
The number that can be divisible by 7 within output 100.
int n;
for (n=7;n<=100;n++)
{
if (n%7!=0)
Continue
printf ("%d", n);
}
In this example, each number of 7~100 is tested, if the number cannot be divisible by 7, the modulo operation is not 0, then the Continus statement turns to the next loop. Only the modulo operation is 0 o'clock to perform the following printf statement, and the output can be divisible by 7.
#include "stdio.h"
void Main () {
Char a,b;
printf ("Input a string:\n");
B=getchar ();
while ((A=getchar ())!= ' \ n ') {
if (a==b) {
printf ("Same character\n");
Break
}b=a;
}
}
Check that there are no adjacent two characters in the line entered.
Char a,b;
printf ("Input a string:\n");
B=getchar ();
while ((A=getchar ())!= ' \ n ') {
if (a==b) {
printf ("Same character\n");
Break
}b=a;
}
In this example program, the first read character is fed into B. Then enter the loop, the next character read into a, compare the a,b is equal, if the same output hint string and stop the loop, if not equal to the character in a B, enter the next loop.
The prime number within 100 of the output. A prime is a number that can only be divisible by 1 and itself. It is possible to determine whether a number is prime by exhaustive method.
void Main () {
int n,i;
for (n=2;n<=100;n++) {
for (i=2;i<n;i++)
if (n%i==0) break;
if (i>=n) printf ("\t%d", N);
}
int n,i;
for (n=2;n<=100;n++) {
for (i=2;i<n;i++)
if (n%i==0) break;
if (i>=n) printf ("\t%d", N);
}
In this example program, the first layer of the loop to determine whether the 100 number of 1~100 is a prime, a total of 100 times, in the second cycle of the log n with 2~n-1, one after another, if the other is to jump out of the layer cycle, indicating not prime. If all the numbers are not in the end of the loop, then the prime number, there is i>=n, it can be judged after the output prime. Then move on to the next big cycle. In fact, more than 2 of all even numbers are not prime, so you can change the step value of the loop variable to 2, that is, increase by 2 each time, in addition to log n with 2~n removal can determine whether the number is prime. This will greatly reduce the number of cycles and reduce the running time of the program.
#include "math.h"
void Main () {
int n,i,k;
for (n=2;n<=100;n+=2) {
K=SQRT (n);
for (i=2;i<k;i++)
if (n%i==0) break;
if (i>=k) printf ("\t%2d", 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.