Statements and Control Structures

This page was translated by a robot.

In order for a computer to be taught what to do, it must be given commands. In the C and C++ languages, such commands are called statements . A statement basically consists of an expression , which ends with a semicolon . Statements are processed sequentially one after the other during runtime. In order to intervene in the processing, the control structures are required in C and C++.;





Set a to 5.
Set b to 3.
Set c to the sum of a and b.
Repeat as long as c smaller than 10
  Print the content of c
  Add 1 to c
  
Exit the program
#include <stdio.h>

int main(){
  int a, b, c;
  a = 2;
  b = 3;
  c = a + b;
  while(c < 10){
    printf("%d\n",  c);
    c++;
  }
  return 0;
}

Details

Although C is considered a so-called command language (see The C and C++ Languages ​​), the term command or instruction is frowned upon these days. An instruction is the correct term for an assembler command: a unique code that the processor should execute. However , one instruction in C is often translated into many instructions in assembler. However, due to the Germanization of the English instructionthis term is sometimes also used for C, such as with simple instructions or functions (usually without a return value and only a few parameters). In C, however, the term statement should be preferred to instruction.

A single statement always consists of an expression , that is, of several nested values ​​and operators . The order in which the individual operators are processed is precisely specified, which can be looked up in the processing order. Each individual operator returns a value that is treated as input for the next operator in the expression. The value of the last operator of the statement is simply ignored. Thus it is even possible that a statement consists of only one value or even of nothing at all, however, such a statement accordingly has no effect. In this case, a compiler often issues a warning likeStatement has no effect .

Several statements are processed sequentially one after the other. When processing control structures, on the other hand, this sequential processing is interrupted. With them, for example, program parts are listed several times or only under given conditions. Control structures instruct the computer to continue the program at a different point if necessary. Some, but not all, control structures expect a semicolon ;as a termination, just like statements do. The control structures that do not ;require a semicolon as a termination expect instead a separate statement or an entire block of code that is enclosed in curly brackets { }. See statement Blocks .

Forgetting the semicolon ;at the end of the statement is a very common mistake for newcomers to C syntax, but in most cases it's easy to fix, especially since today's compilers almost always recognize where a semicolon should go. The semicolon separates sequential statements from each other, which means that the program text (in contrast to so-called functional languages, for example) is divided into individual, smaller statements.

Function prototypes as well as declarations, definitions and initializations of variables are also terminated with a semicolon ;. However, they are processed by the compiler independently of the execution of statements, depending on the situation, and are NOT considered statements.

Next Chapter: Scopes and Files