Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the right value <= // true if left value is less than OR EQUAL to the right value > // true if left value is greater than the right value >= // true if left value is greater than OR EQUAL to the right value Logical Operators! // inverts true to false or false to true && // boolean AND // boolean OR Precedence Rules (from highest to lowest priority) - Arithmetic operators (i.e. ++,--,+,-,*,/, ) - Boolean operators (i.e. <, >, <=, >=) - ==,!= - && - Note: Integer values can be used as boolean values - 0 is considered as false and non- zero is considered as true - For this class, we should not use numbers for boolean values. All boolean expressions must be bool types. If- else statements Ability to execute two alternative statements based on the value of a Boolean Expression. if (Boolean Expression) { // code1 else { // code2 - Notice the {. This allows many statements to be executed within a block. - Without {, only the following statement will be executed and other statements are considered outside the block statement. Multi- way If- else Statements Multiple if- else statements can allow the program to execute many branches.
int x = 3; if (x == 1) cout << x equals 1 << endl; else if (x == 2) cout << x equals 2 << endl; else if (x == 3) cout << x equals 3 <<endl; else cout << x does not equal 1, 2, or 3 << endl; Switch statements - Another way to write Multi- way if- else statements are switch statements. - A switch statement can use either an integer type, bool, char, or enum. - The break statement tells the compiler that it s the end of a case. - Without the break statement, the compiler will execute the statements until a break is reached. int a = 3; switch (a) { case 1: cout << x equals 1 << endl; case 2: cout << x equals 2 << endl; case 3: cout << x equals 3 << endl; default: cout << x does not equal 1, 2, or 3 << endl; It is possible to have many cases refer to the same statements. int a = 3; switch (a) { case 1: case 2: cout << x equals 1 or 2 << endl; case 3: cout << x equals 3 << endl; default: cout << x does not equal 1, 2, or 3 << endl;
Embedded if- else statements Within a block statement, another if statement can be written. int x = 7; if (x >= 5) { cout << x > 5 << endl; if (x == 5) { cout << x == 5 << endl; else { cout << x > 5 << end; While loop while (Boolean Expression) { // code The code block will be executed as long as the boolean expression evaluates to true. Do- while Loop do { //code while (Boolean Expression); Useful if you want to execute a block of code at least once. For Loop for (initialization; boolean expression; update) { //code - Legal to leave the init, boolean expression, and/or update statement empty. - Considered bad style to leave any of them blank. Nested Loops Within a loop s block, other loops can be defined. for (int i = 0; i < 10; i++) { cout << i = << i << endl; for (int j = 0; j < 10; j++) { cout << j = << j << endl; The above nested loop is an example of O(n 2 ), where n = 10 in this case.
Continue and break in loops - continue statement can be used to stop the current execution of statements within the block and jump to the next iteration of the current loop. - break statement can be used to break out of the current loop and continue execution after the end of the loop. Call Stack When functions are called, they are maintained in a call stack The call stack consists of one or more frames - An instance of the function. - Contains information such as parameters, return values, and local variables. Each frame also keeps track of the frame pointer for the function that called it. Therefore, when the function finishes execution, it gets popped from the stack and returns to the function that called it so it can continue execution. Recursion When a function contains a call to itself. Factorial N! = N * (N 1) * (N 2) * * 3 * 2 * 1 Note: 0! = 1, 1! = 1 int factorial (int n) { if (n <= 1) // base case return 1; return n * factorial(n 1); Fibonacci f(n) = f(n- 1) + f(n- 2)). f(0) = 0, f(1) = 1, f(2) = 1, f(3) = 2, f(4) = 3, int fibonacci(int n) { if (n == 1) { return 1; if (n == 0) { return 0; retrun fibonacci(n-2) + fibonacci(n-1);
Tail Recursion When there is no other computation to do once a value is returned. int factorialtail(int n, int product) { if (n == 1) return product; return factorialtail(n - 1, product * n); Stack Overflow - Happens when a program crashes due to running out of memory in its call stack. Enumerations - Enumeration is a type whose values are defined by a list of constants of type int. - You can define enumeration types to any int value. enum Month {JAN = 1, FEB = 2, MARCH = 3, APRIL = 4, MAY = 5, JUNE = 6, JULY = 7, AUG = 8, SEP = 9, OCT = 10, NOV = 11, DEC = 12; Note, without defining the values, it starts with 0 and always increments the previous enum by 1. enum Month { JAN, FEB, MARCH, APRIL, MAY, JUNE, JULY, AUG, SEP, OCT, NOV, DEC ; // JAN = 0 à DEC = 11 Note: It s good to define enums in an appropriate header file so other source files may use it. int month = 3; switch (month) case DEC: case JAN: case FEB: cout << It s winter time! << endl; case MARCH: case APRIL: case MAY: cout << It s spring time! << endl; case JUNE: case JULY: case AUG: cout << It s summer time! << endl; case SEP: case OCT:
case NOV: cout << It s fall! << endl; default: cout << Not a valid month << endl;