Operators, Conditional and Logical Statements and Diff Between c and Java
Operators, Conditional and Logical Statements and Diff Between c and Java
- 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
TATA PUBLICATIONS
BCA
(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:
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
T
F
F
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