Previous Lecture Lecture 2 Next Lecture

Lecture 2, Wed 04/03

Strings, Control Flow

Strings

String name = "Some string";
String name = new String("Some string");

Escape characters

Examples:

System.out.println("a\tb\tc");
System.out.println("d\ne\nf");
System.out.println("g\'h\'i");
System.out.println("j\"k\"l");
System.out.println("m\\n\\o");

Some common String methods

String name = "Richert";
System.out.println(name.length()); // 7
String name = "Richert"
System.out.println(name.substring(1, 5)); // iche
String s = new String("Hello");
System.out.println(s.toUpperCase()); // HELLO
System.out.println(s.toLowerCase()); // hello
System.out.println(s.charAt(2)); // ‘l’
System.out.println(s.charAt(100)); // ERROR – StringIndexOutOfBoundsException
System.out.println(s.equals(“Hello”)); // true
System.out.println(s.equals(“hello”)); // false
System.out.println(s.equals(“Hello”)); // true
System.out.println(s.equals(“hello”)); // false
String a = "a";	
String b = "b";
String x = "A";
		
System.out.println(a.compareTo(a)); // == 0
System.out.println(a.compareTo(b)); // < 0
System.out.println(a.compareTo(x)); // > 0

Concatenation

System.out.println(3 + 4 + "Hi"); // 7Hi
System.out.println(3 + (4 + "Hi")); //34Hi
System.out.println("3" + 4 + "Hi"); //34Hi
String s = "3";
int i = Integer.parseInt(s);
System.out.println(i + 5); // 8
String s = Integer.toString(3);
System.out.println(s); // 3
// OR	
String t = String.valueOf(4);
System.out.println(t); // 4
// OR	
String u = "" + 3;
System.out.println(u); // 3 (kinda hacky)

String pool

Example:

String s = "Hello";
String t = "Hello";
		
System.out.println(s == t); // prints true
		
t = new String("Hello");
System.out.println(s == t); // prints false

Control Flow

Conditional Statements

Allows the execution of code if some boolean value is true. * Examples of boolean experessions: true, false, i < 10, s.equals("Hi");

If statement

if (boolean_expression) {
	statements
} else {
	statements
}
if (boolean_expression)
    statement
else
    statement
if (boolean_expression)
    ; // or { }
else {
    statements
}

Relational Operators

Example:

Object x = new Object(); // just a plain ol’ object.
Object y = x;
if (x == y) {
    System.out.println("x == y"); // y refers to x
} else {
    System.out.println("x != y");
}

Logical Operators

Example:

int z = 5;

if (!((z – 3) > 0)) {
    System.out.println("true");
} else {
    System.out.println(“false"); // Prints False
}

Unreachable Code

public static int f() {
    int a = 0, b = 0;
    if (a < b) {
        System.out.println("a<b");
        return a;
    } else {
        System.out.println("a>=b");
        return b;
    } 
    System.out.println("after if/else"); // Compile error, code is unreachable
}

While Loop

General format:

while (boolean_expression) {
    statements;
}

Example:

int i = 0;
while(i < 10) {
    i++;
    System.out.println(i);
} // Prints 1 -> 10

Do-while Loop

General format:

do {
    statements;
} while (boolean expression); // note the ';' here

Example:

int i = 0;
do {
i++;
    System.out.println(i);
} while(i < 10); // Prints 1 -> 10

For Loop

General format:

for (initial action; boolean expression; update action) {
    statements;
}

Some looping examples:

for (int i = 0; i < 10; i++) {
    System.out.println(i); // prints 0 – 9
}

int i = 0;
while (i < 10) {
    System.out.println(i); // prints 0 - 9
    i++;
}

i = 0;
for (; i < 10;) { // null initial and update statements 
    System.out.println(i); // prints 0 - 9
    i++;
}

for (int j = 0;;j++) { // null boolean expression statement
    System.out.println(j * j); // infinite loop, overflow values

Scoping with Loops

Examples:

int sum = 0;
for (int i = 1; i <= 10; i++)
{
	sum = sum + i;
}
System.out.println(i);  // ERROR: no i here!

int i = 3; 
int sum = 0;
for (int i = 1; i <= 10; i++)   // ERROR: i is already declared
{	}			
	
int sum = 0;
do {
int count = i + 1;
} while (count != 999); // ERROR: no count here!

Break and Continue

Example:

int i = 0;
while (true) {
	i++;
	System.out.println(i);
		
	if (i < 10) {
		continue;
	} else {
		break;
	}
}
System.out.println("Outta the loop");
// Prints 1->10
// Outta the loop