The most interesting, but difficult, in writing a script is designing it so that it can take decision while is executed.
Using conditional instructions can make programs to test various conditions and then decide how to manipulate the datas.
if()
- use if statement to execute some code only if a specified condition is True.if() else
- it executes some code if a condition is True and another code if the condition is False.if() else if()
- it executes some code if a condition is True, or other code if that condition is False and other condition is True.switch()
- selects which piece of code to be executed.if (condition){ //Code to be executed if condition is true }- 'condition' can be any logical expresion.
<script> // if hour > 11, it writes: Hello let hour = 12; if(hour >11){ document.write('<h4>Hello</4>'); } </script>
if() else
we can set commands to be executed when the condition of the if()
is FALSE.if(condition){ // code to be executed if condition is True } else { // code rto be executed if condition is False }- 'condition' can be any logical expresion.
We use the Date
object to get current hour, will be explained in another lesson.
<script> // if 'time' < 11, prints: Good morning // Otherwise 'It's ... o`clock' var d = new Date() var time = d.getHours() if(time<11){ document.write('<h4>Good morning</h4>') } else { document.write('<h4>It`s ' +time+ ' o`clock</h4>') } </script>
else if()
instruction is added after if(), and before 'else'.if(condition_1){ //code to be executed if this condition is True } else if(condition_2){ //code to be executed if condition_1 is False and condition_2 is True } else { //codul care va fi executat cand ambele: conditie_1 si conditie_2 sunt FALSE }- 'condition_1' and 'conditie_2' can be any logical expresion.
else if()
, but if 'condition_2' is FALSE, it will be executed the code from 'else'.<script> //if the hour is between 5 and 11, it writes: Good morning //if it is between 11 and 19, it writes: Good day //otherwise: It's ... o`clock var d = new Date(); let hour = d.getHours(); if(hour >5 && hour <11){ document.write('<h4>Good morning</h4>'); } else if(hour >=11 && hour <19){ document.write('<h4>Good day</h4>'); } else { document.write('<h4>It\'s '+hour+' o`clock</h4>'); } </script>
After the if() instruction you can add several sets of else if(); and 'else' is optional.
The switch()
instruction is used to compare a value with others from a list.
switch(expresion){ case value1:- First we have a single expression between parentheses (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically.
code executed if expresion = value1 break case value2: code executed if expresion = value2 break case value3: code executed if expresion = value3 break default: code executed if expresion is other then value1, valoare2 or valoare3 }
<script> var d = new Date(); var day = d.getDay(); switch(day) { case 5: document.write('Today is friday'); case 6: document.write('Today is saturday'); case 0: document.write('Today is sunday'); default: document.write('There is a lot until saturday'); } </script>
switch()
statement compares the value of 'day' with the value of each case
. If there is a match, the block of code associated with that 'case
' is executed.case
' match the variable 'day', will execute the code associated with 'default'.<script> var nume = 'Marius'; switch (nume) { case 'Cristi': document.write('Friend'); break case 'Marius': document.write('Brother'); break case 'Maria': document.write('Sister'); break default: document.write('Somebody else'); } </script>
<input type="checkbox" name="a_name" value="value" checked="checked" />
#id { background:url("path_to_image.png"); background-size:contain; background-repeat:no-repeat; }
var rest8_7 = 8 % 7; alert(rest8_7);
$nr = ceil(3.5); echo $nr; // 4