Logical Operators

o       In our discussion of conditionals, we saw how to check the veracity of a single condition via a comparator:

 if ( x > some_value )

{

  …expressions… }

o       We have also seen the limitations of such an approach. Let us say we wanted to discover if lay between two values, say val_1 and val_2. There are a number of ways we could achieve this. In our example on student grades, we learned that we could use an if…else pair to achieve this effect:

    if ( x > val_1 )

  {

    …do something…

  } else

    if ( x > val_2 )

 {

   …do something else… }

o       The above code achieves what we want – for the second branch, x must lie between val_2 and val_1 (assuming val_1 is greater than val_2, of course). However, it’s rather unwieldy, and does not scale elegantly to checking three conditions (say we wanted to check if x was an even number as well), or in fact to ten conditions.

o       Enter Logical Operators. These operators are used to join together conditional checks and return true or false depending on whether all or any of the checks are true.

o      In English, we refer to these operators by using the words “AND” and “OR”.

o       For example, say we wanted to do something each Tuesday at 8pm. We would want to check whether the current day was Tuesday, and whether the time was 8pm.

o       Another example: Let’s say we wanted to do something on the first Tuesday of each month, and also on the 3rd of the month as well. We would have to check whether the current day was the first Tuesday of the month, or whether it was the 3rd day of the month. 

Note in the last example, if both conditions were true, then we would be on Tuesday the 3rd and would perform the action. In other words, an or condition allows for either one, or the other, or bothconditions to be true.

o      In JavaScript, we use the following syntax to check multiple conditions:

       ( 100 > 10 && 5 < 8 )

translates as “if 100 is greater than 10 and 5 is less than 8”. In this case, the result is true.

       ( 100 > 200 && 4 < 9 )

in this case, the result is false. Note here that only the first condition is actually checked. Since and requires both comparisons to be true, as soon as it finds a false one it stops checking. This can be useful.

       ( 100 > 10 || 9 < 8 )

translates as “if 100 is greater than 10 or 9 is less than 8”. In this case, the result is true, since at least one of the conditions is met.

       ( 100 > 200 || 4 > 9 )

in this case, the result is false since neither of the comparisons are true. Finally:

       ( 100 > 200 || 5 < 2 || 3 > 2 )

in this case, the result is true. Any one of the three being true will provide this result.

o     As we can see from the last example, this method of checking scales to any number of conditions. We can also mix and match the operators. For example:

               ( ( 100 > 200 && 100 > 300 ) || 100 > 2 )

in this case, the and condition evaluates to false, but since either that or the last condition has to be true to return true, the overall condition returns true as 100 is indeed greater than 2.

o      This sort of complex logic can take a while to comprehend, and will not form a set part of the course. However, it is useful to be aware of it.

Project

o   Open your previous project file, and save it under the name chapter_20.html. o Clear all JavaScript code from your script tags.

o    Ensure that you have a script element in the head area of your document, and one in the body area.

o      Copy the file available_plugins.js from the network drive (your tutor will demonstrate this), and open it using NotePad’s File > Open command.

o       Copy and paste the entire contents of available_plugins.js into your current project file, into the script element in the head area of your page.

o      Have a read through the code. Note that it defines a large, two dimensional array. The array has a list of various components that can be present in web browsers (such as Flash or Quicktime)

o      Add a function to the head area script element, called flash_exists(). This function should use a for loop to check each of the elements of the available_plugins array and establish if Flash is present.

o     Add a further function to the head area script element, called quicktime_exists(). This function should also use a for loop to check each element of the array, this time returning true if Quicktime is present. o Finally, add a function to the head area script element called both_quicktime_and_flash_exist(). This function should call both of the previous functions, store their results in a variable, and produce an alert box containing the message: o “Both Quicktime and Flash” if both functions returned true; or:

o   “One of Quicktime or Flash is missing” if either of the functions return false.

       Call the final function from the body area script element.

o  Check your results in your browser.

Read More

Operators

 22 Aug Admin

C operators are symbols that are used to perform mathematical or logical manipulations. The C programming language is rich with built-in operators. Operators take part in a program for manipulating data and variables and form a part of the mathematical or logical expressions.

Types of operators:-

C programming language offers various types of operators having different functioning capabilities.

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Increment and Decrement Operators
  6. Conditional Operator
  7. Bitwise Operators
  8. Special Operators

Arithmetic Operators:-

C supports all the basic arithmetic operators. The following table shows all the basic arithmetic operators.

OperatorDescription
+adds two operands
subtract second operands from first
*multiply two operand
/divide numerator by denominator
%remainder of division
++Increment operator – increases integer value by one
Decrement operator – decreases integer value by one

Relational operators:-

The following table shows all relation operators supported by C.

OperatorDescription
==Check if two operand are equal
!=Check if two operand are not equal.
>Check if operand on the left is greater than operand on the right
<Check operand on the left is smaller than right operand
>=check left operand is greater than or equal to right operand
<=Check if operand on left is smaller than or equal to right operand

Logical operators:-

C language supports following 3 logical operators. Suppose a = 1 and b = 0,

OperatorDescriptionExample
&&Logical AND(a && b) is false
||Logical OR(a || b) is true
!Logical NOT(!a) is false

Bitwise operators:-

Bitwise operators perform manipulations of data at bit level. These operators also perform shifting of bits from right to left. Bitwise operators are not applied to float or double(These are datatypes, we will learn about them in the next tutorial).

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
<<left shift
>>right shift

Assignment Operators:-

Assignment operators supported by C language are as follows.

OperatorDescriptionExample
=assigns values from right side operands to left side operanda=b
+=adds right operand to the left operand and assign the result to lefta+=b is same as a=a+b
-=subtracts right operand from the left operand and assign the result to left operanda-=b is same as a=a-b
*=mutiply left operand with the right operand and assign the result to left operanda*=b is same as a=a*b
/=divides left operand with the right operand and assign the result to left operanda/=b is same as a=a/b
%=calculate modulus using two operands and assign the result to left operanda%=b is same as a=a%b

Conditional operator:-

The Conditional Operator in C, also called a Ternary operator, is one of the Operators, which used in the decision-making process. The C Programming Conditional Operator returns the statement depends upon the given expression result.

Example:-

Test_expression ? statement1: statement2

Special operator:-

OperatorDescriptionExample
sizeofReturns the size of an variablesizeof(x) return size of the variable x
&Returns the address of an variable&x ; return address of the variable x
*Pointer to a variable*x ; will be pointer to a variable x
Read More