Friday, July 31, 2009

How to do Nested "For" Loops?

Can anyone give me a link, or describe to me how to do a Nested "for" Loop. For homework I have to create a nested "for" loop for a basic multiplication table and I know how to use a "for" loop just not a Nested one. So any Information would be Great!


Thx!

How to do Nested "For" Loops?
Are you using C?





for (i=1; i%26lt;=10; i++)


{


for(x=1; x%26lt;=10; x++)


{


printf("%d\t",x*i);


}


printf("\n");


}





try this i hope it works....





the "\t" gives it a TAB so you get spaces in between and have your numbers aligned.
Reply:Nested just means inside each other. So if I had to add up say a table of numbers, lets say row by row....


Lets say I had 3 rows with 4 columns


The row always represents the outside for loop and the col is the inside





So a table of 3x4


columns


row 1 2 3 4


row 5 6 7 8


row 2 3 9 5





int sum=0;


for ( int row=0; row %26lt; 3; row++)//1 row at a time


{





for ( int col=0; col %26lt; 4; col++)//1 col at a time, but the first col of 4 will go b4 the next row goes


{


sum=sum+ col;//adds first row across


}//end inner for





}//end outter for








Hope you understand...
Reply:My GOD... I thought BASIC went out with the dinosaurs.





FOR NEXT


IF THEN GOTO





both of those will create a loop
Reply:for(i = 1; i %26lt; 10; i++){


for(e = 1; e %26lt;= 15; e++){


System.out.print(i);


}


}





There's a possible example of a nested loop. Work around that :)
Reply:nested means you have several for loop together like





for(i=0;i%26lt;10;i++)


{


for(j=i;j%26lt;=i;j++)


{


statement


}


}





you can have as much as for loop you want make sure you use open and close of the loop in the right place other wise it gives you wrong answer even thought it looks ok
Reply:Here is some pseudo code;


i, j, currentSum : integer





for i = 1 to 10


for j = 1 to 10


currentSum = i * j


print i + " x " + j + " = " + currentSum


next j


next i



performing arts

No comments:

Post a Comment