Control structures are instructions which in turn contain instruction blocks. This makes it possible to control the execution of the instructions contained in the blocks according to certain rules.
There are basically two types of control structures: branches and loops. By means of branches it is possible to make the execution of one or more instruction blocks dependent on conditions (case distinction). Loops allow a block of instructions to be executed repeatedly.
If-then conditions with "if" 
You can make the execution of statements conditional. In a branch (conditional instruction), an instruction block is only run through if a condition is met. Alternatively, if the condition is not met, a second statement block can be run through.
With you introduce an if-then-condition (if = if). This is followed by the wording of the condition in brackets. In order to formulate such conditions, you need comparison operators and in most cases also variables. For cases in which the condition is not met, you can define an "otherwise" branch. This is done through (else = otherwise). The Else branch is not absolutely necessary.
The script is noted in a function called, which is called as soon as the HTML file is completely loaded. For this purpose, the DOMContentLoaded event is monitored dynamically with addEventListener.
Within the function, a dialog window () asks the user to enter the password. The return value of, the entered password, is saved in the variable.
Common problem 
It often happens that an if-query doesn't work as expected. Regardless of what value is checked, the If branch is always used and not, as expected, in the Else branch. It is very likely that this is due to the fact that instead of making a comparison, an assignment was noted.
If you write down the assignment instead, it will be answered successfully with.
You can avoid this error by interchanging, since no variable can be assigned to a number.
Simple either-or query 
For simple either-or conditions there is the conditional (ternary) operator a special syntax that you can use as an alternative to the if / else statement.
When the document is loaded, an EventHandler is added with addEventListener, which is called when the user clicks on the click button with the label and the id = "check" in the form with the click event.
The function checks whether the value of the input field in the form () has the value. Depending on this, either the value or is assigned to the variable. Then a corresponding sentence is put together (see also operator for string linking) and output in the output element.
You need relational operators to formulate conditions.
Differentiation of cases with "switch" 
With and you can distinguish between exactly two cases. If you want to differentiate more finely, that is, to differentiate between several cases, you can write down several if-queries one after the other, but there is a more elegant possibility: the case differentiation with "switch".
With you initiate a case distinction (switch = switch). This is followed by a variable or expression, enclosed in round brackets, for whose current value you make the case distinction. In the example this is the variable with the name. This variable is given a value before the case distinction, because a dialog window () with an input field prompts the user to enter a number between 1 and 4. The entered value is saved in.
The individual cases that you want to query are noted within curly brackets. You initiate each individual case (case). This is followed by the specification of the value that you want to check for. The instruction in the example above then means something like: 'if the input variable has the value "1"'. In the example, an individual message is output for each case.
The word at the end of each case is also important (break = abort). If you omit the word, all of the following cases will be carried out, but you usually don't want that.
In the event that none of the defined cases apply, you can define the case at the end of the case distinction. The instructions below are carried out if none of the other cases apply.
Special feature: same instructions 
Sometimes the same code should be executed for different values of the case distinction. To do this, simply put the relevant cases one below the other.
Special feature: value ranges 
If a range of values is to be checked instead of an exact value, the code must look like this:
1.) The variable whose value you want to distinguish is not written in the round brackets after the word, but in round brackets after the word.
2.) Since the listed cases always check Boolean expressions, the word must be written in the round brackets after the word, because the condition of one of these cases must be met.
Web links 
- What brands have affiliate programs
- Like many brands, Walmart owns
- What are the benefits of eating fruits
- Why are pigs pink 1
- What do you think of Bhojpuri songs
- Who Needs Bariatric Surgery
- What do public libraries offer
- What's the best WordPress magazine theme
- Adam DAngelo is a Jew
- How do people behave less intelligently?
- Why do some people call Evolution EVILution
- What is a deed of incorporation
- What is 1 divided by infinity
- How do I know if I'm anorexic?
- Can you correct this sentence 2?
- How do plants perceive their surroundings
- How good is R for data visualization
- Is Australia in the US
- Is the Bushmaster an MRAP
- How do I contact Harvard by email
- What kind of business does not exist in India?
- Who is the best footballer of all time?
- What AP class did you hate
- Are black Americans racist towards white Americans