Last active
March 20, 2018 13:43
-
-
Save alexzhernovyi/c47915aee1a0f4621f2a91e0ad66ef2a to your computer and use it in GitHub Desktop.
Loop "FOR"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(initialization; condition ; increment/decrement) | |
{ | |
statement(s); | |
} | |
////////// Standart use of "for" | |
for(int a=29; a>=1; a--){ | |
System.out.println("The value of a is: "+a); | |
////////// Infinite "for" loop | |
class infiniteex { | |
public static void main(String args[]){ | |
for(int a=1; a>=1; a++){ | |
System.out.println("The value of a is: "+a); | |
} | |
} | |
} | |
////////// Using several conditions and variables: | |
for(int i = 3, a = 4 ;i > 0, a > 3 ; i++, a++){ | |
System.out.print(i + a); | |
} | |
////////// For loop example to iterate an array: | |
class ex{ | |
public static void main(String args[]){ | |
int arr[]={3,15,85,2}; | |
//i starts with 0 as array index starts with 0 too | |
for(int i=0; i<arr.length; i++){ | |
System.out.println(arr[i]); | |
} | |
} | |
} | |
////////// Enhanced For loop | |
class ex { | |
public static void main(String args[]){ | |
int arr[]={12,21,55,19}; | |
for (int num : arr) { | |
System.out.println(num); | |
} | |
} | |
} | |
//////////////////////////// | |
String arr[]={"What's up","hello","bye"}; | |
for (String str : arr) { | |
System.out.println(str); | |
} | |
////////// for-each loop | |
class ex { | |
public static void main(String[] args) { | |
char[] vowels = {'j', 'u', 'e', 'r'}; | |
// foreach loop | |
for (char item: vowels) { | |
System.out.println(item); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment