120 likes | 243 Views
Join us for Lecture 4 where we dive into the beauty of for loops in Java programming. We will explore how to format paragraphs, understand sequencing through analogies, and construct well-structured code snippets. Each session encourages participation, with online exercises due Thursdays by 10 PM. We’ll also cover important concepts like code compilation and structuring code for clarity. The lecture showcases practical coding examples inspired by the song "All These Things That I’ve Done" by The Killers, illustrating how for loops enhance program functionality.
E N D
Announcements Examples posted after lecture on schedule Discussion Section exercises on linebefore 9am Tuesdays Lab 2 Due Thursday 10pm Last Time: Compilation Scaffolding print(): displays a value println(): displays a value and moves cursor to the next line Announcements & Review Lecture 4: For Loops
Today More on Beauty Analogy: Formatting Paragraphs Sequencing Analogy: a well constructed essay Note on punctuation: end your statements Repetition (i for iteration) for (int i = 0; i < 10; i++) { // your code here } Lecture 4: For Loops
Beauty /* Kathryn S McKinley * September 2005 * file: Song.java - a few lines from a good song * Song: All These Things That I’ve Done * Band: The Killers, Album: Hot Fuss */ public class Song { public static void main (String [] args) { System.out.println(“You are going to bring yourself down”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“Yah yah, you got to help me out.”); } } Lecture 4: For Loops
I’m Ugly /* Kathryn S McKinley * September 2005 * file: Song.java - a few lines from a good song * Song: All These Things That I’ve Done * Band: The Killers, Album: Hot Fuss */ public class Song { public static void main (String [] args) { System.out.println(“You are going to bring yourself down”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“Yah yah, you got to help me out.”); } } Lecture 4: For Loops
Paragraphs // song lines System.out.println(“You are going to bring yourself down”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“Yah yah, you got to help me out.”); Lecture 4: For Loops
Paragraphs // verse System.out.println(“You are going to bring yourself down”); //chorus System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); //verse System.out.println(“Yah yah, you got to help me out.”); Lecture 4: For Loops
Can we do better? // verse System.out.println(“You are going to bring yourself down.”); //chorus System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); System.out.println(“I’ve got soul, but I’m not a soldier. ”); //verse System.out.println(“Yah yah, you got to help me out.”); Lecture 4: For Loops
Yes! // verse System.out.println(“You are going to bring yourself down.”); //chorus for (int i =0; i < 10; i++) { System.out.println(“I’ve got soul, but I’m not a soldier. ”); } //verse System.out.println(“Yah yah, you got to help me out.”); Lecture 4: For Loops
For: Definite Loop Three parts for (i = 0; i < 99; i++) { // loop body } • Initialization • Test • Increment Lecture 4: For Loops
For: Definite Loop Three parts for (int i = 0; i < 99; i++) { // loop body in here } • Initialization - int i = 0; • one time only • Test - if i < 99 then execute body • every time • Increment - i++ add 1 to the value of I • every time Lecture 4: For Loops
BlueJ Examples Lecture 4: For Loops
Questions? Lecture 4: For Loops