Introduction to JavaScript Programming – part 4: Continuing from Introduction to JavaScript Programming – part 3.
In the previous part, we've discussed JavaScript Mathematics, JavaScript Assignment Operators, and JavaScript Comparison Operators. In this part, we are going to discuss Control Structures Used in JavaScript, JavaScript Logical Operators, and JavaScript Bitwise Operators.
Control Structures Used in JavaScript
if( comparison ) { statements; } if( comparison ) { statements; } else { statements; } switch( variable ) { case (value_1): statements; break; case (value_2): statements; break; case (value_n): statements; break; default: statements; break; } for(initial count; comparison; update counter) { statements; } while( comparison ) { statements; } do { statements; } while ( comparison );
We won’t go into detail on the control structures right now because here we are just introducing them and the best way to learn control structures is to use them. But one thing we should know is that the statement “break” can be used to break out of a loop, as shown below.
do { if(nPrice nHighest);
In this code, we have included an “if” statement inside the “do” loop that checks for the condition of the value nPrice being less than or equal to 0. If that condition is “true” we want to break out of the loop, because less than or equal to 0 is not a valid value for nPrice.
This example also demonstrates that control structures can be “nested”. The “if” structure is nested inside the “do / while” structure. This nesting can be done to any level, making it possible to create incredibly powerful programs.
JavaScript Logical Operators
What if you need to check the condition of more than one value in your control structure? JavaScript provides “logical operators” for that purpose. The most commonly used logical operators are && (And) and | | Or. The example below executes the phrase “f the value of nNumA or nNumB is less than nNumC, then execute the statements within the block.
if(nNumA < nNumC | | nNumB < nNumC) { statements; }
Another logical operator is (!) Not. This operator changes a true boolean value to false, or a false boolean value to true. In the code shown below, the comparison “is nNumA less than nNumB” returns “false”. But the “Not” operator changes it to “true” before returning it to the “if” control structure, so the statements within the brackets would be executed.
nNumA = 10; nNumB = 2; if !(nNumA < nNumB) { statements; }
JavaScript Bitwise Operators
JavaScript also provides bitwise operators. These operators perform logical operations at the binary bit level. Operators to shift the bits left or right within the data. Bitwise Operators Used in JavaSCript
operator | Name | Operation |
---|---|---|
& | And | Compares two bits, if both are 1, the result is 1. |
| | Or | Compares two bits, if one or the other is 1, the result is 1. |
^ | XOr | Compares two bits, if one or the other, but not both, is 1, the result is 1. |
>> | Shift left | Shifts the bits to the left. |
<< | Shift right | Shifts the bits to the right. |
We won’t go into detail on the bitwise operators right now because they are used only in very sophisticated programming. We will save learning how to do a kind of programming for the future.
This article has provided you with an overview of the popular and powerful JavaScript web programming language. Keep learning ……… 🙂