120 likes | 251 Vues
This resource provides an overview of primitive data types in Java, including boolean, char, double, and int. It encompasses arithmetic operators, expressions, and variable initialization. Learn about the precedence of operations, string concatenation, and how to utilize increment and decrement operators effectively. The section reviews for loops, showcasing their syntax and practical examples, including nested loops. Engage with self-check exercises to enhance your understanding and prepare for upcoming quizzes.
E N D
Building Java ProgramsChapter 2 Primitive Data and Definite Loops
Review • Primitive data types: boolean, char, double, int • Expressions and literals • Arithmetic operators: +, -, *, /, % • Precedence and casting • Variables • String concatenation and mixing types • Increment/decrement operators • For loops
Review • What output by the following set of statements? • int x = 12; • double y = 24; • System.out.println("x divided by y = " + x / y); • A. x divided by y = 0 • B. x divided by y = 12 / 24 • C. x divided by y = 0.5 • D. x divided by y = + 12 / 24 • E. Nothing – this does not compile.
header body for loop syntax for (initialization; test; update) { statement; statement; ... statement; } Perform initialization once. Repeat the following: • Check if the test is true. If not, stop. • Execute the statements. • Perform the update.
Repetition with for loops We see redundancy when trying to perform a task more than once: System.out.println(“Let’s go…"); System.out.println(“Seahawks!"); System.out.println(“Seahawks!"); System.out.println(“Seahawks!"); System.out.println(“Go home, Peyton!"); Java's for loop statement performs a task many times. System.out.println(“Let’s go…"); // Repeat 3 times for (inti = 0; i <= 2; i++) { System.out.println(“Seahawks!"); } System.out.println(“Go home, Peyton!");
Volunteers Initializer Condition tester Console Updater Variable
for (intcandies = 0; candies < 5; candies++) { System.out.println("I ate " + candies + " candies"); }
Practice-It • Self-check: 2.12, 2.15, 2.16, 2.18, 2.20
Nested for loops One for loop can be nested inside another. for(intouterLoop = 0; outerLoop < 2; outerLoop++) { System.out.println("OuterLoop variable = " + outerLoop); for (intinnerLoop = 5; innerLoop > 0; innerLoop = innerLoop / 2) { System.out.println("InnerLoop variable = " + innerLoop); } }
Nested for loops for (intouterLoop = 0; outerLoop < 2; outerLoop++) { System.out.println("OuterLoop variable = " + outerLoop); for (intinnerLoop = 5; innerLoop > 0; innerLoop = innerLoop / 2) { System.out.println("InnerLoop variable = " + innerLoop); } } • OuterLoopvariable = 0 • InnerLoop variable = 5 • InnerLoop variable = 2 • InnerLoop variable = 1 • OuterLoop variable = 1 • InnerLoop variable = 5 • InnerLoop variable = 2 • InnerLoop variable = 1
Homework Self-check: 2.12, 2.15, 2.16, 2.18, 2.20, 2.26, 2.27 Exercise: 2.2, 2.3 If you finish the homework before class is over: • Review for your quiz on Wednesday. • Read BJP 2.4. • Do extra Practice-Its.