Sei sulla pagina 1di 13

Java Unary Operator Example: ++ and --

class OperatorExample{
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
Java Unary Operator Example 2: ++ and --

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21

}}

Output:

22
21

Java Unary Operator Example: ~ and !

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a);//-11 (minus of total positive value which starts from 0)
System.out.println(~b);//9 (positive of total minus, positive starts from 0)
System.out.println(!c);//false (opposite of boolean value)
System.out.println(!d);//true
}}
Output:

-11
9
false
true
Java Arithmetic Operator Example: Expression

class OperatorExample{
public static void main(String args[]){
System.out.println(10*10/5+3-1*4/2);
}}

Output:

21

Java Shift Operator Example: Left Shift

class OperatorExample{
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

Java Shift Operator Example: Right Shift

class OperatorExample{
public static void main(String args[]){
System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
}}

Output:
2
5
2

Java AND Operator Example: Logical && and Bitwise &

The logical && operator doesn't check second condition if first condition is false. It checks
second condition only if first one is true.

The bitwise & operator always checks both conditions whether first condition is true or false.

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}}

Output:

false
false

Java OR Operator Example: Logical || and Bitwise |

The logical || operator doesn't check second condition if first condition is true. It checks second
condition only if first one is false.

The bitwise | operator always checks both conditions whether first condition is true or false.

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a>b||a<c);//true || true = true
System.out.println(a>b|a<c);//true | true = true
//|| vs |
System.out.println(a>b||a++<c);//true || true = true
System.out.println(a);//10 because second condition is not checked
System.out.println(a>b|a++<c);//true | true = true
System.out.println(a);//11 because second condition is checked
}}
Output:

true
true

Java Ternary Operator Example

class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}

Output:

Java Assignment Operator Example

class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;//10+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}

Output:

13
9
18
9

Java 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

 if statement
 if-else statement
 nested if statement
 if-else-if ladder
Example:

public class IfElseExample {


public static void main(String[] args) {
int number=13;
if(number%2==0){
System.out.println("even number");
}else{
System.out.println("odd number");
}
}
}

Output:

odd number

if-else – example:-
Example:

public class IfElseIfExample {


public static void main(String[] args) {
int marks=65;

if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else{
System.out.println("Invalid!");
}
}
}

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:

switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......

default:
code to be executed if all cases are not matched;
}
Example:

public class SwitchExample


{
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
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.

 Simple For Loop


 For-each or Enhanced For Loop
 Labeled For Loop
Example:

public class ForExample {


public static void main(String[] args) {
for(int i=1;i<=5;i++){
System.out.println(i);
}
}
}

Output:

1
2
3
4
5
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:

for(Type var:array){
//code to be executed
}

Example:

public class ForEachExample {


public static void main(String[] args) {
int arr[]={12,23,44,56,78};
for(int i:arr){
System.out.println(i);
}
}
}

Output:

12
23
44
56
78

Java Infinitive For Loop

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

Syntax:

for(;;){
//code to be executed
}

Example:

public class ForExample {


public static void main(String[] args) {
for(;;){
System.out.println("infinitive loop");
}
}
}

Output:

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

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

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.

Example:

public class WhileExample {


public static void main(String[] args) {
int i=1;
while(i<=6){
System.out.println(i);
i++;
}
}
}

Output:

1
2
3
4
5
6
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.
Example:

public class DoWhileExample {


public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=5);
}
}

Output:

1
2
3
4
5

Java Break Statement with Loop

Example:

public class BreakExample {


public static void main(String[] args) {
for(int i=1;i<=10;i++){
if(i==5){
break;
}
System.out.println(i);
}
}
}

Output:

1
2
3
4

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.

public class ContinueExample {


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

Output:
1
2
3
4
6

class FibonacciExample1
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed


{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

}}

Output:

0 1 1 2 3 5 8 13 21 34

The fibonacci series program in java using recursion.

class FibonacciExample2{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
printFibonacci(count-2);//n-2 because 2 numbers are already printed
}
}

Output:

0 1 1 2 3 5 8 13 21 34

Note: 0 and 1 are not prime numbers. The 2 is the only even prime number because all the other
even numbers can be divided by 2.

The prime number program in java. In this java program, we will take a number variable and
check whether the number is prime or not.

class PrimeExample{
public static void main(String args[]){
int i,m=0,flag=0;
int n=17;//it is the number to be checked
m=n/2;
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
System.out.println("Number is prime");
}
}

Output:

Number is prime

Let's see the factorial Program using loop in java.

class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Output:

Factorial of 5 is: 120

Potrebbero piacerti anche