Solved

loop for please explain?

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
swing controls 2 21
Need Help! Getting a syntax error and don't understand why 3 38
glassfish admin console 1 20
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

740 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