Files
G4G0-1/Semester 1/Programming 1/Week 10/Week 10 Programming 1.md
2024-01-16 13:48:46 +00:00

802 B

Lecture 2 (13:00)

  • Remember to use string1.equals(string2) rather than string1 == string2, as the former may not be accurate.

For Loop Cont.

Example 1:

int sum = 0;
for( int i = 0; i < animalCount.length; i++ )
{
	sum += animalCount[i];
}

Loop Control:

  • Initialisation
  • Loop Condition
  • Update

for the amount of times in the length of the animalCount array, add the animalCount[i] to the sum.

Example 2:

for( int k = 1; k < 500; k += 2)
{
	System.out.println(k);
}

Example 3:

for( int i = 1; i < 4; i++ )
{
	for( int j = i; j < 5; j++ )
	{
		System.out.println( i * j + "\t" );
	}
}

i = 1, j = 1, 2, 3 ,4 | 1, 2, 3, 4 i = 2, j = 1, 2, 3, 4 | 2, 4, 6, 8 i = 3, j = 1, 2, 3, 4 | 3, 6, 9, 12

  • \t represents a TAB character.