Java U 1 Lec 3 Notes PDF
Java U 1 Lec 3 Notes PDF
Today’s Target
Variables
Declaration
Dynamic Initialization
Scope and Lifetime
Control Flow
Linear (Sequential)
Non-linear
Object Oriented Programming with Java
Variables
“A variable is a named memory location used to store data that can be modified during
program execution.”
Declaration
The variables are declared as follows:
int var;
int var=10;
Here, int is the data type, var is the identifier, and 10 is the initial
value.
We may declare more than one variables of the same type in one statement:
int var1=20,var2,var3;
Object Oriented Programming with Java
Variables
Dynamic Initialization
A variable may be initialized dynamically, using any expression valid at the time the variable is declared.
int var1=20,var2=var1+30,var3=var1+var2;
Scope and Lifetime
A block, beginning with an opening curly brace and ending by a closing curly brace, defines a scope.
A scope determines what variables are visible to other parts of the program
Scope also defines the lifetime of these variables. Variables are created
when their scope is entered and destroyed when their scope is left.
The method scope begins with its opening curly brace but method
parameters are included in the method’s scope.
Variable declared with in a scope are not visible to the code outside that
scope. Scopes can be nested. The variables declared in the outer scope are
visible to the inner scope but the reverse is not true.
Object Oriented Programming with Java
Variables
Example:
public class VarDemo {
public static void main(String[] args) {
int var=10;
int var1=20,var2=var1+30,var3=var1+var2;
System.out.println(var);
System.out.println(var1+var2+var3);
if(var3>var)
{
int var4=100;
System.out.println(var3+" is greater than "+ var);
}
else
{
int var5=200;
System.out.println(var+" is greater than "+ var3);
}
System.out.println(var4+var5);// Error
}
}
Object Oriented Programming with Java
Control Flow
“Control flow determines the order of execution of statements in a Java program.”
Control Flow
Types of Control Flow
Non-Linear Control Flow
It changes the normal sequence of execution. It includes Selection, Iteration, and Jump Statements.
Control Flow
Types of Control Flow
Non-Linear Control Flow
1. Selection (Conditional) Statements
‘if’ Statement
The if-else-if ladder checks multiple conditions in order.
Example:
Control Flow
Types of Control Flow
Non-Linear Control Flow
1. Selection (Conditional) Statements
‘switch’ Statement
The switch statement is used to simplify decision-making when multiple conditions need to be
checked. It provides a way to execute different parts of the code based on the value of an expression/
switch (expression) {
variable.
case value1:
// Code to execute if
expression matches value1 It compares a variable/ expression against multiple possible values
break;
and executes the matching case.
case value2:
// Code to execute if Each case should end with break (otherwise, the program will
expression matches value2
break; continue checking the next case even if a match is found).
default: The default case is optional but executes when no match is found.
// Code to execute if
no case matches It works well when replacing multiple if-else-if conditions.
}
Object Oriented Programming with Java
case 5:
Control Flow System.out.println("Thursday");
break;
Types of Control Flow case 6:
System.out.println("Friday");
Non-Linear Control Flow break;
1. Selection (Conditional) Statements case 7:
‘switch’ Statement System.out.println("Saturday");
break;
Example: default:
System.out.println("Invalid day!");
int day = 8; }
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
Object Oriented Programming with Java
Control Flow
Types of Control Flow
Non-Linear Control Flow case "JUN":
System.out.println("EVEN Semester");
1. Selection (Conditional) Statements break;
‘switch’ Statement default:
Example: System.out.println(“Invalid Month!");
break;
}
String month = "OCT";
switch (month) {
case "JUL":
case "AUG":
case "SEP":
case "OCT":
case "NOV":
case "DEC":
System.out.println("ODD Semester");
break;
case "JAN":
case "FEB":
case "MAR":
case "APR":
case "MAY":
Object Oriented Programming with Java
Control Flow
Types of Control Flow
Non-Linear Control Flow
It changes the normal sequence of execution. It includes Selection, Iteration, and Jump Statements.
2. Iteration (Looping) Statements
An iteration statement is used to create loop which repeatedly executes the same set of
instructions until a termination condition is met. Java’s iteration statements are for, while, and
do-while.
‘while’ Loop Example:
The ‘while’ loop repeats a statement or block while its
controlling condition is true. When condition becomes int n=1;
false, control passes to the next line of code immediately while(n<=10)
following the loop. {
Without {}, only the first line after while runs. System.out.println(n);
Since the while loop evaluates its conditional n++;
expression at the top of the loop, the body of the }
loop will not execute even once if the condition is
false.
The body of the while can be empty.
Object Oriented Programming with Java
Control Flow
Types of Control Flow
Non-Linear Control Flow
‘do-while’ Loop
The do-while loop always executes its body at least once as it tests the termination condition at
the end of the loop rather than at the beginning.
Example:
int n=1;
do
{
System.out.println(n);
n++;
} while(n<=10);
Object Oriented Programming with Java
Control Flow
Types of Control Flow
Non-Linear Control Flow
2. Iteration (Looping) Statements
‘for’ Loop
The for loop in Java is used to repeat a set of statements a fixed number of times. The new for-each loop
helps iterate through arrays and collections easily.
Example:
for (int i = 1; i <= 10; i++)
System.out.println(i);
Object Oriented Programming with Java
Control Flow
Types of Control Flow
Non-Linear Control Flow
2. for-each Loop:
It is used to iterate over arrays or collections without needing an index.
It is simpler and safer (avoids index errors).
It is best suited for reading elements but not modifying them.
Example:
Control Flow
Types of Control Flow
Non-Linear Control Flow
It changes the normal sequence of execution. It includes Selection, Iteration, and Jump Statements.
3. Jump Statements
Jump statements in Java are used to transfer control to a different part of the program.
‘break’ Statement
The break statement is used to immediately exit a loop or a switch statement, skipping any
remaining iterations or cases.
Example:
Control Flow
Types of Control Flow
Non-Linear Control Flow
3. Jump Statements
‘continue’ Statement
The continue statement is used to skip the current iteration of a loop and move to the next
iteration without executing the remaining statements in the loop body.
Example:
Control Flow
Types of Control Flow
Non-Linear Control Flow
3. Jump Statements
‘return’ Statement
The return statement is used to exit from a method and optionally return a value to the calling
method.
Example: