for loop

for loop

what is for loop

View Answers

December 11, 2011 at 3:58 PM

Loops are used for iteration which means to execute a part of code repeatedly. Repetition of a part of code can be done for a number of times or infinitely. If repetition of code is done for a number of times it is finite loop and if repetition is done infinitely it is infinite loop. Java provides several loop structures for iteration. Those are for, while, do...while etc. Let us discuss "for" loop. "for" loop provides convenience to give declaration of a variable, initiation of a variable, increment the variable and check for condition for the program control to come out of the loop in one line. The syntax of "for" loop is as follows:-

for(initialization;condition;increment)

{

//statements

}

The "for" loop works as follows- when the program control enters the loop it first does the initialization part and then checks the condition part, if the condition returned true it executes the statements given inside the loop and then goes to increment part and increments the initialized variable. Then again it checks the condition and goes into the loop if the condition returned true. It continues this process until condition is returned false.

Note:- The initialization part is done only once and the program control will never return to the initialization part of the "for" loop when once it is done.

Let us take an example class Tick which implements for loop:-

class Tick

{ public static void main(String args[])

{

int i;

for(i=0;i<10;i++)

{

System.out.println("Tick" + i);

}

}

Here the loop is executed 10 times from 0 to 10. At first when control enters the loop the variable 'i' is initialized to 0 and checks for the condition whether 0 is less than 10 which returns true and it prints Tick 0 for the first time. Then the value of 'i' is incremented to 1 and again condition is checked whether 1 is less than 10 which returns true and it prints Tick 1. This continues till 'i' becomes 10. When 'i' becomes 10, it checks the condition whether 10 is less than 10 which returns false and then control comes out of the "for" loop and the above program ends.

The declaration of 'i' can also be done inside the "for" loop which is as follows:-

class Tick

{ public static void main(String args[])

{

for(int i=0;i<10;i++)

{

System.out.println("Tick" + i);

}

}

Ads









Related Tutorials/Questions & Answers:
loop
loop  what is the difference b/w do while,while and for loop..with example
loop
loop  i want to write my name(inder) through loop in java using star
Advertisements
for loop
for loop  what is for loop   Loops are used for iteration... for a number of times it is finite loop and if repetition is done infinitely it is infinite loop. Java provides several loop structures for iteration. Those
For loop
For loop  Hi! Please explain the following loop: for(i=0;i<a[j];i++) THANX!!   Please reply as soon as possible
for loop
for loop  how to performs the for loop when 2 inputs given like displays the triangle shape and how to take initial values or declaration values plz explain in detailed ex. 1 12 123 1234
For Loop
For Loop  Write a program using a for loop that computes the following sum: 1/2 + 2/2 + 3/2 + 4/2 + 5/2 + ... N/2 N will be an integer limit the user enters. This is an example of my upcoming quiz. I need help on knowing
loop
loop  strong textHello can someone help me with this problem,I need to output the following code using a loop the code is as follows with the user inputs in inverted commas,thanks. ENTER CODE(XX to Stop) CODE:"D24" QUANTITY:"2
for loop
for loop  using only two for loop how can print below numbers 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5
for loop
for loop  using only two for loop how can print below numbers 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5
Limits of "For loop"
Limits of "For loop"  Hi, How can I check the upper limit and lower limit of 'FOR' loop during unit testing using Junit. Thanks & Regards Ankit
How to write a loop and a while loop
How to write a loop and a while loop  How do I write a 1 loop and a 1 while loop for the example code: public boolean isTheFirstOneBigger (int num1, int num2) { if (num1 > num2) { return true
loop statements
loop statements  write a program that reads from input an interger n and prints the factorial of all numbers between 1 and n
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
loop example
loop example   1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 can you give me the code to get the number pyramid
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
For Loop/PHP
For Loop/PHP  Write a class called math. It is to have one property... 12345. How would you write this as a loop? Would I need to use the for loop or do/while loop?   PHP Factorial Example: <?php $n = 5; $i = 1; $f
loop program
loop program  please tell me the code for the output 1 2 3foo 4 5bar 6foo 7baz 8 9foo 10 11 12foo 13 14baz 15foo bar 16 17 18foo . .......till 50
For loop java
For loop java  1 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 3 4 5 4 5 6 7 5 6 7 8 9 6 7 8 9 10
for loop in java
for loop in java  1 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 3 4 5 4 5 6 7 5 6 7 8 9 6 7 8 9 10 using for loop in java   Java Number Pattern 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 public class PatternExample{ public static void main
for loop
for loop
using for loop in javascript
using for loop in javascript  How to use for loop in JavaScript
adding loop
adding loop  Hi I have a program that is not compiling when I add a loop can you help me?The program below is compiling without the loop. >... the loop is: > Blockquote import java.util.*; class Output { public
loop, control statements
loop, control statements  a java program to print
loop, control structures
loop, control structures  a java program to print
While loop Statement.
While loop Statement.   How to Print Table In java using While Loop
ModuleNotFoundError: No module named 'for_loop'
ModuleNotFoundError: No module named 'for_loop'  Hi, My Python... 'for_loop' How to remove the ModuleNotFoundError: No module named 'for_loop... to install padas library. You can install for_loop python with following
ModuleNotFoundError: No module named 'loop'
ModuleNotFoundError: No module named 'loop'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'loop' How to remove the ModuleNotFoundError: No module named 'loop' error

Ads