For Loop

Discussion in 'Java' started by netOwen, Nov 29, 2016.

  1. netOwen

    netOwen

    Joined:
    Nov 28, 2016
    Messages:
    2
    Likes Received:
    0
    I am wonder if someone could explain to me, how this for loop works.

    public class DoWhat
    {
    public static void main(string[] args)
    {
    int x = 0;
    int i;
    for ( i = 1; i <=4; i++)
    {
    x = x + (i*i +3);
    System.out.printf("%4d %4d\n", i,x);
    }// end of loop
    }//end main
    } //end class DoWhat

    i know that the output is

    1 4
    2 11
    3 23
    4 43

    how dose it come to this out put??
     
    netOwen, Nov 29, 2016
    #1
    1. Advertisements

  2. netOwen

    Nihar

    Joined:
    Apr 25, 2017
    Messages:
    3
    Likes Received:
    0
    Enhanced for loop in Java
    As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse collection of elements including arrays.
    Learn Java here:https://hackr.io/tutorials/learn-java

    Syntax
    Following is the syntax of enhanced for loop −

    for(declaration : expression) {
    // Statements
    }

    • Declaration − The newly declared block variable, is of a type compatible with the elements of the array you are accessing. The variable will be available within the for block and its value would be the same as the current array element.

    • Expression − This evaluates to the array you need to loop through. The expression can be an array variable or method call that returns an array.
    Example
    public class Test {

    public static void main(String args[]) {
    int [] numbers = {10, 20, 30, 40, 50};

    for(int x : numbers ) {
    System.out.print( x );
    System.out.print(",");
    }
    System.out.print("\n");
    String [] names = {"James", "Larry", "Tom", "Lacy"};

    for( String name : names ) {
    System.out.print( name );
    System.out.print(",");
    }
    }
    }
    This will produce the following result −

    Output
    10, 20, 30, 40, 50,
    James, Larry, Tom, Lacy,
     
    Nihar, Apr 25, 2017
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,753
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,629
    Tim Slattery
    Mar 30, 2005
  3. -
    Replies:
    12
    Views:
    1,093
    Remon van Vliet
    Jun 15, 2005
  4. Byte
    Replies:
    4
    Views:
    716
    adam.deprince@gmail.com
    Mar 24, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    929
    Ulrich Eckhardt
    Mar 4, 2013
  6. prayercall

    background image for each page

    prayercall, May 11, 2016, in forum: HTML and CSS
    Replies:
    1
    Views:
    779
    Exodus_AU
    May 16, 2016
  7. Jose Concepcion

    Text Adventure Game (for Steam)

    Jose Concepcion, Jun 29, 2016, in forum: Introductions
    Replies:
    0
    Views:
    640
    Jose Concepcion
    Jun 29, 2016
  8. Cambo

    Looking for help

    Cambo, Mar 10, 2017, in forum: Off-Topic
    Replies:
    1
    Views:
    243
Loading...