I tried to understand for loops but still not getting it....? please help me with its dry run
i know its syntax etc but still not getting it
i know it is used for multiple execution...until condition is satisfied
please help me understanding in making patterns

4 Years
Discussion Span
Last Post by csk19

Syntax of for loop:-

for(variable initialization;condition_check;increment/deccrement){
//set  of statements to be executed

So,first of all you will set initial value to start looping,then condition is checked,if the condition returns true then the statement written inside curly braces is executed and finally increment/decrement of variable takes place.

Then condition is checked if true agian block of statements are executed.This step continues until condition_check returns false.

There is another for loop called enhanced for loop.


 int[] numbers = 
     for (int item : numbers) {
         System.out.println("Count is: " + item); 

In above example the loop continues upto the count of elements of arrays.

Click http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html to learn more.


or, the 'enhanced' for:

for ( Type elementName: arrayOrCollectionName){


but so far, what have you found online, in your textbooks, ...
this is very basic stuff, finding out about it should be extremely easy for anyone.


i want dry run only for "paterns" like:
well i kno its programing syntax etc...but not understanding how is it functioned... sir said that it executes 15 times but how??? please help in understanding


"dry run" hasn't got much to do with for loops.
every line of that is one iteration, within that iteration, there's another one, printing the number the same number of times.

public class abcd
    int count=1;
    public void show()
         int[] numbers ={1,2,3,4,5};

             for (int item : numbers)

                 for(int initem=1;initem<=item;initem++)//This loop is responsible for printing values multiple times
                   //count++;//set counter here you can see how much time it is executed.
            System.out.println();//For new line
    //Then print the counter to check how many times it get executed. you will have you answer.
public static void main(String []argd)
    abcd ad= new abcd();
    long time1= System.nanoTime();
    long time2 = System.nanoTime();
    long timeSpent = time2-time1;
    System.out.println("Time taken by the program in nenosec"+" "+timeSpent);


csk: a few things I notice:
1. the timeSpent has nothing to do with the question, trying to show off doesn't make you the better developer
2. try to uphold Java naming conventions
3. don't just hand out code. they won't learn anything by that.


Why do I will show off here...M i getting anything from this? I just tried to help him by my way.


I have read these before and I think I am not familiar with the thread and the remarks you have made on the code. And thanks for these remarks it won't happen again. All I wanted is to give an exercise.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.