Programming language C (V). Control structures II

After analyzing in the previous chapter the conditional structures F and switch and the repetitive structures while and do-while, the objective in this chapter focuses on explanations.

IF

After analyzing the conditional and repetitive structures of while and do-while in the previous chapter, the objective of this chapter focuses on the following explanations:

  • Structure for repetitive
  • break and use of sentences continue in repetitive structures.
  • Use of structures and expressions analyzed in examples of interest: if digged, bit handling.
  • The character string and the definition and use of tables in repetitive structures.

Structure FOR

In this complex repetitive structure expressions of initialization and updating per cycle are supported on the softening body and the repetition condition, resulting in some cases a very comfortable structure.

The syntax is as follows:

Its operation is as follows:

  1. evaluate the initial expression (initialization).
  2. evaluate the expression of the baldines. If it is false to move to the next sentence (end).
  3. if the expression of baldin is true, the expression or body expressions are performed.
  4. evaluate daily expression (update) and 2) to the point.

Therefore, the first expression in parentheses (initialization) is executed once, the second at the beginning of each cycle and the third (update) at the end of each cycle. However, what is done with the for structure can be done by while and assignment as you can see below.

The calculation of the factorial described in Program 1 in the previous chapter can be seen using the for structure.

Program 1. Factorial by for.

If the expressions of the structure are optional, the expression of the baldin should always appear, otherwise the softening would be infinite. When the first and third expression do not appear, for and while are totally equivalent.

The body can be formed by a single expression or a set of expressions separated by{ y}, also admitting the empty body (in this case the body is formed by the character).

Break and Continue

In repetitive structures, even if the expression that controls the bigal (baldin expression) is sometimes fulfilled, it is advisable to exit the bigal if there has been an exception. To do this, break is used in repetitive structures.

An example can be seen in program 2. It reads 80 characters and counts the number of capitals. However, if you read the ‘.’ character, no more characters should be read.

Program 2. Use of the Break judgment.

In other cases, when the body of the repetitive structure is complex, it can happen that, despite wanting to continue softening in a situation, it is not necessary to execute a part of the body, for which a sentence is produced continue very suitable.

In the 3rd program you read 20 numbers and you have to calculate your product, but if the number is Ø is not taken into account.

Program 3. Use of sentence Continue.

Examples

Statement 1: Read three integers and large numbers (Program 4).

Program 4. Cavados If.

In this example, the branches of an if show the if ( if caved) structures and when considering each structure as a simple expression, no key is needed.

Statement 2: Read a character and calculate how many bits are in zero state. (Program 5).

Programme 5. Cavados If.

Bit handling is very difficult in higher languages, while Cz is extremely simple (and), | (or), interactive (xor), (left offset) and (right offset). In the example, by operation and a bit is analyzed, since when in operation and we pair a data with a mask with a single bit in state 1, if the data bit corresponding to 1 is zero, and if it is one, the different result is zero. The operation is in a bigal (with the for structure) that is repeated 8 times, but to analyze the different bits it is necessary to move inside the bigal the bits of the mask (as what has been done in the program) or to move the same data.

Statement 3: Read 40 characters and count bits in Ø.

Given this example, there are 2 questions:

  • Can characters be read at once or one by one? It is possible to read all at once, but for this you have to use the type of data string, which we have not analyzed until now.
  • Is it possible to use the code used in statement 2 to resolve this? The answer is yes, but for that, instead of being the main program 5 (main), it must be defined as a non-primitive action (also known as function, procedure or routine), which we will analyze in the next chapter.

Character strings and tables

The basic data seen so far are characters (char), integers (short, int, long) and real (float, double). On them you can define datasets forming tables or vectors, as you can see in the following example:

char [8Ø];

/* String Ø 8 characters */

Table int [Ø 2], matrix [M] [N];

The string is a character table in which the last character is constant ‘\Ø’. This final string character allows you to detect the end of the string when the string is variable length.

Tables can be one-dimensional (string or table in the example) or multiple (the array in the example is binary). In use the name and index (between drugs) are mentioned, with the index between Ø and the number of components minus one. In the definition the name and number of components (between drugs) must be indicated, initialization being optional. An example can be seen in Program 6.

Program 6. Using tables.
Babesleak
Eusko Jaurlaritzako Industria, Merkataritza eta Turismo Saila