Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Control

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Control statements

If else,switch,for,while ,do while,break ,continue

If-else Statement
The Java if statement is used to test the condition. It checks boolean
condition: true or false. There are various types of if statement in java.

o if statement

o if-else statement

o if-else-if ladder

o nested if statement

Java IF Statement
The Java if statement tests the condition. It executes the if block if condition is true.

Syntax:

1. if(condition){
2. //code to be executed
3. }
Example:

1. public class IfExample {


2. public static void main(String[] args) {
3. int age=20;
4. if(age>18){
5. System.out.print("Age is greater than 18");
6. }
7. }
8. }
Test it Now

Output:

Age is greater than 18

Java IF-else Statement


The Java if-else statement also tests the condition. It executes the if block if condition is
true otherwise else block is executed.

Syntax:

1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }

Example:

1. public class IfElseExample {


2. public static void main(String[] args) {
3. int number=13;
4. if(number%2==0){
5. System.out.println("even number");
6. }else{
7. System.out.println("odd number");
8. }
9. }
10. }
Test it Now

Output:

odd number

Java IF-else-if ladder Statement


The if-else-if ladder statement executes one condition from multiple statements.

Syntax:

1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
Example:

1. public class IfElseIfExample {


2. public static void main(String[] args) {
3. int marks=65;
4.
5. if(marks<50){
6. System.out.println("fail");
7. }
8. else if(marks>=50 && marks<60){
9. System.out.println("D grade");
10. }
11. else if(marks>=60 && marks<70){
12. System.out.println("C grade");
13. }
14. else if(marks>=70 && marks<80){
15. System.out.println("B grade");
16. }
17. else if(marks>=80 && marks<90){
18. System.out.println("A grade");
19. }else if(marks>=90 && marks<100){
20. System.out.println("A+ grade");
21. }else{
22. System.out.println("Invalid!");
23. } }
24. }

Output: C grade
Java Switch Statement
The Java switch statement executes one statement from multiple conditions. It is like if-
else-if ladder statement.

Syntax:

1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matched;
12. }

Example:

1. public class SwitchExample {


2. public static void main(String[] args) {
3. int number=20;
4. switch(number){
5. case 10: System.out.println("10");break;
6. case 20: System.out.println("20");break;
7. case 30: System.out.println("30");break;
8. default:System.out.println("Not in 10, 20 or 30");
9. }
10. }
11. }
Test it NowOutput: 20

Java Switch Statement is fall-through


The java switch statement is fall-through. It means it executes all statement after first
match if break statement is not used with switch cases.

Example:

1. public class SwitchExample2 {


2. public static void main(String[] args) {
3. int number=20;
4. switch(number){
5. case 10: System.out.println("10");
6. case 20: System.out.println("20");
7. case 30: System.out.println("30");
8. default:System.out.println("Not in 10, 20 or 30");
9. }
10. }
11. }
Test it NowOutput:
20
30
Not in 10, 20 or 30

Java For Loop


The Java for loop is used to iterate a part of the program several times. If the number of
iteration is fixed, it is recommended to use for loop.

There are three types of for loop in java.

o Simple For Loop

o For-each or Enhanced For Loop

o Labeled For Loop

Java Simple For Loop


The simple for loop is same as C/C++. We can initialize variable, check condition and
increment/decrement value.

Syntax:

1. for(initialization;condition;incr/decr){
2. //code to be executed
3. }

Example:

1. public class ForExample {


2. public static void main(String[] args) {
3. for(int i=1;i<=10;i++){
4. System.out.println(i);
5. }
6. }
7. }
Test it Now

Output:

1
2
3
4
5
6
7
8
9
10

Java For-each Loop


The for-each loop is used to traverse array or collection in java. It is easier to use than
simple for loop because we don't need to increment value and use subscript notation.

It works on elements basis not index. It returns element one by one in the defined
variable.

Syntax:

1. for(Type var:array){
2. //code to be executed
3. }

Example:

1. public class ForEachExample {


2. public static void main(String[] args) {
3. int arr[]={12,23,44,56,78};
4. for(int i:arr){
5. System.out.println(i);
6. }
7. }
8. }
Test it Now

Output:

12
23
44
56
78

Java Labeled For Loop


We can have name of each for loop. To do so, we use label before the for loop. It is
useful if we have nested for loop so that we can break/continue specific for loop.

Normally, break and continue keywords breaks/continues the inner most for loop only.

Syntax:

1. labelname:
2. for(initialization;condition;incr/decr){
3. //code to be executed
4. }

Example:

1. public class LabeledForExample {


2. public static void main(String[] args) {
3. aa:
4. for(int i=1;i<=3;i++){
5. bb:
6. for(int j=1;j<=3;j++){
7. if(i==2&&j==2){
8. break aa;
9. }
10. System.out.println(i+" "+j);
11. }
12. }
13. }
14. }

Output:

1 1
1 2
1 3
2 1

If you use break bb;, it will break inner loop only which is the default behavior of any
loop.

1. public class LabeledForExample2 {


2. public static void main(String[] args) {
3. aa:
4. for(int i=1;i<=3;i++){
5. bb:
6. for(int j=1;j<=3;j++){
7. if(i==2&&j==2){
8. break bb;
9. }
10. System.out.println(i+" "+j);
11. }
12. }
13. }
14. }

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java Infinitive For Loop


If you use two semicolons ;; in the for loop, it will be infinitive for loop.

Syntax:

1. for(;;){
2. //code to be executed
3. }

Example:

1. public class ForExample {


2. public static void main(String[] args) {
3. for(;;){
4. System.out.println("infinitive loop");
5. }
6. }
7. }
Output:

infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c

Java While Loop


The Java while loop is used to iterate a part of the program several times. If the number
of iteration is not fixed, it is recommended to use while loop.

Syntax:

1. while(condition){
2. //code to be executed
3. }

Example:

1. public class WhileExample {


2. public static void main(String[] args) {
3. int i=1;
4. while(i<=10){
5. System.out.println(i);
6. i++;
7. }
8. }
9. }
Test it Now

Output:

1
2
3
4
5
6
7
8
9
10

Java Infinitive While Loop


If you pass true in the while loop, it will be infinitive while loop.

Syntax:

1. while(true){
2. //code to be executed
3. }

Example:

1. public class WhileExample2 {


2. public static void main(String[] args) {
3. while(true){
4. System.out.println("infinitive while loop");
5. }
6. }
7. }
Output:

infinitive while loop


infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
ctrl+c

Now, you need to press ctrl+c to exit from the program.

Java do-while Loop


The Java do-while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed and you must have to execute the loop at least once, it is
recommended to use do-while loop.

The Java do-while loop is executed at least once because condition is checked after loop
body.

Syntax:

1. do{
2. //code to be executed
3. }while(condition);
Example:

1. public class DoWhileExample {


2. public static void main(String[] args) {
3. int i=1;
4. do{
5. System.out.println(i);
6. i++;
7. }while(i<=10);
8. }
9. }
Test it Now

Output:

1
2
3
4
5
6
7
8
9
10

Java Infinitive do-while Loop


If you pass true in the do-while loop, it will be infinitive do-while loop.

Syntax:

1. do{
2. //code to be executed
3. }while(true);

Example:

1. public class DoWhileExample2 {


2. public static void main(String[] args) {
3. do{
4. System.out.println("infinitive do while loop");
5. }while(true);
6. }
7. }

Output:

infinitive do while loop


infinitive do while loop
infinitive do while loop
ctrl+c

Java Break Statement


The Java break is used to break loop or switch statement. It breaks the current flow of
the program at specified condition. In case of inner loop, it breaks only inner loop.

Syntax:

1. jump-statement;
2. break;

Java Break Statement with Loop


Example:
1. public class BreakExample {
2. public static void main(String[] args) {
3. for(int i=1;i<=10;i++){
4. if(i==5){
5. break;
6. }
7. System.out.println(i);
8. }
9. }
10. }

Output:

1
2
3
4

Java Break Statement with Inner Loop


It breaks inner loop only if you use break statement inside the inner loop.

Example:

1. public class BreakExample2 {


2. public static void main(String[] args) {
3. for(int i=1;i<=3;i++){
4. for(int j=1;j<=3;j++){
5. if(i==2&&j==2){
6. break;
7. }
8. System.out.println(i+" "+j);
9. }
10. }
11. }
12. }

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java Continue Statement


The Java continue statement is used to continue loop. It continues the current
flow of the program and skips the remaining code at specified condition. In case
of inner loop, it continues only inner loop.

Syntax:

1. jump-statement;
2. continue;

Java Continue Statement Example


Example:

1. public class ContinueExample {


2. public static void main(String[] args) {
3. for(int i=1;i<=10;i++){
4. if(i==5){
5. continue;
6. }
7. System.out.println(i);
8. }
9. }
10.}

Output:

1
2
3
4
6
7
8
9
10

Java Continue Statement with Inner Loop


It continues inner loop only if you use continue statement inside the inner loop.

Example:

1. public class ContinueExample2 {


2. public static void main(String[] args) {
3. for(int i=1;i<=3;i++){
4. for(int j=1;j<=3;j++){
5. if(i==2&&j==2){
6. continue;
7. }
8. System.out.println(i+" "+j);
9. }
10. }
11.}
12.}

Output:

1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

You might also like