ComputersProgramming

What is a conditional statement?

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

What is a conditional statement?

So (or, otherwise, a conditional branch instruction) is called the construction of a programming language. It provides execution of a command or a set of commands. But this is possible only if the condition of the truth of a certain logical expression is observed or an instruction was given from the user (operator of technology) to the embodiment.

General application description

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

Conditional branch operator

First let's take a little distraction and recall the rather popular picture of Victor Vasnetsov "The Knight at the Crossroads", written in 1878. She depicts a hero who stopped at a crossroads and decides where to go. Similarly, the conditional operator acts. So, if the knight accepts the offer to go one way, but he will go on one branch. And there can be only one choice. If we talk about the conditional operator, it works only when its logical expression has the value "true". Most programming languages use the keyword if for its designation. It is possible to construct a cascade, when one condition is checked first, then another, then the third, the fourth, and so on. Let's consider this issue in more detail. What are the forms of conditional operators? There are only three:

  1. A conditional statement in which there is only one branch. It looks (averaged) this way: if necessary conditions then the commands end. The computer calculates whether the logical value corresponds to the truth value. If the condition coincides with the data of other parts of the program, then the code begins to be executed until the end keyword. In assemblers, this form of work is the only one available for the programmer. It can also be that in the end there will be no key word, and then it is necessary to be pointed, so as not to attribute something superfluous.
  2. A conditional statement in which there are two branches. It looks like this: if the specified condition then command1 else command2 end. This software design 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 part. In some cases, such a decision is necessary.
  3. A conditional operator with a number of conditions. It looks like this:
    If the first condition then what to do
    Else if if the first condition is not suitable, check the following then what to do
    Else if we check all the conditions one by one then what to do
    Else if none of the above options is suitable, then this command end

As you can see, the conditional operator uses different constructions. They are executed sequentially. That is, the situation is impossible (on normal technique), at which the code execution will start from the end - always from the beginning.

Example with "Pascal"

To understand how this works, let's consider an example in one of the most popular programming languages. The conditional operator in Pascal can be simple and complex. The first type is symbols like =, <,> and other similar ones. A complex conditional operator in Pascal can take the following form:

If a> = y
Then
Op: = Sqr (a-y)
Else
Write (Invalid values entered);

What is a switch?

What is the switch? The peculiarity of its design is that it has at least two branches. But it can only perform one previously specified parameter, which is evaluated by key expressions. Speaking about the difference from the previously discussed 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 brought to it. Also in some programming languages, you can compare with text strings.

History of development

Initially, a command was used, indicating the transition to the calculated label. It specified 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 transferred.

Similar constructions can be seen in programming languages such as BASIC and FORTRAN. We can not say that they are much outdated, because until now they can boast of high enough efficiency, if we talk at all about programming. A conditional operator, which is used in modern languages, is a more complex construction, which affects the speed. Returning to the origins of this element, I would like to add that to determine the required branch, you do not need to sequentially process and compare the result of the expression with other values. Simply there is an entry in the memory of an array of unconditional jump commands, in which the necessary addresses are contained. 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 that are widely used at the moment, the switch operator can also be executed as a transition table, consisting of instructions that move attention to the necessary code fragments without different conditions. How are they denoted? In high-level programming languages, in most cases, use case, swithc, or both.

Features of calculating logical expressions

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

  1. Full calculation. All parameters are calculated with the given values, and then I.
  2. Incomplete calculation. Each parameter is considered separately. If the expression has a value of "true", then the next parameter is computed. Valid for OR.

Conclusion

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

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.