Chapter 7
Java Basics
(Part 2 – Operators in java)
Presented by nuzhat memon
Operat
ors
1.
Arithme
tic
Operato
rs
2.
Compari
son
operato
rs
3.
Logical
Operato
rs
4.
Conditio
nal
operato
rs
5.
Assignm
ent
operato
r
Chapter 7
Java Basics
(Part 2 – Operators in java)
Presented by nuzhat memon
Operator
s
1.
Arithmetic
Operators
2.
Comparison
operators
3. Logical
Operators
4.
Conditional
operators
5.
Assignment
operator
Presented by Nuzhat Memon
Operators
+ Operators are special symbols used to build an expression.
3
Operators
1.
Arithmetic
Operators
2.
Comparison
operators
3. Logical
Operators
4.
Conditional
operators
5.
Assignment
operator
Presented by Nuzhat Memon 4
Basic arithmetic
operators are +, – , *,
/ , % (Modulus)
All these operators are
binary, they take 2
operands.
Used for byte, short, int,
long, float, double and
char (treated as int)
Arithmetic Operators
Operator Meaning Eg
+ Addition 2+8
– Subtraction 2-8
* Multiplication 2*8
/ Division 8/2
% Reminder 8%3
++ & -- is known
as unary operators
++ is increment
operator (which
adds 1) and -- is
decrement
operator (which
subtracts 1)
Increment / Decrement
Operators
++x pre-increment
--x pre-decrement
x++ post -increment
x-- post-decrement
Comparison
operators are
known as
relational
operators.
The result of
expression is
boolean; either
true or false
Comparison Operators
Operator Meaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
Logical operators
known as boolean
operators.
Logical operators:
AND, OR, XOR
(Exclusive OR),
NOT
Logical Operators
Operator Sign Use
AND && Used to combine two values
OR || Used to combine two values
XOR ^
It returns true only if operands
are different
NOT !
Operant is true, result is false
& vice versa
Also known as ternary
operator.
Conditional operator
uses 3 operands.
It uses two symbols ?
and : in the expression.
Syntax : (boolean-expr)
? (expr1) : (expr2)
To assign a value to
variable by using
the assignment
operator ‘=’
Syntax: <variable>
= <expression>;
Variable on lefthand
side of expression is
lvalue (location in
memory)
Conditional
Operator
Operators
Arithmetic
Operator
Increment and
Decrement
Operator
Comparison
Operator
Logical
Operator
Assignment
Operator
Operator
Operand1 Operand2
1
4
2
4
8
0
8/2
8%2
Presented by Nuzhat Memon
If the conditional ternary is
next = (N%2==0)?(N/2):(3*N+1); where N=7 then what will be the value
of variable next?
(A) 22 (B) 7 (C) 3.5 (D) 0
5
3*N+1
N%2==0
N/2
true false
N=7
7%2==0
false
3*7+1 =22
Presented by Nuzhat Memon
While using shorthand version of assignment, it is defined as a+=b where a
is 7 and b is 8 then what will result of a ?
(A) 8 (B) 49 (C) 14 (D) 15
6
<variable>=<variable><operator><expression> <variable><operator> = <expression>;
a=a+b a+=b
a=a-b a-=b
a=a*b a*=b
a=a/b a/=b
a+=b
a=a + b
=7+ 8
Presented by Nuzhat Memon
Thanks!
Any questions?
You can find me at:
+ nuzhatmemon.com
+ nuzhat.memon@gmail.com
7
Presented by Nuzhat Memon 8
Branches/decision/selective
Control Structures
Do while loop
Looping
For loop While loop
If statement Switch case
In general, the statements are executed sequentially, one by one. Sometimes,
program logic needs to change the flow of this sequence.
The statements that enable to control the flow of execution are considered as
control structures
Control Structures
Presented by Nuzhat Memon 9
Entry controlled or pre-test
loop constructs.
It is used when numbers of
iterations are pre-defined.
Entry controlled or pre-test
loop constructs.
It is used when number of
iterations are not
pre-determined.
Exit controlled or post-test
loop constructs.
Here, statements of loop are
executed at least once.
Do while loop
Looping
For loop While loop
for (initialize; condition; iteration){
statements;
}
//initialize
while (condition){
statements;
increment/decrement statement;
}
//initialize
Do{
statements;
increment/decr statement;
} while (condition);
 Loops are used to repeat a sequence of statements over and over until some condition occurs.
 Loop are executed if condition is true.
 Java supports 3 types of looping constructs:
Looping
Presented by Nuzhat Memon
Brain Stroming Session
Which of the following is compiled error free?
(A)for(;;){int i=7}; (B) while (1) { int i=7};
(C) while (true){int i=7} (D) All of these
What will be output of following program?
class abc{
public static void main(string[] S){
for(int i=0;i<10;i++){
SYSTEM.out.println(i);
}
}
}
(A) error (B) i (C) Display 1 to 10 (D) 1,2,3,…….10
10
Presented by Nuzhat Memon
Statement (selective structure)
Control structure branches are used to choose among two or
more possible courses of action, is called as selective structure
(1) IF STATEMENT
+ Used in a program to take one of two alternative coursed of
action, depending upon Boolean valued expression.
(2) SWITCH STATEMENT
+ Used when there are many alternative actions to be taken
depending upon the value of a variable or expression.
+ Test expression should be of the type byte, char, short or int
or enum
11
if(condition){
statements;
}
Else{
statements;
}
Switch (option){
case 1:
statement1;
break;
case 2:
statement2;
break;
default:
statementN;
}
Presented by Nuzhat Memon 12
Entry controlled or pre-test
loop constructs.
It is used when numbers of
iterations are pre-defined.
Entry controlled or pre-test
loop constructs.
It is used when number of
iterations are not
pre-determined.
Exit controlled or post-test
loop constructs.
Here, statements of loop are
executed at least once.
Do while loop
Looping
For loop While loop
for (initialize; condition; iteration){
statements;
}
//initialize
while (condition){
statements;
increment/decrement statement;
}
//initialize
Do{
statements;
increment/decr statement;
} while (condition);
Control structure branches are used to choose among two or more possible
courses of action, is called as selective structure
statement
Presented by Nuzhat Memon
Which statement is executed if the value of Boolean expression is true?
If (boolean expression)
{
<statement1>
}
else
{
<statement2>
}
(A) statement2 (B) error message (C) statement1 (D) statement3
13
Presented by Nuzhat Memon
Statement
BREAK STATEMENT
+ Used to transfer the control outside switch/loop
structure.
+ It is used to exit the loop.
+ It is jumps outside the nearest loop containing
this statement.
CONTINUE STATEMENT
+ It is used to skip the following statement in a loop
and continue with the next iteration.
14
for(){
if(condition){
break;
}
statements;
}
for(){
if(condition){
continue;
}
statements;
}

Std 12 Computer Chapter 7 Java Basics (Part 2)

  • 1.
    Chapter 7 Java Basics (Part2 – Operators in java) Presented by nuzhat memon Operat ors 1. Arithme tic Operato rs 2. Compari son operato rs 3. Logical Operato rs 4. Conditio nal operato rs 5. Assignm ent operato r
  • 2.
    Chapter 7 Java Basics (Part2 – Operators in java) Presented by nuzhat memon Operator s 1. Arithmetic Operators 2. Comparison operators 3. Logical Operators 4. Conditional operators 5. Assignment operator
  • 3.
    Presented by NuzhatMemon Operators + Operators are special symbols used to build an expression. 3 Operators 1. Arithmetic Operators 2. Comparison operators 3. Logical Operators 4. Conditional operators 5. Assignment operator
  • 4.
    Presented by NuzhatMemon 4 Basic arithmetic operators are +, – , *, / , % (Modulus) All these operators are binary, they take 2 operands. Used for byte, short, int, long, float, double and char (treated as int) Arithmetic Operators Operator Meaning Eg + Addition 2+8 – Subtraction 2-8 * Multiplication 2*8 / Division 8/2 % Reminder 8%3 ++ & -- is known as unary operators ++ is increment operator (which adds 1) and -- is decrement operator (which subtracts 1) Increment / Decrement Operators ++x pre-increment --x pre-decrement x++ post -increment x-- post-decrement Comparison operators are known as relational operators. The result of expression is boolean; either true or false Comparison Operators Operator Meaning > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to Logical operators known as boolean operators. Logical operators: AND, OR, XOR (Exclusive OR), NOT Logical Operators Operator Sign Use AND && Used to combine two values OR || Used to combine two values XOR ^ It returns true only if operands are different NOT ! Operant is true, result is false & vice versa Also known as ternary operator. Conditional operator uses 3 operands. It uses two symbols ? and : in the expression. Syntax : (boolean-expr) ? (expr1) : (expr2) To assign a value to variable by using the assignment operator ‘=’ Syntax: <variable> = <expression>; Variable on lefthand side of expression is lvalue (location in memory) Conditional Operator Operators Arithmetic Operator Increment and Decrement Operator Comparison Operator Logical Operator Assignment Operator Operator Operand1 Operand2 1 4 2 4 8 0 8/2 8%2
  • 5.
    Presented by NuzhatMemon If the conditional ternary is next = (N%2==0)?(N/2):(3*N+1); where N=7 then what will be the value of variable next? (A) 22 (B) 7 (C) 3.5 (D) 0 5 3*N+1 N%2==0 N/2 true false N=7 7%2==0 false 3*7+1 =22
  • 6.
    Presented by NuzhatMemon While using shorthand version of assignment, it is defined as a+=b where a is 7 and b is 8 then what will result of a ? (A) 8 (B) 49 (C) 14 (D) 15 6 <variable>=<variable><operator><expression> <variable><operator> = <expression>; a=a+b a+=b a=a-b a-=b a=a*b a*=b a=a/b a/=b a+=b a=a + b =7+ 8
  • 7.
    Presented by NuzhatMemon Thanks! Any questions? You can find me at: + nuzhatmemon.com + nuzhat.memon@gmail.com 7
  • 8.
    Presented by NuzhatMemon 8 Branches/decision/selective Control Structures Do while loop Looping For loop While loop If statement Switch case In general, the statements are executed sequentially, one by one. Sometimes, program logic needs to change the flow of this sequence. The statements that enable to control the flow of execution are considered as control structures Control Structures
  • 9.
    Presented by NuzhatMemon 9 Entry controlled or pre-test loop constructs. It is used when numbers of iterations are pre-defined. Entry controlled or pre-test loop constructs. It is used when number of iterations are not pre-determined. Exit controlled or post-test loop constructs. Here, statements of loop are executed at least once. Do while loop Looping For loop While loop for (initialize; condition; iteration){ statements; } //initialize while (condition){ statements; increment/decrement statement; } //initialize Do{ statements; increment/decr statement; } while (condition);  Loops are used to repeat a sequence of statements over and over until some condition occurs.  Loop are executed if condition is true.  Java supports 3 types of looping constructs: Looping
  • 10.
    Presented by NuzhatMemon Brain Stroming Session Which of the following is compiled error free? (A)for(;;){int i=7}; (B) while (1) { int i=7}; (C) while (true){int i=7} (D) All of these What will be output of following program? class abc{ public static void main(string[] S){ for(int i=0;i<10;i++){ SYSTEM.out.println(i); } } } (A) error (B) i (C) Display 1 to 10 (D) 1,2,3,…….10 10
  • 11.
    Presented by NuzhatMemon Statement (selective structure) Control structure branches are used to choose among two or more possible courses of action, is called as selective structure (1) IF STATEMENT + Used in a program to take one of two alternative coursed of action, depending upon Boolean valued expression. (2) SWITCH STATEMENT + Used when there are many alternative actions to be taken depending upon the value of a variable or expression. + Test expression should be of the type byte, char, short or int or enum 11 if(condition){ statements; } Else{ statements; } Switch (option){ case 1: statement1; break; case 2: statement2; break; default: statementN; }
  • 12.
    Presented by NuzhatMemon 12 Entry controlled or pre-test loop constructs. It is used when numbers of iterations are pre-defined. Entry controlled or pre-test loop constructs. It is used when number of iterations are not pre-determined. Exit controlled or post-test loop constructs. Here, statements of loop are executed at least once. Do while loop Looping For loop While loop for (initialize; condition; iteration){ statements; } //initialize while (condition){ statements; increment/decrement statement; } //initialize Do{ statements; increment/decr statement; } while (condition); Control structure branches are used to choose among two or more possible courses of action, is called as selective structure statement
  • 13.
    Presented by NuzhatMemon Which statement is executed if the value of Boolean expression is true? If (boolean expression) { <statement1> } else { <statement2> } (A) statement2 (B) error message (C) statement1 (D) statement3 13
  • 14.
    Presented by NuzhatMemon Statement BREAK STATEMENT + Used to transfer the control outside switch/loop structure. + It is used to exit the loop. + It is jumps outside the nearest loop containing this statement. CONTINUE STATEMENT + It is used to skip the following statement in a loop and continue with the next iteration. 14 for(){ if(condition){ break; } statements; } for(){ if(condition){ continue; } statements; }