Control Structure in Java
Just like every program Java has also a control structure, it is a section of a program that is used to resolve a problem based on a given parameter.
There are three types of control structure :-
Sequence:- Sequence is trivial. Normally in the program the statement are performed one after the other in the organized manner in which they are written .This is called a sequential execution.
Selection :- The selection structure is used to choose among alternative sequence of action in a program. For example:- if structure, if/else structure, switch structure,
Repetition :- A repetition structure allows the programmer to point out that an action is to be repeated while some condition remain true. For example:- while structure, do/whilestructure, for structure
The if selection statement
The if selection structure performs a point out action only when the expression evaluates to true; otherwise, the action is skipped.
Syntax
if(test_expression) {
// Statement will execute
}
Example
public class Student{
public static void main(String args[]){
int studentGrade = 60;
if(studentGrade >= 60)
System.out.println("Passed");
}
}
Output
Passed
The if/else selection statement
The if/else selection structure allows the programmer to define that a different operation is to be performed when the condition is more true than when the condition is false.
Syntax:-
if (test_expression) {
// Statement 1 will execute
}
else {
// Statement 2 will execute
}
Example
public class IfElse {
public static void main(String args[]){
int number=300;
if( number < 100 ){
System.out.println("The number is less than 100");
}
else {
System.out.println("The number is greater than or equal 100");
}
}
}
Output
The number is greater than or equal 100
The if ...else...if selection statement
The if ...else...if selection structure allows the programmer to execute the many blocks of code.
Syntax
if (test_expression1)
{
// Statement 1 will execute
}
else if(test_expression2)
{
// Statement 2 will execute
}
else if (test_expression3)
{
// Statement 3 will execute
}
.
.
else
{
// Statement 4 will execute
}
Example
public class IfElseIf {
public static void main(String[] args) {
int marks=90;
if(marks<30){
System.out.println("Student is fail");
}
else if(marks>=40 && marks<50){
System.out.println("Student got D grade");
}
else if(marks>=50 && marks<60){
System.out.println("Student got C grade");
}
else if(marks>=60 && marks<70){
System.out.println("Student got B grade");
}
else if(marks>=70 && marks<90){
System.out.println("Student got A grade");
}else if(marks>=90 && marks<100){
System.out.println("Student got A+ grade");
}else{
System.out.println("Invalid!");
}
}
}
Output
Student got A+ grade
The Nested if ...else selection statement
Nested if/else structure test for multiple cases by placing if/else structures inside if/else structures.
Syntax:-
if (test_expression1)
{
// test_expression1 will execute when condition is true
if (test_expression2)
{
// test_expression2 will execute when condition is true
}
}
Example
public class Number {
public static void main(String[] args) {
Integer num1 = 499775, num2 = 66799, num3 = 190775, SmallestNumber;
if (num1 <= num2) {
if (num1 <= num3) {
SmallestNumber = num1;
} else {
SmallestNumber = num3;
}
} else {
if (num2 <= num3) {
SmallestNumber = num2;
} else {
SmallestNumber = num3;
}
}
System.out.println("The Smallest number is " + SmallestNumber);
}
}
Output
The Smallest number is 66799
The switch selection statement
The switch statement is used to for multiple statement for one condition.The switch statement works with byte, short, int, long, enum, etc... Each value in a switch statement is called a case, and the variable being swaps after analyzing for each case.
Syntax
switch(test_expression){
case Statement1:
//code to be executed;
break; //optional
case Statement2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Example
public class Switch {
public static void main(String[] args)
{
int weekday = 4;
String weekdayString;
switch (weekday) {
case 1:
weekdayString = "Today is Monday";
break;
case 2:
weekdayString = "Today is Tuesday";
break;
case 3:
weekdayString = "Today is Wednesday";
break;
case 4:
weekdayString = "Today is Thursday";
break;
case 5:
weekdayString = "Today is Friday";
break;
case 6:
weekdayString = "Today is Saturday";
break;
case 7:
weekdayString = "Today is Sunday";
break;
default:
weekdayString = "Invalid day";
break;
}
System.out.println(weekdayString);
}
}
Output
Today is Thursday