Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

loop for please explain?

Posted on 2004-08-10
5
Medium Priority
?
220 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
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

886 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