Introduction to
Java
Programming
Basics and Control Structures
Topics Covered
• Variables and Data Types
• Basic Operators
Tip: Use links to go to a different page inside your
presentation. Links work best for pages like this one!
• Control Flow: if-else Statements
How: Highlight text, click on the link symbol on the
• Loops: for, while, do-while toolbar and select the page in your presentation
that you want to connect.
Variables and Data Types
• Variables are used to label and store
data that can be referenced and
Java has 8 primitive data types, each with a manipulated in a program.
specific size and range.
• Every variable has a data type that
defines the type of data it can hold.
Syntax - dataType variableName =
value;
Common Non-Primitive Data Types
• String: A sequence of characters (e.g., "Hello").
• Array: A collection of elements of the same type (e.g., int[] numbers =
{1, 2, 3};).
• Class: A blueprint for creating objects (e.g., Student, Car).
• Interface: An abstract type that specifies behaviors that classes must
implement.
Basic Operators in Java
Arithmetic Operators Assignment Operators
Perform mathematical operations. Assign values to variables.
+ (Addition) = (Simple assignment)
- (Subtraction) += (Addition assignment)
* (Multiplication) -= (Subtraction assignment)
/ (Division) *= (Multiplication assignment)
% (Modulus) /= (Division assignment)
int sum = 10 + 5; // Addition %=
int x (Modulus assignment)
= 10; // Simple assignment
int difference = 10 - 5; // Subtraction x += 5; // x = x + 5; now x is 15
int product = 10 * 5; // Multiplication x -= 3; // x = x - 3; now x is 12
int quotient = 10 / 5; // Division x *= 2; // x = x * 2; now x is 24
int remainder = 10 % 3; // Modulus (remainder) x /= 4; // x = x / 4; now x is 6
Basic Operators in Java
Comparison Operators Logical Operators
Compare two values. Combine multiple conditions.
== (Equal to) && (Logical AND)
!= (Not equal to) || (Logical OR)
> (Greater than) ! (Logical NOT)
< (Less than)
>= (Greater than or equal to) boolean condition1 = (5 > 3); // true
<= (Less than or equal to) boolean condition2 = (8 < 6); // false
int a = 10;
int b = 20;
boolean result1 = condition1 && condition2; //
boolean isEqual = (a == b); // false
false (both must be true)
boolean isNotEqual = (a != b); // true
boolean isGreater = (a > b); // false
boolean result2 = condition1 || condition2; // true
boolean isLess = (a < b); // true
(one must be true)
boolean isGreaterOrEqual = (a >= b); // false
boolean isLessOrEqual = (a <= b); // true
boolean result3 = !condition1; // false (negation of
true)
Basic Operators in Java
Unary Operators
Perform operations on a single operand.
+ (Unary plus - typically used to indicate
positive numbers)
- (Unary minus - used to negate a value)
++ (Increment - increases the value by 1)
-- (Decrement - decreases the value by 1)
int x = 10;
x++; // Now x is 11
x--; // Now x is 10 again
int y = -x; // y is -10
Control Flow in Java
Control flow statements in Java determine the int score = 85;
order in which statements are executed.
if (score >= 90) {
• if Statement: Executes a block of code if a System.out.println("Grade: A");
specified condition is true.
} else if (score >= 80) {
• if-else Statement: Executes one block of code if System.out.println("Grade: B");
a condition is true and another if it is false.
} else {
• else if Statement: Tests multiple conditions in System.out.println("Grade: C");
sequence. }
• switch Statement: Selects a block of code to
Control Flow in Java
The switch statement is used for branching based
on the value of a variable.
int day = 3;
switch(day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
Looping Constructs in Java
Repeat a block of code multiple times.
Types:
for Loop: Repeats a block of code a specific number
of times.
while Loop: Repeats a block of code as long as a
specified condition is true.
do-while Loop: Similar to the while loop, but it
checks the condition after the loop executes.
for loop
Repeat a block of code a specific number of times. Initialization: Sets up the loop control variable.
Condition: Tests the loop control variable; loop
Syntax
continues as long as this is true.
for (initialization; condition; update) {
Update: Modifies the loop control variable after each
// Code to be executed
}
iteration.
for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}
while loop
Repeat a block of code while a condition is true. The loop executes as long as the condition
evaluates to true. Ensure the condition
Syntax
eventually becomes false to avoid an infinite
loop.
while (condition) {
// Code to be executed
}
int i = 0;
while (i < 5) {
System.out.println("Iteration: " + i);
i++;
}
do-while loop
Similar to while, but guarantees that the code block The loop executes the block of code first,
is executed at least once. then checks the condition.
Syntax
do {
// Code to be executed
} while (condition);
int i = 0;
do {
System.out.println("Iteration: " + i);
i++;
} while (i < 5);
Thank
you!