Select if and else statements in C++

операторы логического выбора if else в  C++

Prior to this lesson,, learning basics of programming for beginners, we'll write a very simple and boring program, which somehow worked, We are doing something, but they were completely deprived of the ability to choose, as they grow. Such programs are called linear. They were, as trams: If the path is blocked – Stop. No right, neither left not turn. Now, I want all the little good news – Our program today will be much more interesting. We will learn to use in our code operators logical choice if and else and the program will become a kind of intrigue, can “think”, analyze and solve, what to do and what “road” go. That is now the program – This is not the tram, and your car ))) If the road was blocked, you can easily drive around an obstacle and decide, where to go next.

Before, you begin to study this topic, you need to have a good knowledge, what logic operations. So that, if someone is not sure, take the time to read an article about them.

Well – we proceed to the choice of our operators (they are also conditionals, they are also branching statements). We will look at some simple examples, to help you get acquainted with these operators and to use them in the future in a more complex programs.

Example: The user enters two numbers and the program determines which of these two numbers is greater.

Up to 14-th row all clear – the user enters two numbers, they have signed the corresponding variables. How to compare them now and show the result of the comparison? To do this, we apply the operator if (translated-IF). Writing if, next to the parentheses () record condition – Compare the values ​​of variables (variable1 > variable2). And if variable1 really more variable2 – this condition will return true (truth). Then the program will go to the code, which is placed inside the braces {} – strings 21 – 23. That is, we will see on the screen a message, that the first number is greater than the second. If the condition in parentheses () not performed (returns false (lie)), the code in curly braces {} It will be ignored and the program continues with the next line of code under the block if – with the string 26. There we again check the relationship between a variable. All similarly – If the condition is true – Run the code of this unit if, if false – move below. In string 33 check the last possible option – check equality. Be sure to use the operator == (equal to), but not = (assigned). Otherwise, this unit code if It will work with the error. For example variable1 = 3, and variable2 = 4 , that if we mistakenly written in the subject if (variable1 = variable2) , value 4 written in variable1. Get if (4) . And any value other than 0, as you remember, the compiler sees as true . As a result, this block is executed, and we will see a message that, that the values ​​are equal, although it was initially not so. be careful.

This code can be considerably reduced. At first, You can add operator else (see below). Secondly – if the blocks if or else it contains only one line of code in curly brackets {} , these braces, you can not write. We will see, how to change our code:

You see, that the braces in this example we do not use. Another plus – if the first condition if performed, the remaining blocks if else and else, not checked (i.e.. time for execution of the program is reduced). Well, if the first condition is false, the program goes to the next block else if (else if), and verifies its condition – (variable1 < variable2) – string 18. If there lie, then execute the last block else, in which the condition can no longer check, as we previously went over all possible relations between the two values, and there is only the equality.

The result of these programs is the same. But in the second case, and more compact code, and the compiler less.

операторы логического выбора if  elseMain, it is important to remember:

  • syntax if:

операторы логического выбора if else

  • syntax if else:

операторы логического выбора if else

or if you want more conditions:

операторы логического выбора if else

  • block design if else – inseparable. If it is found in the code, it is impossible to separate the extraneous code. For example :

операторы логического выбора if else

  • Work-selection principle if – if the condition is true in parentheses (true), the code block is executed. If false (false) – is ignored and the program starts with a line of code, under the following block if.
  • statement selection condition in parentheses may be a conventional: if(variable > 0) or combined: if (variable > 0 && variable < 100). Combined condition returns true, If both conditions are true.
  • the condition of the statement selection can be an arithmetic expression: if (variable1 – variable2). block code if executed, if the calculation result is not equal to 0. For example: if (5 – 7), if (2 + 5). But if the if (2 -2) – code block will be ignored.
  • if the blocks if , else if or else It contains only one string of code, braces can not be used. But for beginners, while you learn, I recommend always use braces.

And perhaps all of the conditional statements if and else. There is a programming practice on this topic.

Possibly, Watch this video. It covers conditional statements if and else, boolean statements && (And), || (OR), !(NOT), ternary statement ?: (15--s minutes), and multiple-choice statement switch (17--s minutes).

Subscribe to notifications about new articles on our website:

Agree to receive notifications from to my e-mail

Select if and else statements in C++

6 thoughts on “Select if and else statements in C++

  1. Is it possible to do so?
    *Code *
    if(*another condition *)
    *Code *

    1. Could be so, Once this is one if statement, and followed by a second independent statement if … Only then it is necessary to divide them ;
      But you should understand what you are doing:

      It executed two groups of statements: №1 and №2.
      And if you want to exclusive branches, you need if … else if … , as described above, or case statement.

  2. а если в условиях будут стоять =; ==, какой код выполнится в этом случае?

  3. ой, неправильно отправлено сообщение. я имел ввиду больше-равно, меньше-равно, equal to

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>