Thursday, 17 October 2013

Browser Compatibility with JavaScript

JavaScript and Browser Compatibility

(browsers that support javascript, browser compatibility code in javascript, javascript innerhtml browser compatibility, javascript events browser compatibility)

Browser compatibility has always been a major topic for JavaScript programmers. The two most popular Internet browsers since the mid-1990s have been Microsoft's Internet Explorer and Netscape's Communicator. Microsoft and Netscape conventionally have had distinct attitudes as to how browsers should work. As a outcome, Netscape Communicator versions 2, 3, and 4 presented differently in many positions than Internet Explorer versions 3, 4, and 5. HTML sheets and JavaScript’s often behaved distinctly when inquired to do the same thing on either browser.

Now a days there are many world wide web browsers available. And nearly all of them support JavaScript. But the user can disable or enable JavaScript for their convenience.

The foremost difference was in the earlier versions of the Netscape and Internet Explorer, their object forms. The browsers accumulate their functionalities and contents in the pattern of things. JavaScript interacts with browsers by interacting with these things. This difficulty was in these two browsers because they both had distinct object model, so as a outcome some things were found in Netscape and not in Internet Explorer and vice versa. However, with the arrival of the ECMA Script measures, things have become a alallotmentment more reliable in newest years. With the newest versions of both browsers, small dissimilarities in implementation still exist. The good practice to use JavaScript is to use distinct browsers to check the compatibility of the browser.

Another browser issue that JavaScript programmers still have to be worried with is what type of JavaScript the diverse browsers support. JavaScript support started with the Netscape 2 browser, but that browser supports only the primary version of JavaScript. Microsoft began to provide JavaScript support only in Internet Explorer 3. The difficulty is that there are still a lot of people out there running older versions of both browsers, and endeavoring to accommodate them all is very tough.
Not all browsers are conceived equal. In detail, things are made more tough because Microsoft and Netscape are not the only businesses that make browsers. Other browsers supply varying degrees of JavaScript support.


It is not any large-scale topic now a days because the browsers are made very effective and supportive to JavaScript.

Nested If Statement in Programming

Nested if Statement 

(nested if statement in c, nested if statement java, nested if statement example, nested if statement php, nested if statement c++ examples, nested if statement python, nested if statement in java example, nested if statement matlab)

The 'if statement' is computationally one of the most expensive statement in a program. We call it costly due to the fact that the process has to proceed through many cycles to execute an if declaration to assess a single conclusion. So to make a program more effective, try to use the smallest number of if statements. This will make the performance of the program better.

So if we have distinct conditions in which only one will be true as glimpsed in the demonstration of student’s grades, the use of if statement is very costly. To bypass this expensiveness, an alternate of multiple if declarations can be utilized that is if/else declarations. We can compose an if statement in the body of an if declaration which is known as nested if. We can write the cipher of switch statement in the following nested if/else form.
If ( grade == ‘A’ )
cout << “Excellent” ;
else if ( grade == ‘B’ )
            cout << “Very Good” ;
else if ( grade == ‘C’ )
cout << “Good” ;
else if ( grade == ‘D’ )
cout << “Poor” ;
else if ( grade == ‘F’ )
            cout << “Fail” ;
In the code, there is single statement with each ifstatement. If there are more statements with an if statement, then don’t forget the use of braces and make sure that they match (i.e. there is a corresponding closing brace for an opening brace). Proper indentation of the blocks should also be made.

Break Statement in Programming

The Break Statement 

(break statement java, break statement in c, break statement python, break statement matlab, break statement in perl, use of break statement in c language, break statement c++ tutorial, break statement in c example)

The break declaration cuts off the flow of command. We have glimpsed in switch declaration that when a true case is discovered, the flow of control moves through every declaration down ward. We desire that only the declarations of factual case should be performed and the remaining should be skipped. For this reason, we use the break declaration. We compose the shatter declaration after the declarations of a case. Therefore, when a factual case is found and its declarations are executed then the break declaration cuts off the flow of command and control leaps out of the witch statement. If we want to do the identical task for two cases, like in preceding demonstration for ‘A’ and ‘a’, then we don't put break statement after the first case. We write both the situations ( or the situations may be more than two) line by line then write widespread statements to be executed for these cases. We write the shatter declaration after these widespread declarations. We should use the break statement is necessary after the declarations of each case. The shatter declaration is necessary in swap structure, without it the swap structure becomes illogical.  As without it all the declaration will execute after first agree case is found.

The break statement is also used in decision structures other than switch structure. We have seen that in while, do-while and for loops, we have to violate some condition explicitly to terminate the loop before its complete repetitions. As in a program for guessing a character, we make a variable myNum greater than 5 to violate the while condition and exit the loop if the correct character is guessed before five tries. In these loops, we can use the break statement to exit a loop. When a break statement is encountered in a loop, the loop terminates immediately. The control exits the inner most loop if there are nested loops. The control passes to the statement after the loop. In the guessing character example, we want that if the character is guessed in first or second attempt, then we print the message ‘Congratulations, You guess is correct’ and exit the loop. We can do this by using a break statement with an if statement. If the character is guessed, we print the message. Afterwards, the break statement is executed and the loop terminates. So we can write this as follows.

if ( c == ‘z’ ) // c is input from user
{
cout << “Great, Your guess is correct” ; break;
}
Thus, break statement can be used to jump out of a loop very quickly.

Switch Statement in Programming

Switch Statement and its Use

(switch statement c programming, switch statement c++, switch statement tutorial, switch statement example, switch statement syntax, switch statement as3, switch statement java example, switch statement in c language)



Switch Statement
Sometimes, we have multiple conditions and take some action according to each condition. For example, in the payroll of a company, there are many conditions to deduct tax from the salary of an employee. If the salary is less than Rs. 10000, there is no deduction. But if it falls in the slab Rs. 10000 – 20000, then the income tax is deducted. If it exceeds the limit of Rs. 20000, some additional tax will be deducted. So the appropriate deduction is made according to the category or slab of the salary. We can also understand this from the example of grades of a student. If the student has grade ‘A’ we print ‘Excellent’ and 'Very good', 'good', 'poor' and 'fail' for grades B, C, D, and F respectively. Now we have to see how this multi-condition situation can be applied in a program. We have a tool for decision making i.e. 'if statement'. We can use 'if statement' to decide what description for a grade should be displayed. So we check the grade in if statement and display the appropriate description. We have five categories of grades-- A, B, C, D, and F. We have to write five if statements to check all the five possibilities (probabilities) of grade. So we write this in our program as under-
if ( grade == ‘A’ )
cout << “Excellent” ;
if ( grade == ‘B’ )
cout << “Very Good” ;
 if ( grade == ‘C’ )
cout << “Good” ;
if ( grade == ‘D’ )
cout << “Poor” ;
if ( grade == ‘F’ )
 cout << “Fail” ;
These statements are correct and perform the required task. In the following code we are using nested if statement.
If ( grade == ‘A’ )
cout << “Excellent” ;
else if ( grade == ‘B’ )
            cout << “Very Good” ;
else if ( grade == ‘C’ )
cout << “Good” ;
else if ( grade == ‘D’ )
cout << “Poor” ;
else if ( grade == ‘F’ )
            cout << “Fail” ;
In the above example, we see that there are two approaches for a multi way decision. In the first approach, we use as many if statements as needed. This is an expensive approach. The second is the use of nested if statements. The second is little more-efficient than the first one. In the 'nested if statements' the nested else is not executed if the first if condition is true and the control goes out of the if block. The C language provides us a stand-alone construct to handle these instances. This construct is switch structure. The switch structure is a multiple-selection construct that is used in such cases (multi way decisions) to make the code more efficient and easy to read and understand. The syntax of switch statement is as follows.
switch ( variable/expression )
{
case constant1 : statementLlist1 ;
case constant2 : statementLlist2 ;
case constantN : statementListN ;
default : statementList ;
}
In the switch statement, there should be an integer variable (also include char) or an expression which must evaluate an integer type (whole numbers only, the decimal numbers 2.5, 14.3 etc are not allowed). We can’t use compound conditions (i.e. the conditions that use logical operators && or ||) in switch statement and in case statements. The constants also must be integer constants (which include char). We can’t use a variable name with the case key word. The default statement is optional. If there is no case which matches the value of the switch statement, then the statements of default are executed. The switch statement takes the value of the variable, if there is an expression then it evaluates the expression and after that looks for its value among the case constants. If the value is found among the constants listed in cases, the statements in that statementList are executed. Otherwise, it does nothing. However if there is a default (which is optional), the statements of default are executed.
Thus our previous grade example will be written in switch statement as below.
switch ( grade )
{
 case ‘A’ : cout << “Excellent” ;
case ‘B’ : cout << “Very Good” ;
case ‘C’ : cout << “Good” ;
case ‘D’ : cout << “Poor” ;
case ‘F’ : cout << “Fail” ;
}
We know that C language is 'case sensitive'. In this language, ‘A’ is different from ‘a’. Every character has a numeric value which is stored by the computer.. The numeric value of a character is known as ASCII code of the character. The ASCII code of small letters (a, b, c etc ) are different from ASCII code of capital letters (A, B, C etc). We can use characters in switch statement as the characters are represented as whole numbers inside the computers. Now we will see how the use of ' the letter a' instead of 'A' can affect our program. We want our program to be user- friendly. We don’t want to restrict the user to enter the grade in capital letters only. So we have to handle both small and capital letters in our program. Here comes the limitations of switch statement. We can’t say in our statement like case ‘A’ or ‘a’ : statements ; We have to make two separate cases so we write
 case ‘A” :
 case ‘a’ :
statements;
In the switch statement, the cases fall through the case which is true. All the statements after that case will be executed right down to the end of the switch statement. This is very important to understand it. Let's suppose that the user enters grade ‘B’. Now the case ‘A’ is skipped. Next case ‘B’ matches and statement cout << “Very Good” ; is executed. After that, all the statements will be executed. So cout << “Good” ; cout << “Poor” ;and cout << “Fail” ; will be executed after one another. We don’t want this to happen. We want that when a case matches, then after executing its statement, the control should jump out of switch statement leaving the other cases. For this purpose we use key work break.


Increment and Decrement Operators in Programming

Increment decrement operator in c
(increment decrement operator overloading c++, difference between increment and decrement operators, increment and decrement operators in java, increment and decrement operators in c, decrement operator in c)



We have seen that in while, do-while and for loop we write a statement to increase the value of a variable. For example, we used the statements like counter = counter + 1; which adds 1 to the variable counter. This increment statement is so common that it is used almost in every repetition structure (i.e. in while, do-while and for loop). The C language provides a unary operator that increases the value of its operator by 1. This operator is called increment operator and sign ++ is used for this. The statement counter = counter + 1; can be replaced with the statement
counter ++ ;
 The statement counter++ adds 1 to the variable counter. Similarly the expressions i = i + 1 ; and j = j + 1 ; are equivalent to i++ ; and j++; respectively. There is also an operator -- called decrement operator. This operator decrements, the value of its operand by 1. So the statements counter = counter - 1; and j = j - 1; are equivalent to counter--; and j--; respectively.

The increment operator is further categorized as pre-increment and post-increment. Similarly, the decrement operator, as pre-decrement and post-decrement. In pre-increment, we write the sign before the operand like ++j while in post-increment, the sign ++ is used after the operand like j++. If we are using only variable increment, pre or post increment does not matter. In this case, j++ is equivalent to ++j. The difference of pre and post increment matters when the variable is used in an expression where it is evaluated to assign a value to another variable. If we use pre-increment (++j ), the value of j is first increased by 1. This new value is used in the expression. If we use post increment ( j++ ), the value of j is used in the expression. After that it is increased by 1. Same is the case in pre and post decrement.

The operators ++ and -- are used to increment or decrement the variable by 1. There may be cases when we are incrementing or decrementing the value of a variable by a number other than 1. For example, we write counter = counter + 5; or j = j – 4;. Such assignments are very common in loops, so C provides operators to perform this task in short. These operators do two things they perform an action (addition, subtraction etc) and do some assignment. These operators are +=, -=, *=, /= and %=. These operators are compound assignment operators. These operators assign a value to the left hand variable after performing an action (i.e. +, -, *, / and %). The use of these operators is explained by the following examples.

Let’s say we have an expression, counter = counter + 5;. The equivalent of this expression is counter += 5;. The statement counter += 5; does two tasks. At first, it adds 5 to the value of counter and then assigns this result to counter. Similarly the  following expressions

x = x + 9;
x = x - 6 ;
 x = x * 8 ;
x = x / 4 ;
 x = x % 2;

can be written in equivalent short statements using the operators ( +=, -=, *=, /=, %= ) as follows:

x += 9;
x -=  6 ;
 x *= 8 ;
x /= 4 ;
 x %= 2;


Note that there is no space between these operators. These are treated as single signs. Be careful about the operator %=. This operator assigns the remaider to the variable. These operators are alternate in short hand for an assignment statement. The use of these operators is not necessary. A programmer may use these or not. It is a matter of style.

For Loop | For Loop in Programming

for loop c++,  for loop java, for loop vb, for loop matlabfor loop php
for loop vba, for loop oracle
Let’s see what we do in a loop. In a loop, we initialize variable(s) at first. Then we set a condition for the continuation/termination of the loop. To meet the condition to terminate the loop, we affect the condition in the body of the loop. If there is a variable in the condition, the value of that variable is changed within the body of the loop. If the value of the variable is not changed, then the condition of termination of the loop will not meet and loop will become an infinite one. So there are three things in a loop structure i.e. (i) initialization, (ii) a continuation/termination condition and (iii) changing the value of the condition variable, usually the increment of the variable value.
To implement these things, C provides a loop structure known as for loop. This is the most often used structure to perform repetition tasks for a known number of repetitions. The syntax of for loop is given below.
for ( initialization condition ; continuation condition ; incrementing condition )
{
            Statement(s);
}
We see that a ‘for statement’ consists of three parts. In initialization condition, we initialize some variable while in continuation condition, we set a condition for the continuation of the loop. In third part, we increment the value of the variable for which the termination condition is set.

Let’s suppose, we have a variable counter of type int. We write for loop in our program as

for ( counter = 0; counter < 10 ; counter = counter + 1 )
            {
                        cout << counter <<endl;
            }

This ‘for loop’ will print on the screen 0, 1, 2, …, 9 on separate lines ( as use endl in our cout statement). In for loop, at first, we initialize the variable counter to 0. And in the termination condition, we write counter < 10. This means that the loop will continue till value of counter is less than 10. In other words, the loop will terminate statement, we write counter = counter + 1 this means that we add 1 to the existing value of counter. We call it incrementing the variable.

Infinite Loop Problem C++ and in All Programming Langauges

c++ stream infinite loop, infinite loop problem c++, c++ infinite loop,  cin
make infinite loop c++, infinite while loop c++, how to stop an infinite loop in c++, 
c++ cin infinite loop
Consider the condition in the while structure that is (number<=upperLimit) and in the while block the value of number is changing (number = number + 1) to ensure that the condition is tested again next time. If it is true, the while block is executed and so on. So in the while block statements, the variable used in condition must change its value so that we have some definite number of repetitions. What will happen if we do not write the statement number = number + 1; in our program? The value of number will not change, so the condition in the while loop will be true always and the loop will be executed forever. Such loops in which the condition is always true are known as infinite loops as there are infinite repetitions in it.
Tips:
Always use the self explanatory variable names
Practice a lot. Practice makes a man perfect.
While loop may execute zero or more times.

Make sure that loop test (condition) has an acceptable exit.