In this article we will learn about for loop in Java.
The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
Here, i=1 is an initialization from where the for loop starts. i<=5 condition where for loop ends. i++ is incrementation which increment the i variable in each iteration.
Remember that in this for loop variable i is accessible only through this for loop. Outside of for loop variable i is not accessible.
2. for each loop
Syntax
for(Type var:array){
//code to be executed
}
Program
publicclass foreach
{
publicstaticvoid main(String args[])
{
int arr[] = {11, 22, 33, 44, 55};
for(int i : arr)
{
System.out.println(i);
}
}
}
Explanation
In this for each we can say variable i as container for array element. In every iteration of loop index of array is incremented and that element is stored in container.
3. Labbled for loop
Syntax
labelname:
for(initialization;condition;incr/decr){
//code to be executed
}
Program
publicclass labled_for
{
publicstaticvoid main(String args[])
{
iloop:
for(int i=1; i<=5; i++)
{
jloop:
for(int j=1; j<=5; j++)
{
if(j==3) break jloop;
System.out.println(i + "-" + j);
}
}
}
}
Explanation
Here, we named for loop with variable i as iloop and for loop with variable j as jloop. When condition i==3 becomes true iloop will break while jloop continue to iterate.
Infinite for loop
If you use two semicolons ;; in the for loop, it will be infinitive for loop.
In this article we view three key (and interrelated) topics that make it all possible: the CLR, CTS, and CLS . Common Language Runtime From a programmer’s point of view, .NET can be understood as a runtime environment and a comprehensive base class library. The runtime layer is properly referred to as the Common Language Runtime , or CLR. The primary role of the CLR is to locate, load, and manage .NET objects on your behalf. The CLR also takes care of a number of low-level details such as memory management, application hosting, coordinating threads, and performing basic security checks (among other low-level details). Common Type System Another building block of the .NET platform is the Common Type System , or CTS. The CTS specification fully describes all possible data types and all programming constructs supported by the runtime, specifies how these entities can interact with each other, and details how they are represented in the .NET metadata format. Common Languag...
With the first introduction post of C# you can understand the return value of the Main() method, let’s examine the incoming array of string data. Assume that you now want to update your application to process any possible command-line parameters. One way to do so is using a C# for loop. We will see for loop in C# in later article. Accessing command line arguments with for loop Program using System; public class CLA { public static void Main(string[] args) { //this loop prints any number of given command line arguments for(int i=0; i<args.Length; i++) { Console.WriteLine("arg-" + i + " = " + args[i]); } } } Output F:\Code\Algorithm>csc CLA.cs F:\Code\Algorithm>CLA tangled java elaborately arg-0 = tangled arg-1 = java arg-2 = elaborately Explanation Here, you are checking to see whether the array of strings contains some number of items using the Length property of Sys...
While loop in JAVA Syntax while (condition){ //code to be executed } Program public class while_loop { public static void main( String args[]) { int i = 1 ; while (i <= 10 ) { System.out.println(i); i++; } } } Run Output 1 2 3 4 5 6 7 8 9 10 Explanation The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop. While loop starts with the checking of condition. If it evaluated to true, then the loop body statements are executed otherwise first statement following the loop is executed. For this reason it is also called Entry control loop. Once the condition is evaluated to true, the statements in the loop body are executed. Normally the statements contain an update value for the variable being processed for the next iteration. When...
Comments
Post a Comment