What are Flow Control Statements in JavaScript?
I am very new to the JavaScript. Before this I have done C, C++ and Core Java. Since I am new to JavaScript, I am unknown of many terms. I have recently got an assignment in which I have to explain the Flow Control Statements. Can someone explain me exactly what are Flow Control Statements in JavaScript.?? If you provide me some coding for the same, that would be much better for me to understand,.!! Help me soon as possible.!!:notworthy
Re: What are Flow Control Statements in JavaScript?
In JavaScript, you can define variables and manipulate them. A basic execution unit in JavaScript is known as Statement. The following are some basic things that gets applied to the statement :
- You must end the statement with semicolon (;).
- The statements are not ended by the line breaks. In simple words, you can say that, a single statement can be written in multiple lines, and multiple statements can be written in a single line.
- A statement can be simple expression, like a single literal, or a complex expression with assignment operators.
- The key words that mainly involved in a statements are "if", "while", "var", "break", etc.
- You can also use the multiple statements which can be grouped together to form a statement block in the format.
- You can use the comments anywhere in the statements.
Re: What are Flow Control Statements in JavaScript?
You can check the following example, so that you can easily understand how to use the Flow Control Statements in JavaScript..!! Here is the code :
HTML Code:
<html>
<head><title>Statements</title></head>
<body>
<script type="text/javascript">
var a,b,c;
a=4, b=16, c=36;
a*a; b*b; c*c;
{
a *= a;
b *= b;
c *= c;
}
if (true)
document.write("9 statements in total.");
</script>
</pre>
</body>
</html>
Re: What are Flow Control Statements in JavaScript?
I have provided you with an example of the "if" statements, which is a type of the Statement. The example is little bit long but very easy to understand. Just have a glance on the following example :
HTML Code:
<html>
<head><title>Using the If Statements</title></head>
<body>
<pre>
<script type="text/javascript">
document.write('\n');
var is_december = true;
if (is_december) document.write("Merry Christmas!\n");
document.write('\n');
var is_log_on = true;
if (is_log_on) {
document.write("Open the log file.\n");
document.write("Write the log message.\n");
document.write("Close the log file.\n");
}
document.write('\n');
var is_admin = false;
if (is_admin) {
document.write("Display the edit button.\n");
document.write("Display the add button.\n");
} else {
document.write("Display the view button.\n");
}
document.write('\n');
var week_day = 4;
var open_hours;
if (week_day == 0) {
open_hours = "closed";
} else if (week_day >= 1 && week_day <= 5) {
open_hours = "open from 9:00am to 6:00pm";
} else if (week_day == 6) {
open_hours = "open from 9:00am to 4:00pm";
} else {
open_hours = "undefined";
}
document.write("The library is " + open_hours + ".\n");
</script>
</pre>
</body>
</html>
Re: What are Flow Control Statements in JavaScript?
The "switch ... case" statements are also important in the Statements. It is very commonly used when you have to include many conditions at a time. You will get the point better, if you have a look on the following example :
HTML Code:
<html>
<head><title>Examples of Switch Case Statements</title></head>
<body>
<pre>
<script type="text/javascript">
var week_day = 4;
var day, open_hours;
switch (week_day) {
case 0:
day = "Sunday";
open_hours = "closed";
break;
case 1:......
case 2:......
case 3:.....
case 4:.....
case 5:.....
day = "a weekday";
open_hours = "open from 9:00am to 6:00pm";
break;
case 6:
day = "unknown";
open_hours = "open from 9:00am to 4:00pm";
break;
default:
day = "unknown"
open_hours = "undefined";
}
document.write('\n');
document.write("Today is " + day + ".\n");
document.write("The library is " + open_hours + ".\n");
</script>
</pre>
</body>
</html>