What is a conditional statement?

In programming, it is important not only knowledge of languages, but also an understanding of what is responsible for what. This is necessary for the possibility of successful teamwork, and as part of the article you will learn what a condition operator is, why it is needed, what features are there. Also, switching and branching of the code for which it is applied will be outlined in general terms.

What is a conditional statement?

conditional operator

So (or, otherwise, a conditional branch instruction) is called the construction of a programming language. It provides the execution of a command or their set. But this is possible only when the condition of truthfulness of a certain logical expression is met or an instruction from the user (equipment operator) has been received for implementation.

General Description of Application

conditional operator in pascal

The conditional operator is used when the execution or ignoring of a certain set of commands (less often commands) depends on whether there are determining factors for this. It can also be used with an eye on branching. It is one of the three basic constructs that are used in structural programming.

Conditional jump operator

First, let's digress a bit and recall the rather popular painting by Viktor Mikhailovich Vasnetsov, “The Knight at the Crossroads”, written in 1878. It depicts a hero who stopped at a crossroads and decides where to go. The conditional operator acts in a similar way. So, if the hero accepts the offer to go in one direction, but he will go along one branch. And there can only be one choice. If we talk about the conditional operator, then it works only when its logical expression has the value “true”. Most programming languages ​​use the if keyword to indicate it. It is possible to build a cascade when one condition is checked first, then another, then the third, quarter and so on. Let's look at this issue in more detail. What are the forms of conditional statements? There are three of them:

  1. A conditional statement in which there is only one branch. It looks (averaged) like this: if necessary conditions then executed end commands. The computer machine calculates whether the logical value corresponds to the indicator of truth. If the condition matches the data of other parts of the program, then the code starts executing until the end keyword is found. In assemblers, this form of work is the only one available to the programmer. It may also be such that at the end there will not be some kind of keyword, and then it is necessary to be careful not to attribute something superfluous.
  2. A conditional statement in which there are two branches. It looks as follows: if the specified condition then command1 else command2 end. This software construct is used in cases where it is necessary that one of the commands is exactly executed. Therefore, if the condition is true, then the first part of the code is executed, if not, the second. In some cases, such a solution is necessary.
  3. A conditional statement with a number of conditions. It looks like this:
    if first condition then what to do
    else if if the first condition does not fit, check the next then what to do
    else if check all conditions one by one then what to do
    else if none of the above options fits, then this command is executed end

As you can see, the conditional statement uses various constructs. Their implementation occurs sequentially. That is, a situation is impossible (on a normal technique) in which code execution starts from the end - always from the beginning.

Pascal example

conditional operator

To understand how this works, we suggest considering an example in one of the most popular programming languages. The conditional statement in Pascal can be simple and complex. The first type is characters like =, <,> and other similar ones. The complex conditional operator in pascal can take the following form:

if a> = y
then
op: = Sqr (ay)
else
write (Invalid values ​​entered);

What is a switch?

programming conditional statement

What is a switch? The peculiarity of its design is that it has at least two branches. But it can only perform one, given previously by parameters that are calculated by key expressions. Speaking about the difference from the previously considered instructions, you should pay attention to the fact that the return is not a logical value, but the whole, as well as the types that can be cast to it. Also, in some programming languages, comparisons can be made with text strings.

History of development

conditional statement uses

Initially, a command was used to indicate a transition along a calculated label. It indicated an expression acting simultaneously as a selector, which returned an integer value, as well as a set of transition instructions. When the command was executed, a certain value was calculated, which was used as the label number in the entire list of commands, to which the attention of the machine was shifted.

Similar constructions can be seen in programming languages ​​such as Basic and Fortran. This is not to say that they are significantly outdated, because so far they can boast quite high efficiency, if we talk about programming at all. The conditional operator, which is used in modern languages, is a more complex construction, which affects performance. Returning to the origins of this element, I would like to add that in order to determine the necessary branch, you do not need to process and compare the result of the expression with other values. It just takes place in the memory of the array of commands of the unconditional jump, which contains the necessary addresses. When the necessary instruction is executed, the necessary information is simply calculated. And the speed of the program becomes independent of the number of labels.

In modern programming languages, which are widely used at the moment, the switch operator can also be performed as a transition table, consisting of commands that move attention to the necessary code fragments without various conditions. How are they designated? In higher-level programming languages, in most cases they use the names case, swithc, or both of them at the same time.

Features of the calculation of logical expressions

A significant influence on how a program with a conditional statement will be executed is provided by the logic of conditional expression calculations adopted in each particular case. There are two main strategies.

  1. Full calculation. All parameters are calculated with the provided values, and then I.
  2. Incomplete calculation. Each parameter is considered separately. If the expression is true, then the next parameter is calculated. Valid in case of OR.

Conclusion

conditional jump operator

Conditional statements are a very convenient construct in programming. With their help, you can easily implement complex designs without the need to philosophize.

Source: https://habr.com/ru/post/K17594/


All Articles