Created
March 5, 2014 12:53
-
-
Save sivabudh/9366609 to your computer and use it in GitHub Desktop.
How to use for loop.
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
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