Controller Statement JAVA Program

Capture

Statement Control Program

In this section we will study the statements that control the flow of the program, namely branching (selection) and process repeatability.

Statement Branching

Branching in Java using two types namely if and switch statements.

if statement

Format syntax branching using the if statement is:
if (expression) {
statement;
}
Else
{
statements other;
}
Examples of java program using IF:
01import java.util.*;
02public class IF {
03public static void main(String[] args){ int num;
04Scanner scan= new Scanner(System.in);
05System.out.print("Numbers:> "); num=scan.nextInt();
06if(num % 2==0)
07System.out.println("Guess:> Even.");
08else
09System.out.println("Guess:> Odd.");
10}
11}

GIF
coding result, in the example I insert 4 to test even numbers and odd numbers 5 to test

switch Statement

The switch statement provides a multiway branching, so that the program can select several alternatives. The format switch syntax is as follows:
switch (expression) {case constant1:
statement sequence
break;
constant2 case: statement sequence
break;
constant3 case: statement sequence
break;
...
default:
statement sequence
}
Examples coding java programs using the switch:
01public class Switch {
02public static void main(String args[]) { int i;
03for(i=0; i<10; i++)
04switch(i) {
05case 0:
06System.out.println (i + " is zero"); break;
07case 1:
08System.out.println (i + " is one"); break;
09case 2:
10System.out.println (i + " is two"); break;
11case 3:
12System.out.println (i + " is three"); break;
13case 4:
14System.out.println (i + " is four"); break;
15default:
16System.out.println (i + " is five or more");
17}
18}
19}

GIF
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:
01class FOR {
02public static void main(String args[]) { double num, sroot, rerr;
03for(num = 1.0; num < 100.0; num++) { sroot = Math.sqrt(num);
04System.out.println ("The square root of " + num + " is " + sroot);
05// compute rounding error
06rerr = num - (sroot * sroot);
07System.out.println ("Rounding error is " + rerr); System.out.println ();
08}
09}
10}

GIF
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:
01class While {
02public static void main(String args[]) { int e;
03int result;
04for(int i=0; i < 10; i++) { result = 1;
05= i;
06while(e > 0) {
07result *= 2; e--;
08}
09System.out.println ("2 rank " + + " is " + result);
10}
11}
12}

GIF
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:
01class DoWhile {
02public static void main(String args[]) throws java.io.IOException { char ch, answer = 'K';
03do {
04System.out.println("There are letters between A and Z."); System.out.print("You guessed it: ");
05do {
06ch = (char) System.in.read();
07while(ch == '\n' | ch == '\r'); if(ch == answer)
08System.out.println("** TRUE **");
09else {
10System.out.print("...Sorry, Wrong "); if(ch < answer)
11System.out.println("the alphabet is too low");
12else
13System.out.println("the alphabet is too hight"); System.out.println("Try Again!\n");
14}
15}
16 while(answer != ch);
17}
18}

GIF
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:
01class BreakGoto {
02public static void main(String args[]) { int i;
03for (i = 1; i <4; i ++) {ONE: {
04TWO: {
05THREE: {
06System.out.println ("\n is the value i is" + i); if (i == 1)
07break ONE; if (i == 2)
08break TWO; if (i == 3)
09break THREE; System.out.println ("will not be printed");
10}
11System.out.println ("After block three.");
12}
13System.out.println ("After block two.");
14}
15System.out.println ("After block one.");
16}
17System.out.println ("After.");
18}
19}


GIF
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:
1public class Continue {
2public static void main(String args[]) { int i;
3for(i = 0; i<=10; i++) {
4if((i%2) != 0)
5continue; System.out.println(i);
6}
7}
8}

GIF
coding results, will display the number of the 0-10 to continue for even numbers% 2
Disqus Comments