Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com How do you figure out how many times this for loop iterates?

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-08-2012, 05:42 AM PM User | #1 sabi New Coder   Join Date: Oct 2011 Posts: 27 Thanks: 2 Thanked 0 Times in 0 Posts How do you figure out how many times this for loop iterates? How would you figure out how many times this for loop iterates? Instead of having to trace every step, is there another way finding out how many times a nested for loop will iterate? for(row = 1; row <= 5; row++) { for(col = 1; col < row + 1; col++) { System.out.print("*"); } System.out.println() }
 03-08-2012, 05:48 AM PM User | #2 webdev1958 Banned   Join Date: Apr 2011 Posts: 656 Thanks: 14 Thanked 69 Times in 69 Posts 1) initialise a counter variable to 0 just above the FOR loop. 2) incement the counter by 1 as the first step in the for loop 3) Then when the line in the FOR loop that triggers exiting the FOR loop is executed, the counter variable in 1) will contain how many times the loop iterated before exiting. or 4) you can use the current value of col col when you exit the loop to calculate the number of iterations based on its min value and increment that you specified - just basic primary school arithmetic. Last edited by webdev1958; 03-08-2012 at 06:28 AM..
03-09-2012, 12:00 AM   PM User | #3
alykins
Senior Coder

Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
it is going to go through the first loop 5 times- the second loop is going to go through 1 -> 5 times and it will print a star pattern
Quote:
 * ** *** **** *****
I don't know why they start on 1, usually for loops are started on 0... if you ever get confused in a for loop w/ the <= crap, it is the same as the number on the right + 1 and a <... eg
Code:
```for(int i=0; i <=4; i++){
loop will hit 5 times
}
this is the same as
for(int i=0; i<5; i++){
loop will hit 5 times
}```
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE

 Users who have thanked alykins for this post: sabi (03-09-2012)

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 08:27 PM.