Java Basics – conditional statement, switch statement, loops, break continue statement, and operators

Conditional Statement

The Java conditional statement is also known as the Java if-else statement. It is used to test many conditions in a program. It checks the Boolean condition that is true or false. There are four types of if statements in Java are – if statement, if-else statement, if-else if ladder statement and the nested if statement.

If statement 

Java programming languages’ if statement test a condition. It is responsible for executing the if block if the given condition is true. Its Syntax is –

If(condition){

//Code executed

}

Example –

public class IfExamp {

public static void main(String[] args) {

//defining the ‘age’ variable

int age=20;

//check the age

if(age>18){

System.out.print(“Age is greater than 18”);

}

}

}

Output – Age is greater than 18

If else statement 

Another statement is the if-else statement which also tests a condition. If the condition is true, it executes the if block and if the condition is false then the else block is executed. Its Syntax is –

If(condition){

//Code if condition true

} Else{

//Code if condition false

}

Example –

public class IfElseExamp {

public static void main(String[] args) {

//defining variable

int number=13;

if(number%2==0){

System.out.println(“it is an even number”);

}else{

System.out.println(“it is an odd number”);

}

}

}

Output – It is an odd number

If else if statement

The java’s if else if ladder statement is the one that executes a condition from many statements. Its Syntax is –

if(condition1){

}else if(condition2){

}

else if(condition3){

}

else{

}

Example –

public class PositiveNegativeExamp {

public static void main(String[] args) {

int number=-13;

if(number>0){

System.out.println(“Positive”);

}else if(number<0){

System.out.println(“Negative”);

}else{

System.out.println(“Zero”);

}

}

}

Output – Negative

Nested if statement

The last conditional statement is nested if statement that represents the if block inside another if block. The important thing is that the inner if block condition executes if and only if the outer if block condition is true. Its Syntax is –

if(condition){

//code to be executed

if(condition){

//code to be executed

}

}

Example –

public class JavaNestedIfExamp {

public static void main(String[] args) {

//Creating two variables for both the age and weight

int age=20;

int weight=80;

//applying the condition on age and weight

if(age>=18){

if(weight>50){

System.out.println(“You can donate your blood”);

}

}

}}

Output – You can donate your blood

Switch Statement

Java programming language has a switch statement that executes one statement from multiple conditions that are given. It is almost similar to the if-else if ladder statement. This switch statement works with integer, long, string, byte, short, enum types, and some wrapper types such as long, byte, int, and short. We can use string also in our switch statement. In short, the switch statement basically tests the equality of one variable against multiple values. Its Syntax is –

switch(expression){

case value1:

//code to be executed;

break;  //optional

case value2:

//code to be executed;

break;  //optional

……

default:

code that is executed if all cases aren’t matched;

}

Example –

public class SwitchExamp {

public static void main(String[] args) {

int number=20;

switch(number){

case 10: System.out.println(“10”);

case 20: System.out.println(“20”);

case 30: System.out.println(“30”);

default:System.out.println(“Not in 10, 20 or 30”);

}

}

}

Output –

20

30

Not in 10, 20 or 30

Break Continue Statement

Both the break and continue statements are called the jump statements and they are used to skip a few statements inside a loop or and the loop immediately without even checking the test expression. Both of these statements can be used inside the loop such as do while, for, and while loop.

Break 

The first is the break statement that is used to terminate from the loop right away. When there is a break statement inside a loop, the loop iteration stops, and then control returns from the loop at that time to the first statement after that loop. In short, the break statements are used only in situations when the programmer is not sure about the exact number of iterations for a loop, or he wants to terminate that loop based on a condition. Its Syntax is – break;

Example –

class Break {

public static void main(String[] args)

{

// loop is set to run from the numbers 0-9

for (int i = 0; i < 10; i++) {

if (i == 5)

break;

System.out.println(“i: ” + i);

}

System.out.println(“Out of Loop”);

}

}

Output – i: 0i: 1i: 2i: 3i: 4Out of Loop

Continue

The continue statement is used to skip the existing iteration of a particular loop. We can use this statement inside any kind of loop-like for, do-while, and while loop. So, they are used in the situation where the programmer wants to continue the loop but he doesn’t want the remaining states that are after the continue statement. Its Syntax is – continue;

Example –

class Continue {

public static void main(String args[])

{

for (int i = 0; i < 10; i++) {

// If the number is 2

// skip and continue

if (i == 2)

continue;

System.out.print(i + ” “);

}

}

}

Output – 0 1 2 3 4 5 6 7 8 9

Loops

In Java language, the loops are used for executing some of the instructions as well as functions repeatedly when some of the conditions become true. There are three kinds of loops in this language that is for loop, while loop, and do-while loop.

For loop

This loop is used to iterate a certain part of the program a few times. It is recommended to the programmers to use the for loop if the number of iterations is fixed. For loop is of three types in java namely simple for loop, for each or enhanced for loop and labeled for a loop. Its Syntax is –

for(initialization;condition;incr/decr){

//statement or code to be executed

}

Example –

public class ForExamp {

public static void main(String[] args) {

//Code of Java for loop

for(int i=1;i<=10;i++){

System.out.println(i);

}

}

}

Output – 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

While loop

Do while loop is used by the programmers to iterate a specific part of the program several times. It is recommended to use this loop when the number of iteration is not fixed. Its Syntax is –

while(condition){

//code to be executed

}

Example –

public class WhileExamp {

public static void main(String[] args) {

int i=1;

while(i<=10){

System.out.println(i);

i++;

}

}

}

Output – 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Do-while loop

We can use the do-while loop in Java to iterate a part of the program several times too. It is recommended for programmers to use this loop when the number of iterations is not fixed and when they must have to execute this loop at least once in the program. This loop is executed at least once in the program because then the condition is checked after the loop body. Its Syntax is –

do{

//code to be executed

}while(condition);

Example –

public class DoWhileExamp {

public static void main(String[] args) {

int i=1;

do{

System.out.println(i);

i++;

}while(i<=10);

}

}

Output – 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Operators

The operators in Java are symbols that are used to perform certain operations. For example: +, /, *, – etc. There are a lot of operators in the Java programming language like the unary operator, arithmetic operator, relational operator, bitwise operator, assignment operator, shift operator, ternary operator, and the logical operator. Given below are the examples of all the operators that will help you understand them better.

Unary operator –

class OperatorExamp{

public static void main(String args[]){

int x=10;

System.out.println(x++);//10 (11)

System.out.println(++x);//12

System.out.println(x–);//12 (11)

System.out.println(–x);//10

}}

Output – 10,12,12,10

Arithmetic operator –

class OperatorExamp{

public static void main(String args[]){

System.out.println(10*10/5+3-1*4/2);

}}

Output – 21

Relational operator –

public class Example {

public static void main(String args[]) {

int a = 10;

int b = 20;

System.out.println(“a == b = ” + (a == b) );

System.out.println(“a != b = ” + (a != b) );

System.out.println(“a > b = ” + (a > b) );

System.out.println(“a < b = ” + (a < b) );

System.out.println(“b >= a = ” + (b >= a) );

System.out.println(“b <= a = ” + (b <= a) );

}

}

Output – a == b = falsea != b = truea > b = falsea < b = trueb >= a = trueb <= a = false

Bitwise operator –

They are used to perform the operations on the individual bits. Example –

Take the number 35. In binary, it will be – 00100011. Then converted in decimal, it will be 220.

Assignment operator –

class OperatorExample{

public static void main(String args[]){

short a=10;

short b=10;

a=(short)(a+b);//20 which is int now converted to short

System.out.println(a);

}}

Output – 20

Shift operator

This is left shift operator example.

class OperatorExamp{

public static void main(String args[]){

System.out.println(10<<2);//10*2^2=10*4=40

System.out.println(10<<3);//10*2^3=10*8=80

System.out.println(20<<2);//20*2^2=20*4=80

System.out.println(15<<4);//15*2^4=15*16=240

}}

Output – 40, 80, 80, 240

Ternary operator –

class OperatorExamp{

public static void main(String args[]){

int a=2;

int b=5;

int min=(a<b)?a:b;

System.out.println(min);

}}

Output – 2

Logical operator –

class Main {

public static void main(String[] args) {

System.out.println((5 > 3) && (8 > 5));

// true

System.out.println((5 > 3) && (8 < 5));

// false

System.out.println((5 < 3) || (8 > 5));

// true

System.out.println((5 > 3) || (8 < 5));

// true

System.out.println((5 < 3) || (8 < 5));

// false

System.out.println(!(5 == 3));

// true

System.out.println(!(5 > 3));

// false

}}

Leave a Comment