Lecture 2:
Conditionals & Loops
CS 136: Spring 2024
Katie Keith
Record on Zoom
📣 Announcements
📚Readings
🎯 Today’s Learning Objectives
Basic building blocks for programming
primitive data types
assignment statements
Java’s built-in
input /output
conditionals and loops
arrays
objects
Any program you might want to write
Review: Java, a few primitive types
Bits and bytes
Shorthand for “binary digit”
Review: Java, a few primitive types
Java has four other primitive types that we will rarely use:
byte (8-bit integers), short (16-bit integers),
long(64-bit; wider range than int when needed),
float (32-bit floating point)
32-bit
64-bit
1-bit*
16-bit
Reading Java documentation
Modifier=static
The method does not belong to any object
(can stand alone;
a function)
The return type is a double
The input type
is a double
double doubleNumber = -3.14;
double doubleAbs = Math.abs(doubleNumber);
System.out.println(doubleAbs);
Example usage:
Basic building blocks for programming
primitive data types
assignment statements
Java’s built-in
input /output
conditionals and loops
arrays
objects
Any program you might want to write
Essentially, a calculator
This lecture: To infinity and beyond!
✅
🎯 Today’s Learning Objectives
Control flow
Control flow is the sequence of statements that are actually executed in a program. Conditionals (if / else) and loops (for, while) enable us to choreograph control flow.
Booleans
Comparison operators
Java: If and else statements
int number = 0;
// Positive or negative number?
if (number > 0) {
System.out.println("The number is positive.");
}
else if (number < 0) {
System.out.println("The number is negative.");
}
else {
System.out.println("The number is zero.");
}
boolean expression
Java: If and else statements
int number = 0;
// Positive or negative number?
if (number > 0) {
System.out.println("The number is positive.");
}
else if (number < 0) {
System.out.println("The number is negative.");
}
else {
System.out.println("The number is zero.");
}
boolean expression
Unlike Python, no elif keyword in Java
Increment operator
Equivalent statements | i = i +1; and i += 1; | Examples: int a = 3; int b = 3; a = a + 1; b += 1; System.out.println(a == b); //true |
Pre-increment operator | Increments i and returns the new value of i j = ++i; | int a = 3; int b = ++a; System.out.println("a="+a); // 4 System.out.println("b="+b); // 4 |
Post-increment operator | Increments i and returns the old value of i j = i++; | int a = 3; int b = a++; System.out.println("a="+a); // 4 System.out.println("b="+b); // 3 |
To “increment” means to increase the value of a number by one
Increment operator
Equivalent statements | i = i +1; and i += 1; | Examples: int a = 3; int b = 3; a = a + 1; b += 1; System.out.println(a == b); //true |
Pre-increment operator | Increments i and returns the new value of i j = ++i; | int a = 3; int b = ++a; System.out.println("a="+a); // 4 System.out.println("b="+b); // 4 |
Post-increment operator | Increments i and returns the old value of i j = i++; | int a = 3; int b = a++; System.out.println("a="+a); // 4 System.out.println("b="+b); // 3 |
To “increment” means to increase the value of a number by one
Increment operator
Equivalent statements | i = i +1; and i += 1; | Examples: int a = 3; int b = 3; a = a + 1; b += 1; System.out.println(a == b); //true |
Pre-increment operator | Increments i and returns the new value of i j = ++i; | int a = 3; int b = ++a; System.out.println(a); // 4 System.out.println(b); // 4 |
Post-increment operator | Increments i and returns the old value of i j = i++; | int a = 3; int b = a++; System.out.println("a="+a); // 4 System.out.println("b="+b); // 3 |
To “increment” means to increase the value of a number by one
Increment operator
Equivalent statements | i = i +1; and i += 1; | Examples: int a = 3; int b = 3; a = a + 1; b += 1; System.out.println(a == b); //true |
Pre-increment operator | Increments i and returns the new value of i j = ++i; | int a = 3; int b = ++a; System.out.println(a); // 4 System.out.println(b); // 4 |
Post-increment operator | Increments i and returns the old value of i j = i++; | int a = 3; int b = a++; System.out.println(a); // 4 System.out.println(b); // 3 |
To “increment” means to increase the value of a number by one
Decrement operators
Equivalent statements | i = i -1; and i -= 1; | Examples: int a = 3; int b = 3; a = a - 1; b -= 1; System.out.println(a == b); // true |
Pre-decrement operator | Decrements i and returns the new value of i j = --i; | int a = 3; int b = --a; System.out.println(a); // 2 System.out.println(b); // 2 |
Post-decrement operator | Decrements i and returns the old value of i j = i--; | int a = 3; int b = a--; System.out.println(a); // 2 System.out.println(b); // 3 |
To “decrement” means to decrease the value of a number by one
For loops
A for loop is a control flow statement that allows code to be executed repeatedly based on a given condition
Example: Code that prints the powers of 2
Fun CS history!
The reserved keyword for dates back to ALGOL 58 (in 1958)!
Family tree of the Algol, Fortran and COBOL programming languages
int n = 4;
int product = 1;
for (int i =1; i <= n; i++){
product *= i;
}
System.out.println(product);
What does the following Java program print?
What mathematical operation is this implementing?
💡Think-pair-share
Loops.java
💻
While loops
Example:
Code to print powers of 2
A while loop is a control flow statement that repeatedly executes a block of code as long as a specified boolean condition evaluates to true.
Break statement
int power = 1;
while(power <= 1000000){
power = 2*power;
if(power > 10){
break;
}
}
System.out.println(power);
The keyword break causes a loop to immediately terminate.
It skips any remaining iterations and moves control flow to the statement immediately following the loop.
Continue statement
When a keyword continue is executed within a loop, the loop does not terminate entirely.
Instead, the control flow moves to the next step of the loop.
int n = 10;
int product = 1;
for(int i=0; i <= n; i++){
if(i%2 == 0){
continue;
}
product *= i;
System.out.println(product);
}
✅
✅
🎯 Today’s Learning Objectives
Style in programming
A few important rule today… more to come this semester!
Java Style: Camel Case
Java Style: Block Comments
Use block comments to provide a detailed explanation at the top of any program (.java file) and every important method.
Example:
/*
* This method calculates the area of a circle.
* It takes the radius as an input parameter.
*/
public double calculateArea(double radius) {
return Math.PI * radius * radius;
}
Block comment
✅
✅
✅
🎯 Today’s Learning Objectives