0% found this document useful (0 votes)
9 views2 pages

Lec 2.1 Loops+Arrays

Uploaded by

moiz88053
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Lec 2.1 Loops+Arrays

Uploaded by

moiz88053
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

for loop in java

public class Test {

public static void main(String args[]) {

for(int x = 10; x < 20; x = x + 1) {


System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
====================================================
for each loop in java
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(",");
}
}
}
=======================================================
while Loop in java
public class Test {

public static void main(String args[]) {


int x = 10;

while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
========================================================
do while loop in java
public class Test {

public static void main(String args[]) {


int x = 10;

do {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
}
}
===========================================================
scanner input in the Array

package javaapplication10;
import java.util.Scanner;

public static void main(String[] args) {


{

int[] a = new int[5];


Scanner input = new Scanner(System.in);

System.out.println("Please enter number");


for (int i = 0; i < a.length; i++)
{

a[i] = input.nextInt();
}
System.out.println("Numbers are");

for (int i = 0; i <5; i++)


{

System.out.println(a[i]);

}
}
}

==================================================================
scanner input in 2-D Array
public static void main(String[] args) {
{

int[][] a = new int[3][2];


Scanner input = new Scanner(System.in);

System.out.println("Please enter number");


for (int i = 0; i < 3; i++)
for (int j = 0; j < 2; j++)
{

a[i][j] = input.nextInt();
}
System.out.println("Numbers are");

for (int i = 0; i < 3; i++)


for (int j = 0; j < 2; j++)
{

System.out.println(a[i][j]);

}
}
}

You might also like