Solved

loop for please explain?

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

825 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