Skip to main content

Loops in Javascript



The do...while Loop

The do...while loop is just a variant of the while loop. In such loop the block of code is always executed atleast once. The loop will repeat as long as the specified condition is true.

Syntax:

do
{
// Enter here the code to be executed
}
while (var<=endvalue); E.g. Using do…while loop to display numbers

Output: Number:0 Number:1 Number:2 Number:3 Number:4 Note: This loop will always be executed at least once, even if the condition is false, because the code is executed before the condition is tested. 4. JavaScript Break and Continue JavaScript has two special statements that can be used inside loops: a. break: The break command will break the loop and continue executing the code that follows after the loop (if any). E.g Using break statement to come out of the for loop



Output:

The number is 0
The number is 1

b. Continue: The continue command is used in case you need to break the current loop and continue with the next one.

E.g.







Output:

Number:0
Number:1
Number:3
Number:4
Number:5
4.4.3 JavaScript Popup Boxes
In JavaScript you have the ability to pickup the user input or display small amount of text to the user using dialog boxes. There are kinds of popup boxes: Alert box, Confirm box, and Prompt box. These dialog boxes appear as separate windows and their content depend on the information that has been input by the user.
Note: The content of these dialog boxes is independent of the text in the HTML page and do not effect content of webpage in any way.
The three types of dialog boxes are:
1. Alert Box: You can use alert box if you want to make sure information comes through to the user. It is the simplest way to output small amount of textual information to browser’s window. The javaScript alert() method takes a string argument and displays an alert dialog box in the browser window when it is invoked by appropriate code in javaScript. The alert box will display string passed to the alert() method and the ‘OK’ button. User cannot proceed without clicking on the alert button.
Syntax:
alert("text message ");
E.g. Using Alert Box to message user





Output:


2. Prompt Box: You can use prompt box to input a value before entering a page. When a prompt box pops up, the user needs to click either "OK" or "Cancel" to proceed after entering an input value. In case user clicks "OK" button prompt box returns the input value where as if the user clicks "Cancel" the box will return null value.

Syntax:
prompt("text message ","defaultvalue");

E.g. Prompting user to answer a question using Prompt Box







In case user answers correct i.e. user enters 25. Following screen will be displayed on the browser:










3. Confirm Box: JavaScript provides a confirm box to prompt user for verification or confirmation before continuing further with the execution of JavaScript. When a confirm box pops up, the user will have to options either to click on "OK" or "Cancel" button to proceed. In case user clicks "OK", the box returns true and if the user clicks "Cancel", the box returns false.
Syntax:

confirm("text message");

Example:









As the browser finds confirm box following output will be generated.

Comments

Popular posts from this blog

Advantages and Disadvantages of EIS Advantages of EIS Easy for upper-level executives to use, extensive computer experience is not required in operations Provides timely delivery of company summary information Information that is provided is better understood Filters data for management Improves to tracking information Offers efficiency to decision makers Disadvantages of EIS System dependent Limited functionality, by design Information overload for some managers Benefits hard to quantify High implementation costs System may become slow, large, and hard to manage Need good internal processes for data management May lead to less reliable and less secure data

Inter-Organizational Value Chain

The value chain of   a company is part of over all value chain. The over all competitive advantage of an organization is not just dependent on the quality and efficiency of the company and quality of products but also upon the that of its suppliers and wholesalers and retailers it may use. The analysis of overall supply chain is called the value system. Different parts of the value chain 1.  Supplier     2.  Firm       3.   Channel 4 .   Buyer

Big-M Method and Two-Phase Method

Big-M Method The Big-M method of handling instances with artificial  variables is the “commonsense approach”. Essentially, the notion is to make the artificial variables, through their coefficients in the objective function, so costly or unprofitable that any feasible solution to the real problem would be preferred, unless the original instance possessed no feasible solutions at all. But this means that we need to assign, in the objective function, coefficients to the artificial variables that are either very small (maximization problem) or very large (minimization problem); whatever this value,let us call it Big M . In fact, this notion is an old trick in optimization in general; we  simply associate a penalty value with variables that we do not want to be part of an ultimate solution(unless such an outcome is unavoidable). Indeed, the penalty is so costly that unless any of the  respective variables' inclusion is warranted algorithmically, such variables will never be p