Three types of programming structure

Source: Internet
Author: User

Programming languages have three central structures: sequential structure, branching structure, and cyclic structure.

The order structure does not need to say more, is a sentence one sentence of writing. The following are mainly about the two behind.

One, branching structure.

Imagine such a problem, if I am not output Hello world, but want to do a program, enter a number, if the number of inputs is positive, the output "Hello positive", if it is 0, the output "Hello 0", if it is negative, output "hello negative". (in order to make the beginner feel more cordial, the explanation is generally based on the Hello World as the carrier). Obviously this program can not be written in sequential structure, it is necessary to use the branch structure.

Let's take a look at the code of this program.

#include <stdio.h>intMainvoid){    intA; scanf ("%d",&a); if(a>0)  {/*The original sentence here actually does not need curly braces, but in order to review the previous code of norms used to the custom so added braces*/printf ("Hello positive"); }    Else if(a==0) {printf ("Hello 0"); }    Else{printf ("Hello negative number"); }    return 0; } 

Because there is only one sentence in the branch, it can be simplified:

#include <stdio.h>intMainvoid){    intA; scanf ("%d",&a); if(a>0) printf ("Hello positive"); Else if(a==0) printf ("Hello 0"); Elseprintf"Hello negative number"); return 0; } 

In fact, the above program is relatively easy to understand, the following allow me to steal a lazy. Because I found in the blog that some people are doing the same thing with me, so specifically to explain the syntax of the branch structure is done by him, pay special attention to learning logic symbols, such as the above = =, the following is the URL:

Http://www.cnblogs.com/stffer/p/4851676.html

Second, the cyclic structure.

What is the loop structure? Imagine another question: if I'm not going to let you output Hello world once, it's output 10 times, 100 times, and not even a certain time. This will use the loop structure.

Here is the code for output 10 lines Hello World:

#include <stdio.h>int main (void) {    int  i;      for (i=1; i<=; i++) {        printf ("Hello world\n" );    }     return 0

Is it more concise than the 10-sentence printf?

In fact, the power of the loop structure is more than that. The following program: Enter a number n, Output n line Hello world. Can this be written in a sequential structure? Obviously not. Therefore, the function of cyclic structure is not only simple and complicated, but also can accomplish the task that the order structure cannot accomplish. Here is the code for this program.

#include <stdio.h>int main (void) {    int  i,n;    scanf ("%d",&N);      for (i=1; i<=n;i++) {        printf ("Hello world\n");    }     return 0

There is also a loop while loop.

OK, or lazy, the following URL specifically explains the syntax of the looping structure:

Http://www.cnblogs.com/stffer/p/4851701.html

Three types of programming structure

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.