 |
coding result, in the above example use case switch to display the words of a number |
Repetition Statement
Some repetition is used Java statements include is for, while and do- while.
Statement Loop (for)
Forms to use for repetition statement is:
for (initialization; condition; iteration)
{
statement sequence
}
Initialization, typically used to provide an initial value for the control variable in the loop.
Condition, is a boolean expression that determines whether the loop will be repeated or not.
Iteration, is an expression that defines the amount by which the value loop controllers will change every time repetition.
Examples of uses for coding java programs:
02 | public static void main(String args[]) { double num, sroot, rerr; |
03 | for (num = 1.0 ; num < 100.0 ; num + + ) { sroot = Math.sqrt(num); |
04 | System.out.println ( "The square root of " + num + " is " + sroot); |
05 | / / compute rounding error |
06 | rerr = num - (sroot * sroot); |
07 | System.out.println ( "Rounding error is " + rerr); System.out.println (); |
 |
coding result, in the example above is an example of using for to repeat the program seek to know the square root of 1-100 |
Loop Statement (while)
Another form of repetition in Java process is using while statement. Format syntax is:
while (condition) statement;
statement can be either a single statement or statement within the block. While the condition defines conditions that control the flow of repetition or in the form of a Boolean expression that repetition will continue while the condition is true.
Examples coding Java programs using a while loop:
02 | public static void main(String args[]) { int e; |
04 | for ( int i = 0 ; i < 10 ; i + + ) { result = 1 ; |
09 | System.out.println ( "2 rank " + i + " is " + result); |
 |
coding results, examples of the program is the use of repetition while to figure out the results of a number 2 rank 0-10 |
Statement Loop (do-while)
The statement uses the do-while loop has the following syntax format:
do { statements; } While (condition);
The pattern of grooves do-while almost the same while only this format would do repetition first and then check whether the conditions should next or not.
Examples of program coding java do-while:
02 | public static void main(String args[]) throws java.io.IOException { char ch, answer = 'K' ; |
04 | System.out.println( "There are letters between A and Z." ); System.out. print ( "You guessed it: " ); |
06 | ch = (char) System. in .read(); |
07 | } while (ch = = 'n' | ch = = 'r' ); if (ch = = answer) |
08 | System.out.println( "** TRUE **" ); |
10 | System.out. print ( "...Sorry, Wrong " ); if (ch < answer) |
11 | System.out.println( "the alphabet is too low" ); |
13 | System.out.println( "the alphabet is too hight" ); System.out.println( "Try Again!n" ); |
 |
coding result, in the above example is a program using do-while to guess the letters to answer the letter is found if any of the program will continue to repeat until we input the correct letter, answers letters on the right example is K |
Break can also be used instead of a goto statement that jumps to a label, such as coding examples below:
02 | public static void main(String args[]) { int i; |
03 | for (i = 1 ; i < 4 ; i + + ) {ONE: { |
06 | System.out.println ( "n is the value i is" + i); if (i = = 1 ) |
09 | break THREE; System.out.println ( "will not be printed" ); |
11 | System.out.println ( "After block three." ); |
13 | System.out.println ( "After block two." ); |
15 | System.out.println ( "After block one." ); |
17 | System.out.println ( "After." ); |
 |
coding results breakto |
continue statement
This statement is the opposite of a break, continue statement is used to continue the direct repetition, such as coding examples below:
2 | public static void main(String args[]) { int i; |
3 | for (i = 0 ; i< = 10 ; i + + ) { |
5 | continue ; System.out.println(i); |
 |
coding results, will display the number of the 0-10 to continue for even numbers% 2 |