Java Operators Tutorial
Learn Operators and Selections in Java
This article will give you a basic idea of operators and how to use them. Java provides different types of operators, In this tutorial you will find some of the most frequently used operators. Here is the list…
Types of operators in java
1. Arithmetic Operators (+, - , * , / , %)2. Relational Operators (< , > , == , <= , >= , !=)
3. Logical Operators ( &&( and), ! (not), ||(or) )
4. Increment Operators (++)
a) post increment--> a++
b) pre increment---> ++a
5. Decrement Operators (--)
a) post decrement--> a--
b) pre decrement---> --a
Types of Statements.
Java program contains different types of statements.
1. Sequential Statements:
All the statements written in program, are executed sequentially one after another.
2. Control Statements:
Depending the on the condition, the statements selected for execution, and the other statements are ignored. These statements also called as decision-making statements or selection statements, which controls the logic flow based on the condition.
1. if
2. if-else
3. if - else if - else
4. ternary operator
5. switch
3. Iteration(looping) Statements:
These statements executed certain no. of times until the given condition is true. There are 4 types of looping statements available.
1. while
2. do-while
3. for
4. for-each
2. Control or Conditional Statements.
1. if Statement:
syn:
if(some condition) { .......... .......... }
logic:
if(a>b)
{
System.out.println("a is big");
}
if(b>a)
{
System.out.println("b is big");
}
2. if , else
ex:
if(a>b)
{
System.out.println("a is big");
}
else
{
System.out.println("b is big");
}
3. if, else..if and else
if(condition1) { .......... .......... } else if(condition2) { .......... .......... }
else if(condition3) { .......... .......... }
else
{
..........
..........
}
program1:
Find the greatest of three numbers
public class IFElsedemo { public static void main(String[] args) {
int num1;
int num2;
int num3;
int greatest;
if (num1 >= num2 && num1 >= num3) { greatest = num1; }
else if (num2 >= num1 && num2 >= num3) { greatest = num2; }
else { greatest = num3; }
System.out.println("Greatest Number = " + gratest);
}
}
** if block contain single statement no need to use { }, but better if we use.
** if statement doesn't end with ;
** if always checks for whether a condition is true or not, if true it executes the if block, otherwise i.e. the condition is false it fallows next statement of the if.
program2:
Logic: if(tot>=500)
{
System.out.println("excellent");
}
else if(tot>=400 && tot<500)
{
System.out.println("good");
}
else if(tot>=300 && tot<400)
{
System.out.println("average");
}
else
{
System.out.println("fail");
}
Find the grade
class Findgrade { public static void main(String args[]) { int m1; int m2; int m3; int m4; m1=68; m2=87; m3=67; m4=90; int tot; tot=(m1 + m2 + m3 + m4); if(tot >= 500) { System.out.println("excellent"); } else if(tot >= 400 && tot < 500) { System.out.println("good"); } else if(tot >= 300 && tot < 400) { System.out.println("average"); } else { System.out.println("fail"); } { System.out.println(tot); } } }
Try:
-- find a given number odd or even
-- find the youngest of 3 people.
-- find age groups with a given age (elder, middle, younger..etc)
-- find a vowel or consonant of a given character.
-- find the discount of bill amount by given bill value.
-- check a given year is leap year or not
4. Ternary Operator:
Ternary statement is similar to if…else. Its performance is better compare to if..else. It uses 2 symbols ?, : .
syn:
(condition) ? Statement1 : Statement2
ex:
c = (a>b) ? a : b;
if a is greater than b, then it returns a. else, it returns b.
code example: public class Ternary{ public static void main(String args[]){ int a=15; int b=6; int c=0; c=(a>b)? a : b ; System.out.println(c); }
}
5. Switch : It is also called as multi-conditional branching statement.
syn:
switch(choice variable)
{ // switch block open
case 1: // caseoption1 statement; ..... ..... break; case 2: // caseoption2 statement/s; break; case 3: // caseoption3 statements; break; default: // default option statement
} // swith end
logic
switch(num) { 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("Wrong choice");
}
code example:
public class Switchdemo{
public static void main(String args[]){
int choice=4;
switch(choice)
{
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("Wrong choice");
}
}
}
Try Out:
1. write a program to find simple interest.
formula si=(pnr)/100;
p(principal amount)
n(no of years)
r(rate of interest)
2. find whether a person is eligible to vote or not, by checking his age.
3. write a switch program for following menu.
1. Addition
2. Subtraction
3. Multiplication
4. Exit.
Jave Practice programs on switch case
--switch--with number input, with char input, with string input
-- print equivalent of char form digit Ex: if chosen value is 1, it should print "One" and so on.
-- print a given char vowel or consonant using switch case
-- print favorite color using Switch Statement.
-- Write a menu program using Switch Statement
-- print a week day of a digit Ex: if user inputs a digit 1, It prints "it is Monday"…etc.
Comments
Post a Comment