This shows you the differences between two versions of the page.
java:loops:for [2013/04/18 09:01] 88.74.75.6 created |
java:loops:for [2024/02/16 01:04] (current) |
||
---|---|---|---|
Line 9: | Line 9: | ||
a for loop do statements while the condition is true/untrue, the increment counts the loop, it is possible to use more than 1 condition in the for loop | a for loop do statements while the condition is true/untrue, the increment counts the loop, it is possible to use more than 1 condition in the for loop | ||
- | ===== example of for example 1 in java ===== | + | ===== example 1 of for in java ===== |
<code java> | <code java> | ||
Line 40: | Line 40: | ||
i is: 4 <= 5 | i is: 4 <= 5 | ||
i is: 5 <= 5 | i is: 5 <= 5 | ||
- | |||
- | + | ==== example 2 of for in java ==== | |
+ | with 2 conditions | ||
+ | <code java> | ||
+ | package codereferececomjava; | ||
+ | |||
+ | import java.util.Scanner; | ||
+ | |||
+ | public class CodeRefereceComJava { | ||
+ | |||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Scanner keyboard = new Scanner(System.in); | ||
+ | int passwd=12345; | ||
+ | int typedIn = 0; | ||
+ | |||
+ | // 2 conditions in for loop | ||
+ | for (int i = 0; i < 3 && passwd != typedIn; i++) { | ||
+ | System.out.print("please enter 12345 between 3 attempts: " ); | ||
+ | typedIn = keyboard.nextInt(); | ||
+ | } | ||
+ | |||
+ | if (passwd == typedIn) { | ||
+ | System.out.println("Success"); | ||
+ | } else { | ||
+ | System.out.println("Failure"); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | |||
+ | === output of for example 2 in java === | ||
+ | |||
+ | please enter 12345 between 3 attempts: 42 | ||
+ | please enter 12345 between 3 attempts: 12345 | ||
+ | Success | ||