Name: Yousef Amr Abdelazeem Elbish ID: 2301295
Subject: Advanced Programming - Tutorial Group: F 1
Task 3rd April 2024
Q1. What is the difference between for loop and the for-each
loop?
For Loop:
- This is the usual type of loop used when you want to run a block of code several times.
- It uses an index, which is a way to count through each cycle of the loop. This is helpful when
you need to perform complex patterns of iteration or change the elements you are
looping through.
- Here’s how it looks (Syntax):
for (initialization; condition; update) { // code to be executed }
- Use this loop when you need to know exactly how many times the loop should run, or if you
need to adjust the counting index directly.
For-Each Loop:
- This was added to Java in version 5 to make looping simpler.
- It makes it easy to loop through items in collections or arrays without worrying about
managing the index yourself.
- For-Each Loop doesn’t allow you to change the elements of the collection or array you’re
looping through.
1
- Here’s how it looks (Syntax):
for (Type item: collection) { // code to be executed }
In summary, use the regular for loop when you need tight control over the loop, like when
specific conditions or changes are involved. Use the for-each loop for a
straightforward, easy-to-read way to go through items one by one without modifying
them.
Q2. How to make an array with different datatypes?
If you need to store different types of data in one array, you can use an array of objects. This
works because Java is strictly typed and normally doesn't let you mix data types in a
single array.
Here's a simple way to do it (Sample):
Object[] Arr = new Object[5];
Arr[0] = "String"; // Stores a String
Arr[1] = 1; // Stores an Integer
Arr[2] = 1.0; // Stores a Double
Arr[3] = true; // Stores a Boolean
Arr[4] = new int[]{1, 2, 3}; // Stores an array of integers
In this example, each slot in the array can hold any type of object. That’s because everything
in Java inherits from the `Object` class, making it a versatile container. However,
when you need to use these items, remember that you must convert them back to
their specific types.
Q3. How to add a fixed size to an array?
Initialize the size you want to enter.
- Here’s how it looks (Syntax):
int []arr=new int[5]; //Create an array with 5 elements
2
Q4. Answer of Q8 in sheet 5:
- Here’s the answer using for loop:
public class Main {
public static void main(String[] args) {
// Assuming Employee class and employeeData array are defined elsewhere
for(int i = 0; i < employeeData.length; i++) {
if(employeeData[i].yearsWithCompany >= 20) {
System.out.println(employeeData[i].firstName + " "
+ employeeData[i].lastName + " - $"
+ employeeData[i].hourlyWage);
} } } }
- Here’s the answer using for-each loop:
public class Main {
public static void main(String[] args) {
// Assuming Employee class and employeeData array are defined elsewhere
for(Employee emp : employeeData) {
if(emp.yearsWithCompany >= 20) {
System.out.println(emp.firstName + " "
+ emp.lastName + " - $"
+ emp.hourlyWage);
} } } }