Home  >  Article  >  Backend Development  >  What are the three basic program structures in C language?

What are the three basic program structures in C language?

烟雨青岚
烟雨青岚Original
2020-07-16 11:11:2129916browse

The three basic program structures of the C language are: sequential structure, selection structure (branch structure), and loop structure. The sequential structure is executed sentence by sentence from beginning to end; the selection structure, after reaching a certain node, will decide which branch direction to execute next based on the result of a judgment; the loop structure has a loop body, and the loop body is a paragraph code.

What are the three basic program structures in C language?

The three basic program structures of the C language are: sequential structure, selection structure (branch structure), and loop structure;

1. Program structure: In C language programs, there are three program structures: sequential structure, selection structure (branch structure), and loop structure;

Sequential structure: execution sentence after sentence from beginning to end down until the last sentence is executed;

Selection structure: After reaching a certain node, the branch direction to be executed will be determined based on the result of a judgment;

Loop structure: Loop structure There is a loop body, and inside the loop body is a piece of code. For the loop structure, the key is to decide how many times the loop body is executed based on the judgment result;

Note: Logically there is a bool type (also called boolean type, Boolean type), which has only two values. , that is, true and false. The final value of the judgment expression in C language is a bool type. The bool value of this judgment expression determines how to select the selection structure and how to loop the loop structure;

2. Sequential structure: The sequential structure is very simple. Generally, in addition to selection structures and loop structures, what we encounter are sequential structures;

3. Selection structure: There are two main selection structures commonly used in C language:

(1 ) if else:Introduction of keywords: if     else if

  if (bool值)     // 如果bool值为真,则执行代码段1,否则执行代码段2
    {
      代码段1
    }
    else
    {
      代码段2
    }
    if (bool值1)    // 如果bool值1为真,则执行代码段1,否则判断bool值2是否为真
    {
      代码段1
    }
    else if (bool值2)  // 若bool值2为真则执行代码段2,否则直接执行代码段3
    {          // 开头的if和结尾的else都只能有一个,但是中间的else if可以有好多个
      代码段2
    }
    else
    {
      代码段3
    }

(2) switch case:Introduction of keywords: switch  case break default

switch (变量)       // 执行到这一句时,变量的值是已知的
    {             // switch case语句执行时,会用该变量的值依次与各个case后的常数去对比,试图找到第一个匹配项,找到匹配的项目后,
       case 常数1:       // 就去执行该case对应的代码段,如果没找到则继续下一个case,直到default
         代码段1;        // 如果前面的case都未匹配,则default匹配。
         break;
       case 常数2:  
         代码段2;
         break;
       ……
       default:
         代码段n;
         break;
    }

Note:

First, the case must be a constant, and it must be an integer;

Second, generally speaking, after the code segment in each case There must be a break;

Thirdly, there is usually a default after the case. Although the syntax allows no default, it is recommended that you must write it when writing code;

Recommended tutorial: "C language tutorial

The above is the detailed content of What are the three basic program structures in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn