Principles of Computer Science Lecture 2 Dr. Horia V. Corcalciuc Horia Hulubei National Institute for R&D in Physics and Nuclear Engineering (IFIN-HH) January 27, 2016
Loops: do-while do-while loops do { while(<expression>); Contrary to while loops, do-while loops, on the first run, the statements in the do-while block are executed first, after which the expression is evaluated and, if it evaluated to true then the statements are executed again. The loop terminates once the expression at the end evaluates to false.
Control Flow break and continue break do { ; while(<expression>); continue do { continue; ; while(<expression>); The break statement can be used to break out of the inner-most loop such that execution continues after the loop block. The continue statement can be used to skip any statements following the continue, re-evaluate the loop expression and then loop again on the next iteration.
Control Flow do-while vs. while do-while Example int i = 10; do { i = i - 1; while(i > 0); while Example int i = 10; while(i > 0) { i = i - 1; It is possible to convert a while loop to a do-while loop and vice versa.
Control Flow Conditionals: if-else if-else if-else if-else branching if(<truth of expression>) { else if(<truth of expression>)) { else if(<truth of expression>)) {... else { if-else if-else branching Example int i = 10; if(i > 5) { System.out.println("greater"); else if(i < 5) { System.out.println("smaller"); else { System.out.println(5); if branches can have multiple else if conditions but only one if and only one else.
Control Flow Conditionals: switch switch branching switch(<value of expression>) { case <constant>: case <constant>: ; default: ; switch branching Example int i = 10; switch(i) { case 1: System.out.println(1); default: System.out.println("Error!"); Upon entering the switch statement, the expression is evaluated to a value. The block of the first case constant that matches what the expression was evaluated to is executed. In case there is a break statement, then the switch block is exited. Otherwise, execution continues to the next case until a break statement is found (case fallthrough). If no case constants match, and if there is a default case, then the statements of the default case are executed. Exercise
Control Flow if vs. switch if if(<truth of expression>) { else if(<truth of expression>)) {... else { switch switch(<value of expression>) { case <constant>: case <constant>: default: if branches on the truth of the expression whilst switch branches on the value of the expression. if-else if-else cannot fall though the blocks switch case-labels take a constant value, a value that can be determined at compile time, not an expression. The truth of the expression for if-else if-else blocks is evaluated at run-time whilst the constants of the switch cases are determined at compile-time.
Control Flow switch differences between C# and Java switch fallthrough in Java switch(<value of expression>) { case <constant>: case <constant>: default: switch fallthrough in C# switch(<value of expression>) { case <constant>: case <constant>: goto default; default: In C# case fallthrough is possible by using the goto statement.
Control Flow Conditional Evaluation Examples if(3 > 5) switch(10) if(2-1) - does not work in C#, Java if(-1) - does not work in C#, Java switch(2 > 1)
L- and R-Values L- and R-Value Assignment L-Value := R-Value Expressions that refer to containers such as memory locations are called L-Value expressions. L-Values can only appear on the left-hand side of an assignment. On the other hand, an R-Value refers to the value of an expression. All L-Values are R-Values but not all R-Values are L-Values. Illegal Examples (Java and C#) i + 3 = 5; // i + 3 is an r-value not an l-value 3 = i + 2; // 3 is NOT an l-value
Assignment and Testing for Equality Programming languages distinguish between assignment and testing for equality by using the following syntax Exercise : Assignment is performed by using a single equal sign (=) and the assignment operation itself evaluates to the contents of the left-most member in the assignment. Evaluation of Assignment int i = 0 int j = (i = 1) + i; Testing for equality is performed with a double-equal sign (==) and the test evaluates to either true or false. Evaluating an Expression if(false == 5 > 3)
Increment and Decrement Pre-Increment Increment and Decrement int i = 5; int j = ++i + 1; Declare a new integer variable i and assign the value 5 to i Declare a new integer variable j, increment i, add 1 to the result and assign to j Post-Increment Increment and Decrement int i = 5; int j = i++ + 1; Declare a new integer variable i and assign the value 5 to i Declare a new integer variable j, take the current value of i, add 1 to the current value of i, assign to j and increment the value of i
Increment and Decrement : Comparison Increment and Decrement i = i + 1; i = i - 1; Pre ++i; --i; Post i++; i--; ASM Pre-Increment ; j = ++i + 42 mov dword [ss:rbp+var_4], 0x1 mov eax, dword [ss:rbp+var_4] add eax, 0x1 ASM Post-Increment ; j = i++ + 42 mov dword [ss:rbp+var_4], 0x1 mov eax, dword [ss:rbp+var_4] mov ecx, eax add ecx, 0x1
Increment and Decrement : In Conditionals Pre-Increment int i = 0; if(++i) { //... Post-increment int i = 0; if(i++) { //... Java and C# cannot directly convert an integer to a boolean but in C and C++ the value 0 represents false and any other value represents true (it is language specific). Pre-incrementing or pre-decrement takes place before the expression is evaluated and post-increment or post-decrement takes place after the expression has been evaluated. Exercise Avoid micro-optimisations and let the compiler optimise for you but keep in mind that a pre-increment is an operation that may generate shorter code.
Operator Precedence Precedence Increment and Decrement e++, e--, ++e, --e Mathematical *, /, %, +, - Relational <, >, <=, >=, ==,!= Logical &&, Examples int a = 5 * 2-1; a = 5 * (2-1); a = 10 % 2 // In C#: boolean -> bool boolean m = 2 > 3; m = (2 > 3) && (1 < 5); m = (2 > 3) (1 < 5); boolean a = (10 % 2) + 1 == 1;
Calculating Factorial Iteratively Calculating Factorial Numbers int j = 1; int n = 0; while(n < 10) { switch(n) { case 0: ++n; System.out.println(1); default: System.out.println(j); j = ++n * j; Factorial Relation n! = Recurrence { 1 if n 0 n (n 1)! if n > 0
The Modulo Operator: Listing Even Numbers Displaying Even Numbers int i = 0; do { if(i % 2 == 0) { System.out.println(i); while(++i < 10); Even Number Set Even = {2k k Z The modulo operator (%) returns the remainder of the division of a number. The values that the modulo operator operates on can be of an integer type or a floating point type. Can you think of a reason why we used a do-while loop instead of a while loop? What would happen if the do-while loop would be converted directly to a while loop. How would we print out the odd numbers? Exercise
Shorthand Shorthand operators can be used whenever the operation involves the left-most variable to which the result is assigned to. List of Shorthand Operation Example Operation Shorthand Notation Addition i = i + 2 i += 2 Multiplication i = i * 2 i *= 2 Division i = i / 2 i /= 2 Modulo i = i % 2 i %= 2
Lecture Material Mirrors Materials, such as the slides, the programs used, the exercises and the solutions to the exercises will be posted at: Personal Website