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

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
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

System Analysis and Design (SAD)

Introduction to System Analysis and Design (SAD) System are created to solve Problems. One can think of the systemsapproch as an organised way of dealing with a problem. In this dynamic world , the subject system analysis and design, mainly deals with the software development activities. This post include:- What is System? What are diffrent Phases of System Development Life Cycle? What are the component of system analysis? What are the component of system designing? What is System? A collection of components that work together to realize some objectives forms a system. Basically there are three major components in every system, namely input, processing and output. In a system the different components are connected with each other and they are interdependent. For example, human body represents a complete natural system. We are also bound by many national systems such as political system, economic system, educational system and so forth. The objective of the system demands tha...