Features of Programming
Language Fundamental
LESSON OUTLINES
TOPICS TO BE COVERED
Data types
Variables
Operators
Conditional Statements
Control Statements(loops)
Arrays
2
Data Type
Types of data that want to memorize in computer
memory
Data types are used to allocate memory and specify
what type of data using in the program.
All the data types are keywords which are having
built-in meaning.
3
Data Type
Data types in programming
1. int : which is used to store integer value and it will not
accept any decimal value. It allocates 4 bytes of memory.
What is programing ?
2.long: which is used to store integer value and it will not
accept any decimal value. It allocates 8 bytes of memory
3.float: which is used to store decimal value. It allocates 4
bytes of memory.
4.double: which is used to store decimal value. It allocates 8
bytes of memory.
4
Data Type
5. char: which is used to store a single character and it
uses to take only one byte.
What is programing ?
6. Short: which use to take 2 bytes of memory and stores
an integer value.
7. Boolean: which takes only true or false
8. Byte: which takes only byte memory
String: is not a data type but it is a predefined class.
NOTE
It allows storing sets of characters.
5
Data Type
? How many types of data? What are they?
Depending on data structures and storage styles in
What is programing ?
computer memory, categorized data type into two
groups:
1. Primitive data types/ Value Types
2. Reference types/Non-primitive data types
6
Primitive Data Type
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
Stores whole numbers from -2,147,483,648 to
What is programing ?
int 4 bytes
2,147,483,647
Stores whole numbers from -
long 8 bytes 9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
Stores fractional numbers. Sufficient for storing
float 4 bytes
6 to 7 decimal digits
Stores fractional numbers. Sufficient for storing
double 8 bytes
15 decimal digits
boolean 1 bit Stores true or false values 0 or 1
char 2 bytes Stores a single character/letter or ASCII values
7
Non Primitive Data Type
Non-primitive data types are called reference types because they refer to objects.
E.g. String, Array, etc.
The main difference between primitive and non-primitive data types are:
Primitive types are predefined (already defined).
Non-primitive types are created by the programmer and is not defined by Java
(except for String).
Non-primitive types can be used to call methods to perform certain
operations, while primitive types cannot.
A primitive type has always a value, while non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types starts
with an uppercase letter.
8
Variable Is a named of location that can store a value.
It is nothing but a memory location name by which
can refer your data.
Values may be number, text, and other types of
data.
Declaration and initialization of a variable
Syntax:
datatype variable_name=value
Example:
int a=10;
int b=20;
int c=a+b;
9
Variable Rules to follow for declaring the variable name:
1.Should start with lower case and the first letter of the
second word should start with a capital letter. i.e. first
name, orderNumber
2.Can start with _ (underscore)
3.Can start with $
4.The first letter cannot be a capital letter
5.It can contain numbers but not in the middle
6.No other special symbols allowed
Legal variable names:
Illegal variable names:
myvar = "John"
my_var = "John" 2myvar = "John"
_my_var = "John" my-var = "John"
myVar = "John" my var = "John"
myvar2 = "John"
10
public class JavaDataTypes {
public static void main(String[] args) {
Int a,b,c;
Example of
a=10;
data types
b=20;
and using
c=a+b;
variables
System.out.println(“Addition of a and b is ” +c);
}
}
11
Operators
Operators are symbols that tell the compiler to perform specific
mathematical or logical manipulations.
Arithmetic Operators:
Symbol Operation Usage Explanation
+ addition x+y Adds values on either side of the operator
Subtracts the right hand operand from the left hand
- subtraction x-y
operand
* multiplication x*y Multiplies values on either side of the operator
Divides the left hand operand by the right hand
/ division x/y
operand
Divides the left hand operand by the right hand
% modulus x%y
operand and returns remainder
12
Operators
Relational Operators:
These operators are used for comparison. They return either true or false based on the comparison result.
The operator '==' should not be confused with '='.
Symbol Operation Usage Explanation
Checks if the values of two operands are equal or not,
== equal x == y
if yes then condition becomes true.
Checks if the values of two operands are equal or not,
!= not equal x != y
if values are not equal then condition becomes true.
Checks if the value of the left operand is greater than
> greater than x>y the value of the right operand, if yes then condition
becomes true
Checks if the value of the left operand is less than the
< less than x<y value of the right operand, if yes then condition
becomes true.
Checks if the value of the left operand is greater than
>= greater than or equal x >= y or equal to the value of the right operand, if yes then
condition becomes true.
Checks if the value of the left operand is less than or
<= less than or equal x <= y equal to the value of the right operand, if yes then
condition becomes true.
13
Operators
Logical Operators:
These operators take boolean values as input and return boolean values as output.
Symbol Operation Usage Explanation
&& logical AND x && y Returns true if both x and y are true else returns false.
|| logical OR x || y Returns false if neither x nor y is true else returns true
! logical NOT !x Unary operator. Returns true if x is false else returns false.
14
Operators
Assignment Operators:
Symbol Operation Usage Equivalence Explanation
Assigns value from the right side operand(s) to
= assignment x=y
the left side operand.
Adds the right side operand to the left side
+= add and assignment x += y x = x+y operand and assigns the result to the left side
operand.
Subtracts the right side operand from the left side
-= subtract and assignment x -= y x= x-y operand and assigns the result to the left side
operand.
Multiplies the right side operand with the left side
*= multiply and assignment x *= y x= x*y operand and assigns the result to the left side
operand.
Divides the left side operand with the right side
/= divide and assignment x /= y x= x/y operand and assigns the result to the left side
operand.
Takes modulus using the two operands and
%= modulus and assignment x%=y x= x%y
assigns the result to the left side operand.
15
Conditional Statements
Conditional statements: are used to execute a set of statements based on condition
satisfaction.
1.if
2.else
3.else if
4.nested if
1 if
write one condition and execute a set of statements based on the condition.
Statements under if will be executed only if the condition is true.
Syntax:
if(condition) x =100
{ if(x > 10){
statements; System.out.println(x+ "is greater than 10");
}
16 }
Conditional Statements
2 If-else
It’s the same as if but will one additional else.
It will execute only if the condition is true but do not have any else statement.
public class Student {
syntax: public static void main(String[] args) {
if(condition) { int x = 10;
int y = 12;
statement 1; //executes when condition is true
if(x+y < 10) {
}
System.out.println("x + y is less than 10");
else{ }
statement 2; //executes when condition is false else {
} System.out.println("x + y is greater than 20");
}
}
17
}
Conditional Statements
3 Else if
The if-else-if statement contains the if-statement followed by multiple else-if statements.
Using if and else is able to check for two conditions only.
public class Student {
Syntax:
public static void main(String[] args) {
if(condition 1) {
Int time= 13;
statement 1; //executes when condition 1 is true
if (time < 10) {
}
System.out.println(“Good Morning");
else if(condition 2) {
} else if (time < 20) {
statement 2; //executes when condition 2 is true
System.out.println(“Good Day"); }
}
else {
else {
System.out.println(“Good Evening");
statement 2; //executes when all the conditions are false
}
}
}
18
}
Conditional Statements
4 Nested if
In nested if-statements, the if statement can contain a if or if-else statement inside another if or
else-if statement.
Syntax : public class Test {
if(condition 1) { public static void main(String args[]) {
statement 1; //executes when condition 1 is true int x = 30;
int y = 10;
if(condition 2) {
if( x == 30 ) {
statement 2; //executes when condition 2 is true
if( y == 10 ) {
}
System.out.print("X = 30 and Y = 10");
else{
}
statement 2; //executes when condition 2 is false
}
} }
19 } }
Switch Statement
The switch statement contains multiple blocks of code called cases and a single case
is executed based on the variable which is being switched.
The switch statement is easier to use instead of if-else-if statements.
It also enhances the readability of the program.
public class Student implements Cloneable {
switch (expression){ public static void main(String[] args) {
case value1: int num = 2;
statement1; switch (num){
break; case 0:
. System.out.println("number is 0");
. break;
case 1:
.
System.out.println("number is 1");
case valueN:
break;
statementN; default:
break; System.out.println(num);
default: }
default statement; }
20 } }
Loop Statements
In programming, sometimes we need to execute the block of code
repeatedly while some condition evaluates to true.
Loop statements are used to execute the set of instructions in a repeated
order.
The execution of the set of instructions depends upon a particular
condition.
All the programming languages have the below 3 types of loops.
a) For Loop
b) While loop
c) do-while loop
21
Loop Statements
For Loop
It enables us to initialize the loop variable, check the condition, and
increment/decrement in a single line of code.
Use the for loop only when exactly know the number of times, we want to execute the
block of code.
Syntax>>>
for(initialization, condition, increment/decrement)
{
//block of statements
}
22
Loop Statements
For Loop
public class Calculattion {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
for(int j = 1; j<=10; j++)
{
sum = sum + j;
}
System.out.println("The sum of first 10 natural numbers is " + sum);
}
} Output:
23 The sum of first 10 natural numbers is 55
While Loop
The while loop is also used to iterate over the number of statements multiple times.
However, if don't know the number of iterations in advance, it is recommended to
use a while loop.
Unlike for loop, the initialization and increment/decrement doesn't take place inside
the loop statement in while loop.
It is also known as the entry-controlled loop since the condition is checked at the
start of the loop.
If the condition is true, then the loop body will be executed; otherwise, the
statements after the loop will be executed.
24
While Loop
The syntax of the while loop is given below.
while(condition){
//looping statements
}
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0; Output:
System.out.println("Printing the list of first 10 even numbers \n"); Printing the list of first 10 even numbers
while(i<=10) { 0
2
System.out.println(i);
4
i = i + 2; 6
} } } 8
10
25
Do-While Loop
The do-while loop checks the condition at the end of the loop after executing the
loop statements.
When the number of iteration is not known and have to execute the loop at
least once, can use do-while loop.
It is also known as the exit-controlled loop since the condition is not checked
in advance.
The syntax of the do-while loop is given below.
do
{
//statements
} while (condition);
26
Do-While Loop
public class Calculation {
Output:
public static void main(String[] args) {
// TODO Auto-generated method stub Printing the list of first 10 even numbers
0
int i = 0; 2
System.out.println("Printing the list of first 10 even numbers \n"); 4
6
do { 8
System.out.println(i); 10
i = i + 2;
}while(i<=10);
} The difference between do-while and while is that do-while evaluates its expression at
} the bottom of the loop instead of the top.
Therefore, the statements within the do block are always executed at least once
27
An array is a collection of similar type of elements which has contiguous
memory location.
Arrays are nothing but a set of similar elements stored in a single variable.
If we take a normal variable (int x;) it will take/store only one value at a time
and we cannot store more than one value.
Example: Let’s say would like to write a program where you want to store student
rule numbers for college in which there are 1000 students available.
If you use a normal variable I have to declare a thousand variables like below:
int rolno1=1;
int rolno2=2;
int rolno3=3
And it goes on till
int rolno1000=1000;
28
Declaration of Arrays:
Data type arrayname [ ];
Example: int rolno [ ];
Allocating Memory for Arrays:
int rolno[] = new int[1000]
Using “new” here new is a keyword
rolno[0]=1; which basically used to allocate the
rolno[1]=2; memory.
rolno[2]=3;
rolno[3]=4;
And it goes on….. Till
rolno[999]=1000;
29
Identifiers and Naming Convention
30
Identifiers
❏ Identifiers are the names of variables, methods, classes, packages.
Rules of Identifiers
►Identifiers must be composed of letters, numbers, the underscore _ and the dollar sign $.
►Identifiers may only begin with a letter, the underscore or a dollar sign.
►name, _name, $name , student_name, student$name, studentName, student2name
►num1, revenue, netrevenue, net$revenue
31
Identifiers
The main restriction on the names you can give:
➔ Identifiers cannot be a keyword. E.g. int void =0; X double static =2.99;X
➔ they cannot contain any white space. E.g. student name X
➔ cannot begin with a number. E.g 1num , 20student X
➔ all variable names are case-sensitive.
E.g. Total and total are different identifiers.
Total -> class name , package name
total -> variable name , method name
32
Naming Conventions
❏ Class names should be nouns, in mixed case with the first letter of each internal word capitalized.
E.g. CalculatorProgram,Calculator_Program,Calculator$Program, Calculator Program X
❏ Variable names should be short (meaningful). In mixed case with a lowercase first letter. Internal words
start with capital letters.
E.g.: firstNum, studentName, student_name
❏ Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal
word capitalized.
E.g. findAverage( ) , calculateAge( ), findTotal( )
33
In Java programming , how to write code in the following
structures.
I. Sequence structure
II. Selection Structure
III. Iteration Structure
34
35