Java notes

Java 1st notes : click here

Java 2nd notes: click here 

Java 3rd notes: click here

Java 4th notes 👇👇👇

Pdf 👇👇👇


Control statements

Control statements are of three types They are

1.Conditional / decision for selection statement

2. Looping / iterative / repetitive statement they are also called as pre and post conditional checking statement

3. Jumping statement

1. Conditional statements (if, if else, else if, nested if)

if syntax: if(condition)

{ True block statement;

}

Statement x;


Example:

import java.util.Scanner;

//checking number +,-,0

public class ifDemo{

public static void main(String[] args) {

Scanner sc= new Scanner(System.in);

System.out.println("Enter a value ");

int n=sc.nextInt();

if(n>0)

System.out.println("Positive number!!!");

if (n<0)

System.out.println("Negative number");

if(n==0)

System.out.println("Given number is zero");

System.out.println("Demo on if statement");

sc.close();

}}


2.If else syntax:

 

if(condition)

{ True block statement

}

else

{ False block statement

}

Statement x;


Example

Write a program to check given number is even or odd.


import java.util.Scanner;

public class ifelseDemo {

public static void main(String[] args) {

Scanner sc= new Scanner(System.in);

System.out.println("Enter any number ");

int n=sc.nextInt();

if(n%2==0)

System.out.println("Even number");

else

System.out.println("Odd number");

System.out.println("Demo on if else");

sc.close();

}}


3. Else if


Else if ladder is ment for checking more than one condition

Syntax: if(condition)

{ Statement 1;

}

else if(condition 2)

{ Statement 2;

}

else if(Condition 3)

{ Statement 3;

}

else

Statement n;


Example

import java.util.Scanner;

public class check_number {

public static void main(String[] args) {

double a,b;

Scanner sc= new Scanner(System.in);

System.out.println("Enter a value");

a=sc.nextDouble();

System.out.println("Enter b value");

b=sc.nextDouble();

if(a>b)

System.out.println("a is greater then b");

else if(b>a)

System.out.println("b is greater than a ");

else

System.out.println("a is equal to b");

sc.close();

}}


4. Nested if


• If statement present in another if is called nested if.

Syntax:

if(condition 1){

if(condition 2)

{ Statement 1;

}

else {

Statement 2;

}

}

else {

Statement 3;

}


Example

Write a program to check whether a person is eligible to donate blood or not where age >18 and weight > 60.

import java.util.*;

class blooddonation

{ public static void main (String args[])

{ int age,wt;

System.out.println("Enter your age and weight");

Scanner sc=new Scanner (System.in);

age=sc.nextInt();

wt=sc.nextInt();

if(age>18){

if(wt>60)

System.out.println("Eligible for bood donation");

else{

int wtgain=60-wt;

System.out.println("Please gain"+wtgain+"kg");

}

}

else{

int agegain=18-age;

System.out.println("Please wait for"+agegain"+"year");

}

System.out.println("Demo on nested if);

} } }


4. Switch

Switch is also a multi condition checking statement


Syntax:

switch(option)

{ case label 1:

Statement 1; break;

}

{ case label 2:

Statement 2; break;

}

default: statement ; break;


Rules

• option cannot be a float value

• case label cannot be repeated

• case label can be an expression

• default is optional

• no statement can be placed between case label


Example 1

import java.util.*;

class switchdemo{

public static void main (String args[]){

int n;

Scanner sc=new Scanner (System.in);

System.out.println("enter a value between 0 and 4");

n=sc.nextInt();

switch(n){

case 1: System.out.println("one"); break;

case 2: System.out.println("two"); break;

case 3: System.out.println("three"); break;

default: System.out.println("out of range");

break;

}}}


2. Write a program to perform basic calculator for performing arithmetic operations on supplied two integers.

 

import java.util.*;

class calculator{

public static void main (String args[]){

int a,b;

Scanner sc=new Scanner (System.in);

System.out.println("enter 2 values");

a=sc.nextInt();

b=sc.nextInt();

System.out.println("enter any operator");

String ch=sc.next();

switch(ch){

case "+": System.out.println(a+b); break;

case "-": System.out.println(a-b); break;

case "*": System.out.println(a*b); break;

case "/": System.out.println(a/b); break;

case"%": System.out.println(a%b);break;

}}}


3. Program to check given character is vovel or not


import java.util.*;

class vovel{

public static void main (String args[]){

string ch;

Scanner sc=new Scanner (System.in);

System.out.println("enter any character");

ch=sc.next();

switch(ch){

case "a" : case "e" : case "i": case "o":

case"u": System.out.println("vovel"); break;

default : System.out.println("consonent");

}}}