6/4/2019
CS 1102 - AY2019-T4: Discussion Assignment
1/47
Home
►
My courses
►
CS 1102 - AY2019-T4
►
18 April - 24 April
►
Discussion Forum Unit 2
►
Discussion Assignment
Subscribe
HOME
CONTACT US
RESOURCES
LINKS
FACULTY
MY COURSES
MENU
Search forums
Discussion Assignment
by Eric Goh (Instructor) - Thursday, 11 April 2019, 5:09 PM
Describe the for loop and compare with the for-each loop by describing the operation of
each and discussing how they di±er from each other. You must include the role of enums in
your description and provide an example of a for loop and a for-each loop.
46 words
Permalink | Reply
Re: Discussion Assignment
by Dalton Overlin - Saturday, 20 April 2019, 1:53 AM
Introduction
For this assignment, I will ²rst start with explaining what a standard for loop is, and
give an example of one. Then I will explain what a for-each loop is, and give an example
of a for-each loop. After giving the two descriptions, and examples I will draw a

Subscribe to view the full document.
6/4/2019
CS 1102 - AY2019-T4: Discussion Assignment
2/47
comparison between a standard for loop, and a for-each loop. Then after drawing a
comparison I will discuss the roles of enums, or enumerations, and give some examples
on how they are useful, and why they are called constants.
The for Loop
The standard for loop can be used for many things, but in this example below I will
be using the for loop to iterate through and print each element of an array. In the
example, the variable x is initialized as an array, the array is then assigned the value of
²ve elements, then each of those elements is assigned random four-digit numbers to
represent years. The for loop then uses incrementation to iterate through each array
element of x. It starts at position 0, then works its way through the array with the help of
the count++; statement that increments the variable count each time the loop runs. Then
once count is equal to the length of elements in the array the for loop will stop. This
results in the printing of each of the elements contained within the array. This
demonstrates that the for loop runs for every time that variable count is not equal to the
length of elements within the array. So when the loop ends, and it goes to start again if
the loop condition returns true, such as count not being equal to the length of the array
it will run, but if it is false it will not run (Programiz.com, n.d.).

6/4/2019
CS 1102 - AY2019-T4: Discussion Assignment
3/47
package forexample;
public class ForExample {
public static void main(String[] args) {
// --------This example is the standard for loop!
int[] x; // This initializes a new array.
x = new int[5]; // This creates 5 new elements in the array, by default each new
element of an array with a int base is 0
x[0] = 1909; // This along with the next few assignment statements assigns values
to each of the elements of the array x.

Subscribe to view the full document.

- Fall '18