Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Comparison operators, and logical values

Comparison and Logical operators are used to test for true or false.

Comparison Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values. Given that x=5, the table below explains the comparison operators:

Operator Description Comparing Returns
== is equal to x==8 false
x==5 true
=== is exactly equal to (value and type) x==="5" false
x===5 true
!= is not equal x!=8 true
!== is not equal (neither value nor type) x!=="5" true
x!==5 false
> is greater than x>8 false
< is less than x<8 true
>= is greater than or equal to x>=8 false
<= is less than or equal to x<=8 true

Logical Operators : Logical operators are used to determine the logic between variables or values. Given that x=6 and y=3, the table below explains the logical operators:

Operator Description Example
&& and (x < 10 && y > 1) is true
|| or (x==5 || y==5) is false
! not !(x==y) is true

And the logical values connected ofc with Boolean Data type.

Boolean Data Type- E.g: true
It can have logical values true or false.
Variable type Boolean should not have any quotes.

i.e. "true" is not equal to true, "true" will be considered as a string.

 

var a = true;
var b = false;


Boolean variable values are mostly used along with "if", "if else" statements and "while" loops.

Example Code:
<script language="javascript">
var a = true;
if(a == true)
{document.write("this is true");}
</script>
Result:
this is true

 

 

Bitwise Operators.

Bitwise operators treat their operands as a sequence of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octalnumbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.

The following table summarizes JavaScript's bitwise operators:

Operator Usage Description
Bitwise AND a & b Returns a one in each bit position for which the corresponding bits of both operands are ones.
Bitwise OR a | b Returns a one in each bit position for which the corresponding bits of either or both operands are ones.
Bitwise XOR a ^ b Returns a one in each bit position for which the corresponding bits of either but not both operands are ones.
Bitwise NOT ~ a Inverts the bits of its operand.
Left shift a << b Shifts a in binary representation b (< 32) bits to the left, shifting in zeros from the right.
Sign-propagating right shift a >> b Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off.
Zero-fill right shift a >>> b Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off, and shifting in zeros from the left.



35. Conditionals: if, '?'

Conditional statements are used to perform different actions based on different conditions.

Very often when you write code, you want to perform different actions for different decisions.

You can use conditional statements in your code to do this.

if statement - use this statement to execute some code only if a specified condition is true

Use the if statement to execute some code only if a specified condition is true.

Syntax

if (condition) code to be executed if condition is true;

The following example will output "Have a nice weekend!" if the current day is Friday:

<html>
<body>

<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>

</body>
</html>

Notice that there is no ..else.. in this syntax. The code is executed only if the specified condition is true.

Logical operators.

The logical operation - in programming operation on a logical expressions (a Boolean) type, corresponding to some operations on the statements of the algebra of logic. As and expressions, logical expressions can take one of two veritable values are «true» or «false». Logical operations are used for production of complex logical expressions from more simple. In turn, the logical expressions are typically used as a condition for the sequence control program execution.
Logical operators are used to determine the logic between variables or values. Given that x=6 and y=3, the table below explains the logical operators:

Operator Description Example
&& and (x < 10 && y > 1) is true
|| or (x==5 || y==5) is false
! not !(x==y) is true


Among the logical operation of the most well-known of the conjunction (&&), disjunction (||), negation (!). They are often confused with bit operations, although these are two different things. For example, the following code in the C language:

The logical operation And (conjunction, the operation of the logical multiplication). The action, which is determined by the operation And will, if fulfilled all affecting him factors (conditions).

Action associated with the operation «And» can be written as follows: X = AB = A*B = A ^ B

The logical operation OR (disjunction of the operation, the logical addition). The action, which is determined by the operation OR will happen, if executed at least one (any), which determine its condition.

Action associated with the operation OR can be written as follows: X = A + B = A v B

 

The logical operation of the Exclusive OR. Operation Exclusive OR carries out the summation of the modulo two i.e. without account transfer in the senior category. Action associated with the operation Exclusive OR can be written as follows: X = A B.

Operation identity. Operation identity determines the identity of the arguments.

Action associated with the operation of identity can be written as follows: X = A B.

 

Cycles while, for

While loop
The while loop executes a block of code while a condition is true.

Syntax

while (condition)
{code to be executed;}

Example

The example below first sets a variable i to 1 ($i=1;).

Then, the while loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:

<html>
<body>
<?php
$i=1; while($i<=5)
{ echo "The number is " . $i . "<br>";
$i++; } ?>
</body> </html>

Output:


The number is 1
The number is 2
The number is 3
The number is 4
The number is 5



For loop

The for loop is often the tool you will use when you want to create a loop.

The for loop has the following syntax:

for (statement 1; statement 2; statement 3)
{ the code block to be executed }

Statement 1 is executed before the loop (the code block) starts.

Statement 2 defines the condition for running the loop (the code block).

Statement 3 is executed each time after the loop (the code block) has been executed.

Example

for (var i=0; i<5; i++)
{ x=x + "The number is " + i + "<br>"; }


Date: 2016-01-03; view: 908


<== previous page | next page ==>
The box model inCSS | Directive break and continue
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)