Feb 01: Arrays vs Strings
Learning Objectives
After today's class, you should be able to:
- Write standard and enhanced for loops that iterate over arrays.
- Describe common patterns for looping over arrays and strings.
- Summarize the types of questions that will be asked on Exam1.
Lesson Outline¶
Mini Lecture [10 min]
- See notes below
Lab03 [45 min]
- Lab 3: CodingBat Loops
- Each team will be assigned a problem to solve
- Spokesperson will present solution to the class
Exam1 Prep [20 min]
- Watch for announcement about study material
Reminders¶
If you haven't already…
- Submit Quiz03 on Canvas
- Need to install OpenJDK 17 (for testing to work right)
- Submit HW3 stubs to Gradescope before the weekend!
By the end of Monday
- Complete Homework 3 (due Feb 05) at 11:00pm.
Mini Lecture¶
Use the following reference material during today's lab.
Operators¶
Increment (++
) and decrement (--
)
Assignment | Shorthand |
---|---|
i = i + 1; |
i++; |
i = i - 1; |
i--; |
Compound assignment
Assignment | Shorthand |
---|---|
i = i + 2; |
i += 2; |
i = i - 2; |
i -= 2; |
i = i * 2; |
i *= 2; |
i = i / 2; |
i /= 2; |
i = i % 2; |
i %= 2; |
While Loops¶
Also known as a pre-test loop.
number = 1;
while (number <= 10) {
System.out.println(number);
number++;
}
Question
What is output by the following loop? And what mistake was made?
int i = 0;
while (i < 3)
System.out.println("i = " + i);
i = i + 1;
Do-While Loops¶
Also known as a post-test loop.
number = 1;
do {
System.out.println(number);
number++;
} while (number <= 10);
Question
What is output by the following loop? Explain how the code works.
number = 99;
do {
System.out.println(number);
number++;
} while (number <= 10);
System.out.println(number);
For Loops¶
Also known as the loop that you should use over 90% of the time.
// Loop A: count forwards
for (i = 1; i <= 10; i++) {
System.out.println(i);
}
// Loop B: count backwards
for (i = 10; i >= 1; i--) {
System.out.println(i);
}
Question
Rewrite each for loop above as a while loop.
Question
Change the for loops to print multiples of 3.
Array Loops¶
Standard for loop ("for each index")
for (int i = 0; i < array.length; i++) {
// do something with array[i]
}
- Note: the variable name
i
stands for index
Enhanced for loop ("for each value")
for (int value : array) {
// do something with value
}
- The
:
means in (i.e., "for each value in array") - Note: don't name the variable
i
(not an index)
String Loops¶
Standard for loop ("for each index")
for (int i = 0; i < str.length(); i++) {
// do something with str.charAt(i)
}
- Notice string
length()
uses parentheses - Must use
charAt()
method instead of[]
Enhanced for loop ("for each char")
for (char c : str.toCharArray()) {
// do something with c
}
c
is a more appropriate variable name thani
Exam1 Prep¶
Coming Soon¶
The following materials will be available by Monday of next week.
Remember that we will not have class on Tuesday (Assessment Day).
- Videos
- Solution for HW3
- Review Weeks 1–3
- Practice
- Sample exam (same format)
- Additional sample questions
Question Types¶
- Identify errors
- Invalid assignments (data types)
- Syntax (Ex: semicolons, braces)
- Write statements
- Declare (only)
- Declare and initialize
if
,while
,for
, etc.- Basic string methods
- Evaluate expressions
- Arithmetic (esp.
/
and%
) - Logic (
&&
,||
,!
)
- Arithmetic (esp.
- Code tracing
- What is the output of …?
- What is the value of…?
- Code writing
- Write a method that…
- Write a JUnit test for…