1 of 34

Lecture 2:

Conditionals & Loops

CS 136: Spring 2024

Katie Keith

2 of 34

Record on Zoom

3 of 34

  • Course webpage:
    • Refresh for previous lecture content
    • Lecture recordings linked
    • Code linked
  • Optional: Laptops in lab on Weds/Thurs
  • Fri: Winter carnival no class

📣 Announcements

4 of 34

📚Readings

5 of 34

  • Review types & Java docs
  • Java: Conditionals and loops
  • Java: Style tip!

🎯 Today’s Learning Objectives

6 of 34

Basic building blocks for programming

primitive data types

assignment statements

Java’s built-in

Math library

input /output

conditionals and loops

arrays

objects

Any program you might want to write

7 of 34

Review: Java, a few primitive types

8 of 34

Bits and bytes

Shorthand for “binary digit”

9 of 34

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

10 of 34

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:

11 of 34

Basic building blocks for programming

primitive data types

assignment statements

Java’s built-in

Math library

input /output

conditionals and loops

arrays

objects

Any program you might want to write

Essentially, a calculator

This lecture: To infinity and beyond!

12 of 34

  • Review types & Java docs
  • Java: Conditionals and loops
  • Java: Style tip!

🎯 Today’s Learning Objectives

13 of 34

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.

14 of 34

Booleans

15 of 34

Comparison operators

16 of 34

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

17 of 34

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

18 of 34

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

19 of 34

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

20 of 34

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

21 of 34

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

22 of 34

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

23 of 34

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

24 of 34

Fun CS history!

The reserved keyword for dates back to ALGOL 58 (in 1958)!

Family tree of the Algol, Fortran and COBOL programming languages

25 of 34

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

26 of 34

Loops.java

💻

27 of 34

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.

28 of 34

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.

29 of 34

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);

}

30 of 34

  • Review types & Java docs
  • Java: Conditionals and loops
  • Java: Style tip!

🎯 Today’s Learning Objectives

31 of 34

Style in programming

  • Style is subjective!
    • Analogy: MLA vs. APA when creating a bibliography
  • If you have multiple developers or you are sharing code, it’s nice to have a consistent set of rules.
  • We’ll be following Google’s Java Style Guide.

A few important rule today… more to come this semester!

32 of 34

Java Style: Camel Case

  • Classes: UpperCamelCase
    • Begin with a upper-case letter. All other words start with a lowercase letter (no spaces, no underscores)
  • Methods: lowerCamelCase
    • Begin with a lower-case letter. All other words start with a lowercase letter (no spaces, no underscores)
  • Constants: UPPER_SNAKE_CASE
    • All uppercase letters with words separated by underscores

33 of 34

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

34 of 34

  • Review types & Java docs
  • Java: Conditionals and loops
  • Java: Style tip!

🎯 Today’s Learning Objectives