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:
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:
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.
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).
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.
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.
Statement 1: Read three integers and large numbers (Program 4).
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).
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:
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.