Solved

loop for please explain?

Posted on 2004-08-10
5
209 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 125 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 125 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now