0

What is actually this code doing, need step wise help

public class Program { public static void main(String[] args) { String[] Values = {"A", "B", "C"}; for ( int i = 0; i < Values.length; i++) { System.out.println(Values[i]); } } }

1st Oct 2017, 8:12 AM
Hrishi Bhoir
2 Respuestas
+ 3
First line is class declaration Second line defines the main function which is the starting or entry point of any program In third line an array of type String is defined with three elements in it i.e., A, B, C. In fourth line a loop is defined which iterates through the array we defined earlier. It starts from o till the length of array i.e. 3. Values.length = 3 (total no. of elements in array) i++ increments the value of i by 1 System.out.println(Values[i]) will simply output each element based on its index. array index starts from 0, so when i=0 Values[i] = Values[0] = A i++ = i+1 = 0+1 = 1 Values[i] = Values[1] = B i++ = i+1 = 1+1 = 2 Values[i] = Values[2] = C i++ = i+1 = 2+1 = 3 when i = 3 the loop gets ended due to defined condition (i < Values.length i.e., i < 3) in for loop. Output: A B C
1st Oct 2017, 9:09 AM
Ankit Agrawal
Ankit Agrawal - avatar
+ 7
in simple terms.. your for loop is looping the amount of elements you have in your array each time it loops it adds 1 to int i which prints out like this. values [0] values [1] values [2] output A B C if you want this to print on the same line remove "ln" at the end of "println"
1st Oct 2017, 8:59 AM
D_Stark
D_Stark - avatar