Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
84 views

Operators, Conditional and Logical Statements and Diff Between c and Java

Uploaded by

vaishnaviwaggi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Operators, Conditional and Logical Statements and Diff Between c and Java

Uploaded by

vaishnaviwaggi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

8CA

1.1.6 Basic Operators


constructs which can manipulate the values of the .
Operators are the 2 and 3 are operands and +is called
+ 3 =5, here
InConsider the expression
this article 2
on Java operators, the goal is to get you the expertise required
started andwork with operators in Java.
operator
operators:
Java supportsthe following types of
1. Arithmetic Operators in Java
mathematical operations
(Arithmetic Operators are used to perform for the below table.) likee
subtraction, etc. Assume that A = 10 and B =
20 ad ition,
Operator Description Example
+ Addition Adds values on either side of the operator A+B=30

- Subtraction
Subtracts the right-hand operator with left A-B=-10
hand operator
Multiplies values on either side of the A*B-200
*Multiplication operator
Divides left hand operand with right hand
Division A/B=0
operator
Reandr

Modulus Divides left hand operand by right hand A%B=0


operand and returns remainder
2. Assignment Operators in Java
An AssignmentOperator is an operator used to assign a new value to a variable
Assume A = 10 and B = 20 for the below table.
Operator Description Example
Assigns values from right side operands to left side operand c=a+b
It adds right operand to the left operand and assigns the result
to left operand c+=a

It subtracts right operand from the left operand and


result to left operand assigns the C-=a

It multiplies right operand with the left


result to left operand operand and assigns the c*=a

It divides left operand with the


result to left operand right operand and assigns the
c/=a
%= It takes modulus using two
left operand operands and assigns the result to c %=a
Performs exponential
assign value to the left(power) calculation on operators and
operand cA=a
8
ATO
JAVA PROGRAMMING

3. Relational Operators in Java


These operators compare the values on either side of them and decide the relation
among them. Assume A = 10 and B = 20.\
Operator Description Example
If the values of two operands are equal, then the
== (A == B) is not
condition becomes true. true
If the values of twO operands are not equal, then
condition becomes true. (A = B) is true
V
If the value of the left operand is greater than the value (a > b) is not
of right operand, then condition becomes true. true
A
If the value of the left operand is less than the value of
right operand, then condition becomes true. (a<b) is true
If the value of the left operand is greater than or equal
to the value of the right operand, then condition (a >=b) is not
true
becomes true.

If the value of the left operand is less than or equal to


<=
the value of right operand, then condition becomes (a <=b) is true
true.

4. Logical Operators in Java)


The following are the Logical operators present in Java
Operator Description Example
&& (and) True if both the operands is true a<10&& a<20

||(or) True if either of the operands is true a<10 | | a<20


!(not) True if an operand is false (complements the operand) I(x<10 && a<20)
5. Unary Operator in Java
Unary operators are the one that needs a single operand and are used to increment
a value, decrement or negate a value.)
Operator Description Example
++ incremernts the value by 1. There is post-increment and pre a++ and ++a
increment operators
decrements the value by 1.There is post decrement and pre a- or -a
decrement operators
invert a boolean value

TATA PUBLICATIONS
BCA

6. Bitwise Operator in Java


(Bitwise operations directly manipulate bits. In all computers, numbers a
represented with bits, a series of zeros and ones]1n fact, pretty much everything in
computer is represented by bits. Assume that A- 10 and B=20 for the below tabl
Operator Description Example
&(AND) returns bit by bit ANDof input a&b
I(OR) returns COR of input values a|b
A(XOR) returns XOR of input values a^b
-(Complement) returns the one's complement. (all bits reversed)
7. Ternary Operators in Java
The ternary operator is aconditional operator that decreases the length of code
while performing comparisons and conditionals. This method is an alternative for using
if-else and nested if-else statements. The order of execution for this operator is from left
to right.
Syntax:
(Condition) ? (Statement1) : (Statement2);go la
Condition: It is the expression to be evaluated which returns a boolean value.
Statement 1: It is the statement to be executed if the condition results ina true state.
Statement 2: It is the statement to be executed if the condition results inafalse state.
8. Shift Operators in Java
Shift operators are used to shift the bits of a number left or
right, thereby
multiplying dividing the number. There are three different types of
or
shift operaters
namely left shift operator()<<, signed right operator(>>) and
unsigned right shit
operator(>>>).
Syntax:
number shift_op number of places _to_shift;
10
JAVA PROGRAMMING

1.1.7 Conditional and Logical Statements


In lava, la statement is an executable instruction that tells the compiler what to
nerform) It forms a complete command to be executed and can include one or more
expressions. Asentence forms a complete idea that can include one or more clauses.
Control Statements

(Tava compiler executes the code from top to bottom.)The statenments in the code
are executed according to the order in which they appear, However, Java provides
lstatements that can be used to control the flow of Java code. Such statements are called
control flow statements) It is one of the fundamental features of Java, which provides a
smooth flow of program.
Conditional or Selection Statementsdaels adlbold s
1if Statement
2 if-else statement
3 if-else-if statement

4switch statement
1. If Statement:
In Java, the "if" statement is used to evaluate a
condition.) The control of the
program is diverted depending upon the specific condition.(The condition of the If
statement gives a Boolean value, either true or false. )In Java,
there are four types of if
statements given below.
It is the most basic statement among all control flow
a Boolean expression and enables the statementsin Javal It evaluates
program to enter a block of code if the expression
evaluates to true.)
Syntax of if statement is given below.
if(condition) {
statement 1; l/executes when condition is true
)Example
public classStudent{
public static void
int x = 10; main(String[] args)
TATA PUBLICATIONS
X+yOutput:
System.out.printin("'x+y Example. statement else statement
nt public Syntax:
if(condition) {evaluated ifelse 2. Output:
false)asstatement System.ot lnty12
System.out.
else }( if(x+y int ipublic ot X*yis
y x code,
is = =
ater 10)
< 12: 10;static class The greater
2; 1; ie,ifelse
printin("x {
voiStudent
d /lexecutes //executes printn(xyin
than else than
20 main(Stringll statement
block.
+y { 2
when when 20)
is is
greater less condition greater
condition The is
than args) else an
than extension than
{ is is block
20"); 10"); false true
20')
is to
executed the
if-statement,
if
the
condition
which
uses
of
the
another
if-block
block
s
A
Delhi
Output:
System.out.printin(city);:
System.out.println("city
}elseagra");
{ is }else if(city"Delhi";
String
city= public public }
statement else( statement } eneate a 3.
System.out.printin('
noida");
System.out.printin("city
city is}elsemeerut"); is Example. statement
if(conditionSrntax
1) statements.)
{condition
if(city if == static class if-else-if
The
(city ofdecision statement
if-else-if
==
"Meerut") 2; 2; 1;
== voidStudent llexecutes l/executes //executes if-else-if true.is In
"Agra") "Noida")
other
{
main(String] tree
{ statement We
{ { when when when statement
can words,
where
all condition condition chain.also
args) the is the wethe
conditions given define contains
{ program can
2
is is1 an say
true below.e true else
are that
may
false statementif-statement
at it
enter is
the
in
chain
taode the thefollowedby
endof block of
aonbd if-else
of JAVA
the
code multiple
statements else-ifPROGRAMMING
at
where

13 the that
EXAMPLE 5 4 3. 2. 1. 4. BCA
14
publicpublic (expression){
switch ifcontains
default: case value1:
case statements.variable
statement;
default statementN;
break; valueN: statement1;
break;
The thWhile
e expression. Cases since The Points Statement:
Switch
In
static class ItBreak Default
is case supported
syntax same optional, case
which
multiple Java,
voldStudent using statement cannot to
type statement
executed is variables be It is Switch
String] also
mplements
to switch It noted beingblocks ch
use as if is be enhances statements
th e terminates
not optional. duplicateversion
the
statements, used, can about switched,
variable. of
args) switch be code
Cloneable next 7 the
ofint, switch
{ statement the
However, Javashort, readability Thecalled are
similar
wecase switch whenany statement:e switch cases
must is
executed. block byte, to
is it statement and
if-elseofstatemer
ofif-else-if
given willnotice the of char, the
when a
also that program.
or single
below.
bethe the enumeration. statements.
is
a doesn't easier case
constant case condition is
to
executed
expression The
match
use
value. is String instead switch
satisfied the based
the on
will type
value
be is
JAVA PROGRAMMING

int num=2:
switch (numX
case 0:

System.out. printin("number is 0");


break;
case 1:
System.out.printin("number is 1"):
break;
default:
System.out.printin(num);

Output:

While using switch statements, we must notice that the case expression will be of
value. The switch
the same type as the variable. However, it will also be a constant
permits only int, string, and Enum type variables to be used.
Logical Ststements
relational
(Logical operators are generally used for combining two or more the
statements. They return Boolean values) The logical operators are used primarily inand
evaluation
expression evaluation to make a decision. These operators allow the
manipulation of specific bits within the integer.
Ie's also called Boolean
The Java Logical Operators work on the Boolean operand. as a
return Boolean values
logical operators.(It operates on two Boolean values, which
gives false, and if
result.... It reverses the value of operands, if the value is true, then it
it is false, then it gives true)
Basic Logical Operations
1. Negation: It means the opposite of the original statement. If pis
a statement
as 'it is not the case that p.' So,
then the negation of p is denoted by ~pand read
ifp is true then ~pis false and vice versa.)
~ p is Paris is not in France.
Example: If statement p is Paris is in France, then
15
TATA PUBLICATIONS
BCA

two statements. If P, q are two sta.


(Conjunction: It means Anding of denoted by p " qand
2.
conjunction
then "p and ofq" iscompound
p and statement,
refer ed
as th
a q. The conjunction of pand qis true only when both pan,

q are true. Otherwise, it is false.


pAq

T
F

3. (Disjunction: It means Oring of two statements. If p, q are two statements, the


"p or q" is a compound statement, denoted by p (" q and referred to as k
disjunction of pand q. The disjunction of pand qis true whenever at least one
the two statements is true, and it is false only when both p and q are false.)
P Bin p Vq
T
T

F
T

4. Amplication / if-then (8): An


q." It is false if p is true andq is implication pö'q is the proposition "if p,
false. The rest
cases are true,esetet
T pq
T
T
T
F
F
T
F T
F
16
CATO
JAVA PROGRAMMING
Wand Only If ( p q is
when pand qare Bame, Le., both bi-conditional logical connective which is true
are false or both are true,
P
1
T

T
Derived Connectors
1. (NAND: It means negation after ANDing of two
twopropositions Nanding of pand q to be a statements) Assume p and q be
proposition which is false when
pand qare true, otherwise true. It is denoted by both
pyq.)
P

T
pVq
T

T T
F
F
2. NOR or Joint Denial: It means negation after
pandq be two propositions( NORing of p andq ORing of two statements.)Assume
when both p and qare false, otherwise false. It isto be a proposition which is true
P
denoted by p Jq.
T
T
T

T
F

3.
XOR: Assume p and qbe twopropositions. XORing of p
or qis true but not both and and q is true if p is true
vice-versa. It is denoted by p *q.
P
P ) g
T

F F

TATA PUBLICA NS
BCA

Between Cand Java bo


1.1.8 Some Typical Differences
C vs Java
Here, are important differences between
Java
C

Cis a Procedural Programming Language. Java is an Object-Oriented language.


Cwas developed by Dennis M. Ritchie in Java language was developed by James
Gosling in 1995.
1972.
the
It is a high-level language because
It is a middle-level language as it is translation of code is taken place into machine
binding the gaps between machine level
and high-level languages. language, which uses compiler or interpreter,
In the Cdeclaration variable are declared In Java, you can declare a variable anywhere
at the beginning of the block.
Free is a variable used for freeing theo A compiler will free up the memory by calling
the garbage collector. 1
memory in C.
Cdoes not support threading. Java has a feature of threading.
Csupport pointers. Java does not support pointers.
2
Memory allocation can be done by a new
Memory allocation can be done by malloc. keyword.
Garbage collector needs to manage In Java, it is automatically managed by a
manually. garbage collector
Cdoes not have a feature of overloading Java supports method overloading.
functionality.
Coffers support for call by value and call
by reference. Java only supports a call by value.
I
The average salary for a CDeveloper is Theaverage salary for ajava developer is
$104,051 per year in the United States. $104,710 per yea.
TIOBE rating is 2 TIOBE rating is 1

You might also like