Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sivabudh
Created March 5, 2014 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivabudh/9366609 to your computer and use it in GitHub Desktop.
Save sivabudh/9366609 to your computer and use it in GitHub Desktop.
How to use for loop.
import java.util.*;
class Untitled {
public static void main(String[] args) {
String string = "5,6,7,8";
String[] strings = string.split(","); // this array has 4 numbers
// "for loop" first example
for(String str : strings) // for loop difference
{
int number = Integer.parseInt(str); // 'str' difference
// Example of how to use if/else
if(number == 5 || number == 6) {
number += 5;
}
else {
number -= 8;
}
System.out.println(number);
}
// "for loop" second example
for(int idx = 0; idx < strings.length; idx++) // for loop difference
{
int number = Integer.parseInt(strings[idx]); // strings[idx] difference
// Example of how to use if/else
if(number == 5 || number == 6) {
number += 5;
}
else {
number -= 8;
}
System.out.println(number);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment