2 - Intro and Control flow

Slideshow

The recording will be published as soon as possible.

The class went over:

  • Java basics (recap of lesson 1)

  • Conditionals

  • For and while loops

Guide

Demonstrated code

App.java
 1public class App {
 2    public static void main(String[] args) throws Exception {
 3        System.out.println("Hello, World!");
 4        String str1 = "first string";
 5        str1 = "second string";
 6        System.out.println(str1);
 7
 8        int numOne = 4;
 9        int numTwo = 9;
10        int numSum = numOne + numTwo;
11        int numDiff = numOne - numTwo;
12        int numProd = numOne * numTwo;
13        int numQuot = numOne / numTwo;
14        System.out.println(numSum);
15        System.out.println(numDiff);
16        System.out.println(numProd);
17        System.out.println(numQuot);
18
19        int a = 5;
20        int b = 10;
21
22        if (a==b){
23            System.out.println("a = b");
24        }
25        else if (a<b){
26            System.out.println("a < b");
27        }
28        else{
29            System.out.println("a > b");
30        }
31
32        if (a < 10 && b > 5){
33            System.out.println("a is less than 10 AND b is greater than 5");
34        }
35        else if (a < 10 || b < 5 || !(a==b))  {
36            System.out.println("a is less than 10 OR b is less than 5");
37        }
38        else if (a!=b) {
39            System.out.println("a is NOT equal to b");
40        }
41        else {
42            System.out.println("neither of the conditions were true");
43        }
44        int j = 0;
45        while (j<5){
46            System.out.println("j is currently: " + j);
47            j++;
48        }
49
50        for (int y = 0; y<=3; y++){
51            System.out.println("Outer loop y = " + y);
52            for (int z = 1; z <=2; z++){
53                System.out.println("  Inner loop z = " + z);    
54            }
55        }
56    }
57}

Exercise

  • Write a program to check if the grade percentage in a variable is an A, B, C, or D.

    • A = 90+, B = 80+, C = 70+, D = 0-69.9

      • Use a loop to go through numbers from 50 to 94, inclusive

      • In increments of 4

  • Hint:

    • Think about what type of loop would be better

    • How you would increase something by 4 and use it?

Solution

Solution
GradeChecker.java
 1public class GradeChecker {
 2    public static void main(String[] args) {
 3        // Loop from 50 to 94 in increments of 4
 4        for (int grade = 50; grade <= 94; grade += 4) {
 5            // Check grade category
 6            if (grade >= 90) {
 7                System.out.println("Grade " + grade + " = A");
 8            } else if (grade >= 80) {
 9                System.out.println("Grade " + grade + " = B");
10            } else if (grade >= 70) {
11                System.out.println("Grade " + grade + " = C");
12            } else {
13                System.out.println("Grade " + grade + " = D");
14            }
15        }
16    }
17}