Javascript If two conditions are executed to display a message. Conditional operators. Cyclic Operators of the JavaScript language - for
Lesson number 5.
IF branch operators in javascript
Branch operators Designed to ensure that the program can run this or that block of code, depending on the faithfulness of True or not faithful to FALSE conditions.
Branching operators There are five species, in this lesson we will look at two of them:
- Branch Operator IF
- Branch Operator IF ELSE
Runs the code if the condition returns TRUE.
As a condition, in branching operators, comparison or logical operations are usually performed.
The scheme of the IF branch operator is as follows:
If (condition) (code starts if the condition returns true)
We give an example with an IF branch operator:
// Create two variables VAR Numone; var numtwo; // assign variables numone \u003d 5; numtwo \u003d 3; If (Numone\u003e Numtwo) (Alert ("Condition Returned True");)
In the script, we created two variables Numone and NumtWo, assigned them numeric values \u200b\u200b5 and 3.
Then created a branch operator if, which compares the values \u200b\u200bof two variables. If the comparison operation returns True, the code will start the code located between the curly brackets. In our case, the window will appear with the message, the condition returned True. If the comparison operation returns False, then nothing happens.
Double layers //, are comment. After a double slash, you can write any text, the JavaScript language interpreter, will perceive it as a comment and will not be processed. As we remember, in languages \u200b\u200band, you can also create comments.
Branch Operator IF ELSEIt is designed to start a block of code, depending on the value that the condition returns: True or False
The IF ELSE Branch Operator Scheme is as follows:
If (condition) (code starts if the condition returns true) ELSE (the code will start if the condition returns false)
We give an example with the Branch Operator if Else:
Var numone; var numtwo; numone \u003d 5; numtwo \u003d 3; If (Numone\u003e Numtwo) (Alert ("Condition Returned True");) ELSE (Alert ("Condition Returned False");)
Assign the NUMTWO variable, the number is greater than 5, for example 7, then the condition returns false and the window will appear with the condition, the condition returned FALSE.
Let's go back to our hares
Recall the task about Zaitsev and Mazaya from last chapter.
Without the use of conditions and actions performed (or not executed)
depending on the conditions, calculate the number of hares in the last
the boat will not be easy.
Add a Lastboatrabbit branching to the poundation:
Var Lastboatrabbits \u003d. fUNCTION (TotalRabbitS)(if (TotalRabBits \u003d\u003d\u003d 0) (// Return 0;) ELSE (var restrabbits \u003d TotalRabBits% 6; if (restrabbits \u003d\u003d\u003d 0) (// Return 6;) ELSE (RETURN RESTRABBITS;)));So, if there are no hares on the river, the last boat will not bring anyone (in fact, it will return almost immediately, because Grandfather will definitely know that there are no hares on the river).
And if the number of hares on the river is more 6, then the latter will be a boat completely loaded by hares.
What else could it be improved in this program? I would use a separate variable,
storen the number of hares placed in the boat, in case grateful
hares will give a big boat with a big boat. Like which suddenly.
Syntax branching
Branching may have a part that is executed if the condition is incorrectly
and may not have it:
Conditions
As a condition in IF, an expression of a comparison of two numbers or lines may appear
using operations \u003d\u003d,\u003e,< , >= , <= , != и === , любая переменная, которой
it was assigned a logical value and simply any value as a result of work
If will be reduced to a logical value.
Examples of comparison:
10\u003e 5 // \u003d\u003e True 11< 6 // => False 5\u003e \u003d 5 // \u003d\u003e True 3! \u003d 3 // \u003d\u003e False "ABC" \u003d\u003d "ABC" // \u003d\u003e True "ABC" \u003d\u003d\u003d "ABC" // \u003d\u003e TrueVariables as a condition:
VAR CONDITION \u003d 10\u003e 5; If (CONSOLE .LOG ("10\u003e 5"); // will be executed }Logic Operations on Terms
Several expressions returning a logical (or logical) values
you can combine with logical operations. Such operations are called:
logical and &&, logical or || and logical denial! .
Logical and returns true only if TRUE from both sides
logical or returns False only if it is false from both sides.
The denial returns false for true and, on the contrary, True for False.
According to the rules of JavaScript, the values \u200b\u200bof 0, null and undefined are given to FALSE.
However, getting the result && we will get the first integral value that
caused to False, and getting the result || - the first integral value
which is given to True:
Thus, the function that returns the number of hares in the last boat could be rewritten so:
var boatcapacity \u003d 6; Var Lastboatrabbits \u003d. fUNCTION (TotalRabbitS)(Return TotalRabBits && (TotalRabBits% Boatcapacity || Boatcapacity););Tasks
- Write the FizzBuzz feature that accepts the Number and:
- For numbers, multiple three, returns "Fizz"
- For numbers, multiple five, returns "buzz"
- For numbers, multiple fifteen (at the same time three and five), returns "Fizzbuzz"
- In other cases, returns the original number
- Write an Igotonorth poundation that accepts the Number parameter and determines whether the number is suitable for us. The number is suitable if it is greater than 10, less than 30 and more times 7.
Good day. In touch, Alexey Gulein. In the last article we disassembled creating cycles in javascript . In this article I would like to tell about conditional Operator in JavaScript. In general, all programming consists of conditions, and in all programming languages \u200b\u200b(JavaScript has not exceeded) there is a concept of a conditional operator. The meaning of the conditional operator It is that if the condition is performed, then these actions are made if it is not done - then do other actions. The general scheme of the conditional operator looks like this:
If (condition) (// block of operators); ELSE (// block of operators)
Let's immediately write any example: we will request a user's number and, if it is even, output "This is an even number", if not - "This is an odd":