Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 10-08-2003, 10:59 PM   #1 (permalink)
Stupidnewb
Registered User
 
Stupidnewb's Avatar
 
Join Date: Oct 2003
Posts: 6
Stupidnewb is on a distinguished road
Hi Problems with figuring out while loops

Hi all I just started writing my first while/for/do loop and I have this assignment to calculate a user input into printing out a value. I know that seems uber vague so here is a better description:


The user inputs a value n between starting with two then I output the sum of n odd integers. So if they enter 3 then the output should be 9.

1+3+5 and what I am having trouble with is how to tell the loop when to stop. I mean I set the user input to the variable say 3 and then do I set the number of times the loop runs to another variable and then have the loop break when these two variables are equal? I am very very lost. Please help a girl out I haven't been this confused since I watched the ending of Evangelion!

__________________
Stupidnewb is offline   Reply With Quote
Old 10-09-2003, 04:26 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
You could write a PhD thesis in philosophy on Evangelion. I didn't have the patience to even try to figure it out.

This sounds like a job for a "for" loop. But if you want to do it with a while loop, that's fine too.

As I understand, you want to find the sum of 'n' odd integers.

Ok, if that's the case, you want to loop 'n' number of times. You'll need two variables, one to keep the sum and one to keep track of the loop. Here's a sample method:

Code:
public int oddSum(int n){ int sum = 0; int i = 0; while(i < n){ // <Enter odd-summing code here.> i++; // This will increase the value of 'i' by one. It's a common programming shortcut. } return sum; }
The loop breaks when 'i' equals 'n'. Why not when 'i' greater than 'n'? Because in programming, all things start at '0' instead of '1'. Chalk it up to tradition, but it's going to be a recurring theme in programming.

If someone wants to the sum of the first odd number, they'll enter '1'. Well, because 0 is less than 1, the loop will execute. Next time around, 'i' will equal 1, and therefore not be less than 'n', and the loop will break before executing the code again.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems debugging visual c++ in vs.net 2003 Engineer MS Technologies ( ASP, VB, C#, .NET ) 2 07-20-2004 12:19 PM
Help :: Loops & Arrays Jarumajoo Standard C, C++ 3 12-01-2003 08:48 PM
no dial tone problems fabiopao Linux / BSD / OS X 4 02-23-2003 10:51 AM
I have problems... anon Lounge 4 01-12-2003 07:47 PM
MySQL problems sde Linux / BSD / OS X 5 12-07-2002 10:04 PM


All times are GMT -8. The time now is 07:52 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle