About 2,760,000 results
Open links in new tab
  1. java - Adding values in a for loop - Stack Overflow

    Oct 14, 2010 · My answer just has a little bit different syntax to perform the loop and sum. import java.util.Scanner public class AddingValuesWithAForLoop { public static void main( String[] args ) { Scanner keyboard = new Scanner(System.in); System.out.println( " \n" ); System.out.println( "Number: " ); int number = keyboard.nextInt(); int sum = 0; for (int ...

  2. java - How to get a FOR loop to add numbers - Stack Overflow

    Oct 17, 2013 · It just have to be used to control the number of how many times the for loop to be executed. You have to add the currently read value to the sum of the values read before. this should be done like this .. sum=0; sumt=0; for (int number=1; number<=4; number++) sum =ConsoleInput.readInt("enter"); sumt += sum; . System.out.println("total is"+sumt);

  3. Java For Loop - W3Schools

    When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop: Syntax for ( statement 1 ; statement 2 ; statement 3 ) { // code block to be executed }

  4. Java For Loop - GeeksforGeeks

    Apr 17, 2025 · The for-each loop in Java (also called the enhanced for loop) was introduced in Java 5 to simplify iteration over arrays and collections. It is cleaner and more readable than the traditional for loop and is commonly used when the exact index of an element is not required.

  5. Java Program to Compute the Sum of Numbers in a List Using For-Loop

    Sep 8, 2022 · Given a list of numbers, write a Java program to find the sum of all the elements in the List using for loop. For performing the given task, complete List traversal is necessary which makes the Time Complexity of the complete program to O (n), where n is the length of the List. Example: Output: Sum = 6. Input : List = [5, 1, 2, 3]

  6. Java For Loop - Baeldung

    Feb 16, 2025 · In this article, we’ll look at a core aspect of the Java language – executing a statement or a group of statements repeatedly using a for loop. 2. Simple for Loop. A for loop is a control structure that allows us to repeat certain operations …

  7. Java for Loop (With Examples) - Programiz

    Java for loop is used to run a block of code for a certain number of times. The syntax of for loop is: for (initialExpression; testExpression; updateExpression) { // body of the loop }

  8. Iterating over ArrayLists in Java - GeeksforGeeks

    Jun 4, 2024 · Method 1: Using for loop. Method 2: Using while loop. Method 3: Using for each loop. Method 4: Using Iterator. Method 5: Using Lambda expressions. Method 6: Using Enumeration interface. Now it is a further additive to the article as we are done with discussing all methods that can be used to iterate over elements.

  9. Adding Elements to a Collection During Iteration - Baeldung

    Mar 7, 2025 · Explore several methods, including ListIterator (enhanced for looping with a copy) and Java 8 Streams, which allow you to add elements to a list during iteration in Java.

  10. java - Adding values in for loop - Stack Overflow

    Feb 21, 2013 · Just add one variable outside the for loop and add your values to this variable to calculate total: int n = scan.nextInt(); int total = 0; for(int i=0; i <= n; i++){ System.out.println ("number please"); int c = scan.nextInt(); //stuck total += c; }

Refresh