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

Recommended Answers

All 9 Replies

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.

eg:-

 int[] numbers = 
         {1,2,3,4,5,6,7,8,9,10};
     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:
1
22
333
4444
55555
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
                 { 
                   System.out.print(item);
                   //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.
    //System.out.print(count);
    }
public static void main(String []argd)
{
    abcd ad= new abcd();
    long time1= System.nanoTime();
    ad.show();
    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.

read my remarks, it shows why you're not really helping.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.