Java Arrays Loops Presentation
Java Arrays Loops Presentation
Beginners
Arrays and Loops
A beginner-friendly introduction to
fundamental Java concepts
Today's Topics
Creating Arrays
Accessing Array
Elements
// Reading values
int firstScore = scores[0];
int thirdScore = scores[2];
// Changing values
scores[1] = 92; // Changes
scores[4] = 80; // Changes
// Printing a value
System.out.println("First sc
First score: 95
System.out.println("The arra
IndexOutOfBoundsException:
Happens when you try to access
an index that doesn't exist
Always remember:
Array Exercises
Show Solution
Show Solution
Introduction to
Loops
Why Do We Need
Loops?
System.out.println(1);
System.out.println(2);
System.out.println(3);
// ... and so on for 100 lin
For Loop
Example: Printing
Numbers 1-5
Number: 1 Number: 2
Number: 3 Number: 4
Number: 5
How it works:
1. Initialize i = 1
2. Check if i <= 5 (true)
3. Execute the code block (print
number)
4. Update i (i++, which means
i = i + 1)
5. Repeat steps 2-4 until
condition is false
While Loop
while (condition) {
// Code to repeat
}
Example: Counting
Down from 5
int countdown = 5;
System.out.println("Blast of
Countdown: 5 Countdown: 4
Countdown: 3 Countdown: 2
Countdown: 1 Blast off!
Do-While Loop
do {
// Code to repeat
} while (condition);
Example: Menu
Selection
import java.util.Scanner;
do {
System.out.print
System.out.print
System.out.print
System.out.print
System.out.print
choice = scanner
// Process the u
System.out.print
} while (choice != 3
System.out.println("
}
10
For-Each Loop
Example: Iterating
Through an Array
11
Index 0: 10 Index 1: 20
Index 2: 30 Index 3: 40
Index 4: 50
Value: 10 Value: 20
Value: 30 Value: 40
Value: 50
12
Loop Control
Statements
if (!found) {
System.out.println("30 w
}
The continue
Statement
1 is odd 3 is odd 5 is
odd 7 is odd 9 is odd
13
Show Solution
Exercise 2: Finding
Maximum Value
Show Solution
Show Solution
14
Nested Loops
Example: Printing a
Pattern
* * * * * * * * * * * * *
* * * * * * *
// A 2D array representing a
int[][] grid = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
15
Common Loop
Patterns
1. Looping Through
Array in Reverse
50 40 30 20 10
3. Processing Every
Other Element
16
Choosing the
Right Loop
Loop Example
Best For
Type Use Case
When you
know Iterating
exactly how through an
for
many array with
iterations an index
you need
Reading
When you
values from
need to
an array
for- process
(when
each every
index
element in a
doesn't
collection
matter)
When the
loop
Reading
condition
input until a
depends on
while specific
something
value is
other than a
entered
simple
counter
When you
Displaying
need to
do- a menu and
execute the
while getting user
loop at least
input
once
17
Advanced
Exercises
Show Solution
Show Solution
18
Summary
Arrays
An array is a collection of
elements of the same type
Array indexes start at 0
Arrays have a fixed size that
cannot be changed after
creation
We can access elements
using their index:
array[index]
The length of an array can be
found using: array.length
Loops
Loop Control
19
Thank You!
Keep practicing arrays
and loops