Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

loop for please explain?

Posted on 2004-08-10
5
Medium Priority
?
218 Views
Last Modified: 2010-03-31
hi
i am reading this tutorial on java.sun.com and i have problem understanding this program

1 public class BasicsDemo {
2    public static void main(String[] args) {          
3        int sum = 0;
4        for (int current = 1; current <= 10; current++) {
5            sum += current;
6        }
7        System.out.println("Sum = " + sum);
8    }
9  }

i don't understand the following:

line 2 "what is (String[]args) and why is there void in the main function
line 4 & 5 can you explain the loop how does it work because the sum = 55 and i don't know how the number is getting there i just cannot understand the equation and the way the loop works Please explain that loop to me as best as you can


I am just begining in java i found tutorials on java.about.com and java.sun.com

is there any other good sites that you guys know about  

thank you  
0
Comment
Question by:macipod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 500 total points
ID: 11764532
line two explains you that, it is starting method of your java class

generally syntax of main is  RDT static main(String s[])

RDT-> is return data type void means this method will not return any thing


coming to your loop it is calculating

It is just printing the sum of first 10 integeres

0
 
LVL 9

Accepted Solution

by:
Nick_72 earned 500 total points
ID: 11764559
hi,

>>line 2 "what is (String[]args) and why is there void in the main function

the args parameter is an array of strings which contains any parameters you sent to the applications when it was started (for example from the commandline you could use:  java myApplication parameter1 parameter2).
The first position in the args array would contain "parameter1" and the second position in the array would contain "parameter2". Of course, if no parameters are sent, the length of args is 0.
The main method is declared void since it never has a return value.

The loop:

The variable sum is declared as 0, and the loop value (current) starts at 1 and loops as long as current is less or equal to 10, and in every loop it adds its value to the sum variable.
Thus, it's equal to writing:

1+2+3+4+5+6+7+8+9+10 = 55.

Hope it helps,

Nick
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11764562
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 11764635
sum += current;

is equal to:

sum = sum + current;
0
 

Expert Comment

by:umairp
ID: 14493036
good job guys
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question