Java concepts-3
---------------
1. Comparative operators
< -less than
> -greater than
<= less than or equals
>= greaterthan or equals
AND or &&
OR or ||
NOT EQULS TO or !=
.equals() or ==
2. Conditinal statements:
To execute particular block of statement based on given condition true/false
i. If condition
ii. else if condition
iii. Nested if
iv. switch case
i. if Condition:
To execute particular block of statement when given expression is true
Syntax:
if (condition){
------
------//statements
}
ex:
int a= 10;
int b= 20;
if (a < b){
System.out.println("a- is smaller than b");
}
ii. else if condition:
using Else If we can execute different block of statements based on given expression/condition is true or false
syntax:
if (condition)
{
-----------
-----------//statements
}
else
{
-----------
-----------//statements
}
Exp: Write program to find smaller value in given 2 numbers
script:
int a= 30;
int b= 20;
if (a < b){
System.out.println("a- is smaller than b");
}
else{
System.out.println("b-is smaller than a");
}
3. Nested if:
To check more than one condition in a block of statement
Syntax:
if (condition-1){
----------------
----------------//Statements-1
}
else if (condition-2){
-------------
-------------//statements-2
}
else{
-------------
-------------//statements-3
}
Exp: write program to find smaller value in given 3 numbers
script:
int a= 30;
int b= 20;
int c= 10;
if ((a < b) && (a < c)){
System.out.println("smaller value is: "+a);
}
else if (b < c){
System.out.println("smaller value is: "+b);
}
else{
System.out.println("Smaller value is: "+c);
}
Exp: write program to find student grade based on given marks
Script:
int stMarks;
String stGrade;
System.out.println("Enter student marks");
//to read student marks from console
Scanner sc= new Scanner(System.in);
stMarks= sc.nextInt();
if (stMarks >= 90){
stGrade="A";
}
else if (stMarks >= 80){
stGrade ="B";
}
else if(stMarks >= 70){
stGrade ="C";
}
else{
stGrade ="D";
}
System.out.println("Student marks are: "+stMarks);
System.out.println("Student Grade is: "+stGrade);
4. Switch statement:
It is similar to nested if, but it will increase reliability of the script
Syntax:
switch(expression)
{
case (condition-1):
-------
-------//statements-1
break;
case (condition-2):
--------
--------//statements-2
break;
case (condition-3):
--------
--------//statements-3
break;
default:
--------
--------//statements-4
}
Exp: Write program to display color name based on given character from "RGB"
script:
System.out.println("Enter any one character from -RGB");
Scanner sc= new Scanner(System.in);
String myColor=sc.nextLine();
switch(myColor.toUpperCase()){
case "R":
System.out.println("Your choice is: RED");
break;
case "G":
System.out.println("Your choice is: GREEN");
break;
case "B":
System.out.println("Your choice is: BLUE");
break;
default:
System.out.println("Invalid entity");
}
========================================================================
---------------
1. Comparative operators
< -less than
> -greater than
<= less than or equals
>= greaterthan or equals
AND or &&
OR or ||
NOT EQULS TO or !=
.equals() or ==
2. Conditinal statements:
To execute particular block of statement based on given condition true/false
i. If condition
ii. else if condition
iii. Nested if
iv. switch case
i. if Condition:
To execute particular block of statement when given expression is true
Syntax:
if (condition){
------
------//statements
}
ex:
int a= 10;
int b= 20;
if (a < b){
System.out.println("a- is smaller than b");
}
ii. else if condition:
using Else If we can execute different block of statements based on given expression/condition is true or false
syntax:
if (condition)
{
-----------
-----------//statements
}
else
{
-----------
-----------//statements
}
Exp: Write program to find smaller value in given 2 numbers
script:
int a= 30;
int b= 20;
if (a < b){
System.out.println("a- is smaller than b");
}
else{
System.out.println("b-is smaller than a");
}
3. Nested if:
To check more than one condition in a block of statement
Syntax:
if (condition-1){
----------------
----------------//Statements-1
}
else if (condition-2){
-------------
-------------//statements-2
}
else{
-------------
-------------//statements-3
}
Exp: write program to find smaller value in given 3 numbers
script:
int a= 30;
int b= 20;
int c= 10;
if ((a < b) && (a < c)){
System.out.println("smaller value is: "+a);
}
else if (b < c){
System.out.println("smaller value is: "+b);
}
else{
System.out.println("Smaller value is: "+c);
}
Exp: write program to find student grade based on given marks
Script:
int stMarks;
String stGrade;
System.out.println("Enter student marks");
//to read student marks from console
Scanner sc= new Scanner(System.in);
stMarks= sc.nextInt();
if (stMarks >= 90){
stGrade="A";
}
else if (stMarks >= 80){
stGrade ="B";
}
else if(stMarks >= 70){
stGrade ="C";
}
else{
stGrade ="D";
}
System.out.println("Student marks are: "+stMarks);
System.out.println("Student Grade is: "+stGrade);
4. Switch statement:
It is similar to nested if, but it will increase reliability of the script
Syntax:
switch(expression)
{
case (condition-1):
-------
-------//statements-1
break;
case (condition-2):
--------
--------//statements-2
break;
case (condition-3):
--------
--------//statements-3
break;
default:
--------
--------//statements-4
}
Exp: Write program to display color name based on given character from "RGB"
script:
System.out.println("Enter any one character from -RGB");
Scanner sc= new Scanner(System.in);
String myColor=sc.nextLine();
switch(myColor.toUpperCase()){
case "R":
System.out.println("Your choice is: RED");
break;
case "G":
System.out.println("Your choice is: GREEN");
break;
case "B":
System.out.println("Your choice is: BLUE");
break;
default:
System.out.println("Invalid entity");
}
========================================================================
No comments:
Post a Comment