3-Intro To Java Constructs
3-Intro To Java Constructs
PROGRAMMING
CONSTRUCTS
• Lexical Issues
• Data Types
• Variable
Topics • Operators
• Control & Looping Constructs
• Enhance For Loop
JAVA - Atomic Elements
Whitespac
Identifiers Literals Comments
e
Average = 74.71428571428571
Compilation Error
Character
• Unicode to represent all the
characters in natural languages
• 16 bits
• Range: 0 to 65,536
A
C
67
Output
Arithmetic Operators
Operato Meaning Example Result Expansio
r n
+= Addition n = 10 11 n=n+1
Augmented Assignment n += 1
(compound)
Assignment Operators
-= Subtraction n = 10 9 n=n-1
• Arithmetic Assignment n -= 1
operators (+, -, *, /,
%) are combined *= Multiplicati n = 10 20 n=n*1
with assignment on n *= 2
(=) operator Assignment
/= Division n = 10 2 n=n/1
Assignment n /= 5
%= Modulus n = 10 1 n=n%1
Assignment n %= 3
Operator Meaning Example Result Expansion
++var Pre increment n = 10 11 n=n+1
n = ++n n=n
True & False are non-numeric values; not equal to zero or one or non-zero like in C & C+
+
Boolean Logical Operator
Operator Meaning Example Result
A = true, B = false
& Logical AND A&B False
| Logical OR A|B True
^ Logical XOR A^B True
|| Short Circuit OR A || B True
&& Short circuit AND A && B False
! Logical Unary NOT !A False
X = true; Y = true
!X&!Y | X^Y|!Y ----> ?
Syntax
• variable = value;
Assignment • variable = expression;
Operator
Example
• w = 2;
• w = 3 + 6 – 1;
Ternary (three) operator
Syntax
Expression1 ? Expression2 : Expression3;
Expression1 is true Expression2 will be
executed Else, Expression3 will be
executed
The ?
Operator
Final Mark:49
++ (postfix), --(postfix), ++(prefix), --(prefix),
~, !, +(unary), -(unary)
Int a = 10, b = 2, c = 20;
*, /, %
Int t = a + b * c
+, -, >>, >>>, <<
>, >=, <, <=
Ans: 240 ? 50 ?
==, !=
&, |, ^, &&, ||, ?:, Ans: 50
Operator Precedence
Parenthesis
++ (postfix), --(postfix), +
+(prefix), --(prefix), Int a = 10, b = 2, c =
~, !, +(unary), -(unary) 20;
*, /, % Int t = (a + b) * c
+, -, >>, >>>, <<
>, >=, <, <= Ans: 240 ? 50 ?
==, != Ans: 240 HOW ???
&, |, ^, &&, ||, ?:,
Operator Precedence
DIY
• Write a Java program to calculate the area of a rectangle
• Create a program that converts temperature from Celsius to Fahrenheit
and vice versa
• Write a program that checks whether a given number is positive or
negative using the ternary operator
DIY - 2
• Implement a program that determines if a person is eligible to vote based
on their age.
• Develop a program that compares two numbers and prints whether the
first number is greater, smaller, or equal to the second number.
• Create a program to find the maximum of three numbers using the ternary
operator.