CONTROL STATEMENTS
CONTROL STATEMENT |
A control statement determains whether the nest set of tasks have to be Executed or not
These are divided into three categories:
- Selection Statements
- Iterative Statements
- Jump Statements
SELECTION STATEMENT |
Selection statements are classified into two types:
- if
- Switch
IF
|
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
- if statement
- if-else statement
- if else-if ladder
- nested if statement
The java if statement tests the condition. if executes the if block it condition is true
SYNTAX:
|
EXAMPLE:
package mypackage;
public class Ifcondition {
public static void main(String[] args) { int a, b; a=20; b=29;
if (a>18) { System.out.println("Age is greater then 18"); } } } |
OUTPUT: Age is greater then 18 |
2. IF-ELSE STATEMENT
The java if-else statement also tests the condition. it executes the if block if condition is true other wise else block is executed
SYNTAX:
|
EXAMPLE:
package myPackage; public class IfelseCondittion { public static void main(String[] args) { String actual="ABCD"; String expected="ABCD123"; boolean status=actual.equals(expected); if(status) { System.out.println("THIS IS SUCCESS"); } else { System.out.println("THIS IS FAILURE"); } } } |
OUTPUT: THIS IS FAILURE |
package mypackage;
public class Ifelsecondition {
public static void main(String[] args) { int a , b; a=5; b=5;
if (a == b) { System.out.println("pass"); } else { System.out.println("fail"); } } } |
OUTPUT: PASS |
package mypackage;
public class Ifelsecondition {
public static void main(String[] args) { int Telugu, English; Telugu=50; English=59;
if (Telugu > 35 || English > 35) { System.out.println("pass"); } else { System.out.println("fail"); } } } |
OUTPUT: Pass |
package mypackage;
public class Ifelsecondition {
public static void main(String[] args) { int Telugu, English; Telugu=50; English=29;
if (Telugu > 35 && English > 35) { System.out.println("pass"); } else { System.out.println("fail"); } } } |
OUTPUT: Pass |
EVEN / ODD NUMBER
It is a program of odd and even numbers check it the number is divisible by 2 or not
package mypackage;
public class Ifelsecondition {
public static void main(String[] args) { int number =50;
if (number % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } } } |
OUTPUT: Odd |
USING TERNARY OPERATOR
We can also use ternary operator ( ?, : ) to perform the task of if - else condition it is a short hand way to check the condition if the condition is true the result of ? is returned but it the condition is false the result of : is returned.
package mypackage;
public class Ifelsecondition {
public static void main(String[] args) {
int number=13; String output=(number%2==0)?"Even Number":"Odd Number"; System.out.println(output); } } |
OUTPUT: Odd Number |
package condition; public class leapyear { public static void main (String [] args ) { int year = 2020; if ((year %4 == 0)) { System.out.printIn("LEAP YEAR"); } else { System.out.printIn("COMMON YEAR"); } } } |
OUTPUT: LEAP YEAR |
|
package statements; public class IfElseIf { public static void main (String[]args] { int marks = 90; if (marks >= 80 && marks <100 ) { system.out.println("A Grade"); } else if (marks >= 60 && marks <80 ) { system.out.println("B Grade"); { else if (marks >= 40 && marks <60 ) { system.out.println("C Grade"); } else if (marks >= 35 && marks <40 ) { system.out.println("D Grade"); }else{ system.Out.Println("fail); } } } |
OUTPUT: A Grade |
|
package operators; public class Arithmaticperators { public static void main (String[]args] { int age= 50; int weight = 80; if ( age > 40 ) { if (weight > 50) { System.out.printIn();"Eligible to donate blood"); } } } |
OUTPUT: Eligible to donate blood |
- Java switch statement executes one statement from multiple conditions. It is like if-else-ladder statement.
- The switch statement works with byte, short, int, long, enum types, string and some wrapper types.
- Since java 7 you can use strings in the switch statement
- There are one or N number of case values for a switch expression
- The case value must be of switch expression type only.
- the values must be unique.
- Each case statement can have a break statement which is optional.
- The case value can have a default label which is optional
|
package statements; public class Switch { public static void main (String[]args] { int age = 20; switch (age) { case 10: system.out.println("10"); break; case 20: system.out.println("20"); break; case 30: system.out.println("30"); break; case 40: system.out.println("40"); break; case 50: system.out.println("50"); break; default: System.out.printIn("invalid"); } } |
OUTPUT: 20 |
ITERATIVE STATEMENTS |
Iterative statements are divided into four types they are
- While
- do-while
- for
- for-each
WHILE |
while (condition){ } |
package controlstatements; public class wholeLoop { public static void main (String[]args] { int = 1i; while (i <=10) { System.Out.Println(i); i++; } } } |
OUTPUT: 1 2 3 4 5 6 7 8 9 10 |
while (true){ } |
do{ }while (condition); |
package controlstatements; public class DoWhile { public static void main (String[]args] { int = 1i; do{ System.Out.Println(i); i++; }while(i <=10); } } |
OUTPUT: 1 2 3 4 5 6 7 8 9 10 |
do{ |
FOR |
We can initialize
the variable, check condition and increment/decrement value. It consists
of four parts:
- Initialization: It is the
initial condition which is executed once when the loop starts. Here, we
can initialize the variable, or we can use an already initialized
variable. It is an optional condition.
- Condition: It is the
second condition which is executed each time to test the condition of the
loop. It continues execution until the condition is false. It must return
boolean value either true or false. It is an optional condition.
- Statement: The
statement of the loop is executed each time until the second condition is
false.
- Increment/Decrement: It
increments or decrements the variable value. It is an optional condition.
for(initialization; condition; inc/dec) { } |
package controlstatements; public class ForLoop{ public static void main (String[]args] {
for (int i = 1; i <= 10; i++) { System.out.println(i); } } } |
OUTPUT: 1 2 3 4 5 6 7 8 9 10 |
package controlstatement; public class forLoop{ public static void main (String[]args] {
for (int i = 10; i >= 1; i--) { System.out.println(i); } } } |
OUTPUT: 10 9 8 7 6 5 4 3 2 1 |
package controlstatements; public class EvenNumbers{ public static void main (String[]args] {
int num = 100; for (int i = 2; i <= num; i=i+2) { System.out.println(i); } } } |
OUTPUT: 2 4 6 8 10 . . . . 98 100 |
package controlstatements; public class EvenNumbers{ public static void main (String[]args] {
int num = 100; for (int i = 1; i <= num; i=i+2) { System.out.println(i); } } } |
OUTPUT: 1 3 5 7 9 . . . . 97 99 |
If we
have a for loop inside the another loop, it is known as nested for loop. The
inner loop executes completely whenever outer loop executes.
package conditionalstatements; public class NestedForLoop { public static void main (String[]args] { . for (int i = 1; i <= 3; i++) { System.out.println(i + " " + j); } } } |
OUTPUT: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 |
package conditionalstatements; public class Pyramid { public static void main (String[]args] { . for (int i = 1; i <= 5; i++) { System.out.println(" * "); } System.out.Println(); } } |
OUTPUT: * ** *** **** ***** |
FOR-EACH |
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 does not index. It returns element
one by one in the defined variable.
while(type variableName : arrayName) { } |
package conditionalstatements; public class ForEach{ public static void main (String[]args] { String[] cars = {"Volvo", "BMW", "Ford", "Honda"}; for (String i : cars) { System.out.println(i); } . } } |
OUTPUT: Volvo BMW Ford Honda |
TABLES
package conditionalstatements; public class Tables{ public static void main (String[]args] { } . } } |
OUTPUT: Volvo BMW Ford Honda |
JUMP STATEMENTS |
BREAK |
CONTINUE |
RETURN |