Programming language C (IV). Control structures I
1991/05/01 Alegria Loinaz, Iñaki | Maritxalar, Montse Iturria: Elhuyar aldizkaria
Allows you to select or play expressions using control structures. While the expressions analyzed in the previous chapter allow you to write fully linear and sequential programs, in most programs you must decide whether or not to execute certain commands according to the data, using conditional structures and repeat the execution of the sets of orders using repetitive structures. The purpose of this chapter is to draft and use these structures in language C.
Among the conditional structures, the most used are two and the C language has two structures (if) and one option (switch). In the first one it is chosen between two branches depending on the value of a logical expression. In the second, however, you can put as many branches as you want.
C is very rich in repetitive structures, as it has three types: while, chorus (do/while) and changing (for). Its characteristics will be analyzed later.
IF structure
This structure or sentence is in any language and in C kills the general characteristics, that is, a logical expression is evaluated and the branch corresponding to the next or failing ri (else) is executed depending on the result.
The structural syntax is as follows:
As can be seen, the branch is optional, distinguishing the complete or simple structure depending on the appearance.
For example, the if structure that appears in the next part of the program is simple.
Instead, in the next part we have the whole structure:
Note the following nuances:
- Expressions of branches, ending with character.
- In the branches with more than one order must be specified at the beginning and end of the same.
- To express equality in the conditional expression the agent = and no = is used, since in this case there is no error, but an assignment occurs and if the assigned value is Ø, it will be considered a false result and in the rest as true.
- When there is another if (if kabiatua) within an if there is a problem with the branches, since finding an else may doubt whether it corresponds to the first or second. The rule is that an else always corresponds to the last if- written, regardless of margins.
- Structure condition If? expression 1: expression2, which can be replaced by expression, as seen in program 1.
SWITCH structure
This structure allows you to generate the number of branches you want according to the value of an expression, starting each branch by a case keyword (except the last one, which can start with the default keyword). The corresponding syntax is:
As you can see, the expressions in the branches are optional as the default branch. If more than one expression is placed on a branch, you don't have to put{ y}, but each one must end the character.
When a swicth is found in execution, the baldin expression is evaluated and searched based on the value in which the accounting expression branches the same value. If these branches are found, the judgments corresponding to that branch and the following are made. Only the expressions corresponding to the branch are performed in the Pascal case structure. However, if C wishes, the last expression of the branches will be
...If the value of the expression is not in the branches, the expressions of the default branch will be made, if this branch has been written.
Program 2 explains the conventional use of the switch (used by the break).
WHILE structure
The most general repetitive structure is like Pascal’s “while-do” sentence. From the syntactic point of view:
The expression of baldin is evaluated and if it is true (not Ø) the expressions of the structure are made and the expression is re-evaluated, forming a bigal.
When the evaluation of the expression is false it passes to the next sentence.
The expression in parentheses is the condition of repetition and the body in which the other expression or set of expressions is repeated. If the body has several sentences at the beginning and end are necessary. In program 4 you can see a usage.
DO-WHILE structure
While
in this modification of the structure, the evaluation of the condition occurs at the end of the softening. Therefore, in this do-while structure the body runs at least once. On the contrary, in the while structure it may occur that the first evaluation is false. In the rest they are exactly the same.
The structure syntax is as follows:
Program 5 shows the changes made in the do-while factor program.
if
the new one is used for the Ø and 1 factorials to run correctly.
In the next chapter we will continue with the structures and with the help of examples we will delve into the use of control structures.
Gai honi buruzko eduki gehiago
Elhuyarrek garatutako teknologia