Control Statements in Java
Conditional Control Statement
The conditional control statement controls the order in which a java program is executed.
Types of Conditional control statement:
If statement
Switch statement
If Statement
Syntax :
i) if(condition){
s1;
}
ii) if(condition){
s1;
}
else{
s2;
}
iii) if (condition) {
s1;
} else
if (condition) {
s2;
} else {
s3;
}
Question : Write a program to pass three numbers from command lines and find:
i) minimum
ii) maximum
package com.javabykiran.basics;
public class Lab12 {
public static void main(String[] args) {
int a = 5;
int b = 2;
int c = 1;
if ((a > b) && (a > c)) {
System.out.println("Min" +a);
} else if (b > c) {
System.out.println("Min" +b);
} else {
System.out.println("Min" + c);
}
if (a < b) {
if (a < c) {
System.out.println("Min:" +a);
} else {
System.out.println("Min" + c);
}
} else {
if (b < c) {
System.out.println("Min" +b);
} else {
System.out.println("Min" + c);
}
}
}
}
Switch Statement
Syntax :
switch(expr) {
case No1: s1;
break;
case Non: sn;
break;
default: s;
}
Example:
package com.javabykiran.basics;
public class Lab16 {
public static void main(String[] args){
int a = Integer.parseInt(args[0]);
switch (a) {
case 1:
System.out.println("FRI");
break;
case 2:
System.out.println("MON");
break;
case 3:
System.out.println("TUE");
break;
default:
System.out.println("Invalid No");
break;
}
}
}
NOTE: The default statement can be written anywhere in the switch statement. But they have a break. In the last statement, it does not have break, but it is also possible.
Looping Control Statement
Looping statements execute statements over and over again, in a loop. It continues until the condition result is true.
Types of Looping control statement:
For statement
While statement
Do while statement
For Statement
The for statement lets a code execute in a loop continuously.
Syntax :
for (initialization; condition; increment/decrement) {
s1; s2;
}
for( ; ; ) { // possible empty
s1; s2;
}
Question. Write a programme to read a number from the command line and display the numbers which divisible by five, from one to t h e given number in both order.
package com.javabykiran.basics;
public class Lab17 {
public static void main(String[] args) {
int n = 67;
System.out.println("Forward order");
for (int i = 1; i <= n; i++){
if (i % 5 == 0) {
System.out.println(i);
}
}
System.out.println("Reverse order");
for (int i = n; i >= 1; i--){
if (i % 5 == 0) {
System.out.println(i);
}
}
}
}
Output:
Forward order
5
10
15
20
25
30
35
40
45
50
55
60
65
Reverse order
65
60
55
50
45
40
35
30
25
20
15
10
5
Question. Write a programme to generate prime numbers from 1 to 100.
package com.javabykiran.basics;
/*
Prime Numbers Java Example:
This example shows how to generate prime numbers
between 1 and the given number, using a for
loop.
*/
public class GeneratePrimeNumbersExample {
public static void main(String[] args){
// define limit
int limit = 100;
System.out.println("Prime numbers between 1 and " +limit);
// loop through the numbers one by one
for (int i = 1; i < 100; i++) {
boolean isPrime = true;
// check to see if the number is prime
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
// print the number
if (isPrime)
System.out.print(i + " ");
}
}
}
/*
* Output of Prime Numbers example would be Prime numbers between 1 and 100 12
*3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
*/
Question. Write a programme to read a two numbers from command lines and print all the prime numbers between given range.
package com.javabykiran.basics;
public class Lab19 {
static Boolean isPrime(int n) {
int count = 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
count++;
break;
}
}
if (count == 0) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
int m = 3;
int n = 30;
for (int i = m; i <= n; i++) {
boolean b = isPrime(i);
if(b){
System.out.println(i);
}
}
}
}
Output:
3
5
7
11
13
17
19
23
29
While Statement
The while statement executes a single line continuously until the condition is met.
Syntax :
initialization;
while(condition) {
s1;
s2;
Increment/decrement
}
Example :
package com.javabykiran.basics;
public class Lab22 {
public static void main(String[] as) {
int n = Integer.parseInt(as[0]);
//int n = 10;
System.out.println("Forward order");
int i = 1;
while (i <= n) {
if (i % 5 == 0) {
System.out.println(i);
}
i++;
}
System.out.println("Reverse order");
i = n;
while (i >= 1) {
if (i % 5 == 0) {
System.out.println(i);
}
i--;
}
}
}
Output:
Forward order
5
10
Reverse order
10
5
Question : Write a programme to read the number from the command line and display the sum of individual digits of the given number.
package com.javabykiran.basics;
public class Lab33 {
public static void main(String[] args){
int n = 123;
// int n=Integer.parseInt(args [0]);
int sum = 0;
while (n != 0) {
int r = n % 10;
sum += r;
n = n / 10;
}
System.out.println("Sum = " + sum);
}
}
Output:
Sum = 6
Do while Statement
The do while statement executes a particular chunk of the code until a Boolean expression is true.
Syntax :
Initialization;
do {
//processing s2;
Increment/decrement
} while ( condition );
Example:
package com.javabykiran.basics;
public class Lab20 {
public static void main(String[] args) {
int n = 30;
System.out.println("while");
int i = 1;
while (i <= n)
System.out.println("ok" + i);
if (i% 5 == 0) {
System.out.println(i);
}
i++;
System.out.println("do_While");
i = 1;
do {
if ((i % 5) == 0) {
System.out.println(i);
}
i++;
System.out.println("ok" +i);
} while (i <= n);
}
}
Enumerate the difference between While and Do While statement.
| While | Do While |
|---|---|
| In the case of While, the first condition will be verified and then the statement will be executed, Inside the while block. | In the case of Do-While first statement, inside do block is executed and the condition is be verified. |
| If the condition is false the first time, then the block of statement will be executed zero times. | If the condition is false the first Time, then the block of statement will be executed once. |
Unconditional Control Statement
Types of Unconditional control statements:
Break
Continue
Goto
Break
Break statement transfers the control unconditionally to the end of the block.
Continue
The Continue statement transfers the control unconditionally to the beginning of the block.
Example:
package com.javabykiran.basics;
public class Lab23 {
public static void main(String[] args) {
int n=12;
for(int i=1; i < n; i++) {
if(i %2==0){
System.out.println(i+" Next is continue");
continue;
}
else if(i==9) {
System.out.println("i is 9");
break;
}
System.out.println("Ok "+i);
}
System.out.println("Done");
}
}
Output:
Ok 1
2 Next is continue
Ok 3
4 Next is continue
Ok 5
6 Next is continue
Ok 7
8 Next is continue
i is 9
Done
Example:
package com.javabykiran.basics;
public class Lab24 {
public static void main(String[] args){
/*
* if(1) { } while(1) { -- }
*/
int i = 1;
/*
*for(i<=3)
sop(i);
i++; } /* while(true) sop("ok");}
*/
// System.out.println("Done");
}
}
package com.javabykiran.basics;
public class Lab25 {
public static void main(String[] args) {
for (int i = 1; i <= 2; i++){
JBK: for (int j = 1; j <= 2; j++) {
for (int k = 1; k <= 2; k++){
if (k <= 3)
System.out.println(k + "\t");
else
break JBK;
} // end of k for
System.out.println("JBK");
} // end of j for
System.out.println("*");
} // end of i for
System.out.println("Done");
}
}
Output:
1
2
JBK 1
2
JBK
* 1
2
JBK 1
2
JBK
* Done
← Back to Java Language Chapter