Java Tutorials
Java Swing Tutorial: How to Create a GUI in Java with Examples
What is Swing in Java? Swing in Java is a Graphical User Interface (GUI) toolkit that includes the GUI...
In this tutorial, we will learn-
Loops are useful when you have to execute the same lines of code repeatedly, for a specific number of times or as long as a specific condition is true. Suppose you want to type a ‘Hello’ message 100 times in your webpage. Of course, you will have to copy and paste the same line 100 times. Instead, if you use loops, you can complete this task in just 3 or 4 lines.
Syntax:
for(statement1; statement2; statment3)
{
lines of code to be executed
}Try this yourself:
<html>
<head>
<script type="text/javascript">
var students = new Array("John", "Ann", "Aaron", "Edwin", "Elizabeth");
document.write("<b>Using for loops </b><br />");
for (i=0;i<students.length;i++)
{
document.write(students[i] + "<br />");
}
</script>
</head>
<body>
</body>
</html>Syntax:
while(condition)
{
lines of code to be executed
}The “while loop” is executed as long as the specified condition is true. Inside the while loop, you should include the statement that will end the loop at some point of time. Otherwise, your loop will never end and your browser may crash.
Try this yourself:
<html>
<head>
<script type="text/javascript">
document.write("<b>Using while loops </b><br />");
var i = 0, j = 1, k;
document.write("Fibonacci series less than 40<br />");
while(i<40)
{
document.write(i + "<br />");
k = i+j;
i = j;
j = k;
}
</script>
</head>
<body>
</body>
</html>Syntax:
do
{
block of code to be executed
} while (condition)The do…while loop is very similar to while loop. The only difference is that in do…while loop, the block of code gets executed once even before checking the condition.
Try this yourself:
<html>
<head>
<script type="text/javascript">
document.write("<b>Using do...while loops </b><br />");
var i = 2;
document.write("Even numbers less than 20<br />");
do
{
document.write(i + "<br />");
i = i + 2;
}while(i<20)
</script>
</head>
<body>
</body>
</html>
What is Swing in Java? Swing in Java is a Graphical User Interface (GUI) toolkit that includes the GUI...
What is Abstraction in OOP? Abstraction is the concept of object-oriented programming that "shows"...
Following is a step by step guide to install Java on Linux. In this training, we will install Java on...
What is String "Length" Method in Java? This function is used to get the length of string in Java....
Following is a step by step guide to download and install Eclipse IDE: Step 1) Installing Eclipse Open...
What is JDK? JDK is a software development environment used for making applets and Java...