Jump
statements or unconditional control statement:
- Unconditional control statement is also
called a jump statement.
- There are three kinds of jumping
statements,
*Break
*Continue
Break
statement:
- The break statement is used to execute
particular blocks only.
- For example, the switch case.
Syntax:
Loop or if conditions
break;
or
lable_name: Loop or if conditions
break lable_name;
Example program:
package exampleprogram;
import java.util.Scanner;
public class Break_Example {
public static void main(String[] args) {
int[] num = {1000, 2000, 3000, 4000};
boolean found = false;
Scanner input = new Scanner(System.in);
go:
for (int i = 1; i <= 3; i++) {
System.out.println("Enter your
PIN number : ");
int pin = input.nextInt();
for (int n : num) {
if (n == pin) {
found = true;
break;
}
}
if (found) {
System.out.println("Entered
PIN number is correct.");
System.out.println("You
can choose the below options \n1.Cash withdrawal \n2.Cash deposit \n3.Mini
Statement");
break;
} else {
System.out.println("Sorry
try again!!!");
System.out.println("You
let with " + (3 - i) + " more options...");
System.out.println("Entered PIN number " + pin + " is
incorrect.\nPlease re-enter the pin number");
}
}
}
}
Output:
Continue statement:
- Continue statement is used inside loops.
- Whenever a continue statement is
encountered inside a loop, control directly jumps to the beginning of the loop
for the next iteration, skipping the execution of statements inside the loop’s body for
the current iteration.
Syntax:
Loop or if conditions
continue;
or
lable_name: Loop or if conditions
continue lable_name;
Example program:
package exampleprogram;
import java.util.Scanner;
public class Continue_Example {
public static void main(String args[]) {
Scanner read = new Scanner(System.in);
reading:
for (int i = 1; i <= 3; i++) {
System.out.print("Enter a even
number: ");
int value = read.nextInt();
verify:
if (value % 2 == 0) {
System.out.println("You
are enterd even number : "+value);
break reading;
} else {
System.out.println("Please
enter even number only.");
System.out.println("You
can enter " + (3 - i) + " times only...");
continue reading;
}
}
}
}
Output:
No comments:
Post a Comment