220 likes | 240 Views
This document by Hector M. Lugo-Cordero from September 3, 2008, explores various flow control concepts in programming, such as conditions, selections, loops, and jump statements. It covers topics like truth tables, logical operators, if statements, switch cases, cycles including while, do-while, for loops, and jump statements like break, continue, go-to, try, and catch.
E N D
Flow Control By Hector M Lugo-Cordero September 3, 2008 Department of Electrical and Computer Engineering
Outline • Conditions • Selection • ? • if • if … else • If … else if … else • Switch • Cycles • While • Do while • For • Jump Statements • Break • Continue • Go to • Try and catch The Department of Electrical and Computer Engineering
Conditions • Conditions are expressions that can be either true or false • x > 0 is true if x is greater than zero • Operators • Equal ( == ): x == y x equals y • Greater ( > ): x > y x is greater than y • Less ( < ): x < y x is smaller than y • Not equal ( != ): x != y x is not equal to y • Greater or equal ( >= ): x >= y • Less or equal ( <= ): x <= y The Department of Electrical and Computer Engineering
Conditions (cont.) • Complex conditions • Test more than one condition • And ( && ) is true if and only if both conditions are true • x >= 0 && x <= 9 is true if x is between 0 and 9 inclusive • Or ( || ) is true if one of the conditions is true • x < -3 || x > 10 is true if x is smaller than -3 or greater than 10 • Not ( ! ) Negates the result of an expression The Department of Electrical and Computer Engineering
Truth Tables The Department of Electrical and Computer Engineering
Selections • Selections alter the flow of the program which is typically sequential (line by line) • Selections are made according to the value of a Boolean expression The Department of Electrical and Computer Engineering
Selection (?) • Syntax • <var> = <condition> ? <true_value> : <false_value> • Execution • Evaluates <condition> • If true stores <true_value> into <var> • Else stores <false_value> into <var> • Example • int x = -9; • int y = x < 0 ? -1*x : x; • cout << “|” << x << “| = ” << y << endl; • Prints: |-9| = 9 The Department of Electrical and Computer Engineering
If • Syntax • if(<condition>){ … } • else if(<condition>){ … } //optional • else //optional • Execution • If the condition is true then the if block is executed • Optional • One can add else if to test other conditions in case the first is not met. The first that becomes truth will be executed and the others won’t be tested. • Else can be added if none of the conditions is met then execute the else code. The Department of Electrical and Computer Engineering
If (cont.) • Example • int x = 0; • cin >> x; • if(x == 0){ • cout << “The number is 0” << endl; • } • else if(x > 0){ • cout << “The number is positive” << endl; • } • else{ • cout << “The number is negative” << endl; • } The Department of Electrical and Computer Engineering
Switch • Syntax • switch(<var>) • { • case <value1>: • … • break; • case <value2>: • … • break; • … • default: • } • Execution • Evaluates the variable <var> • Executes the code in the block of the corresponding value • If no break is found it continues to evaluate each case • Default is executed if no case is true or no break is found The Department of Electrical and Computer Engineering
Switch (cont.) • Example • int x = 9; • x = x % 2; • switch(x) • { • case 0: • cout < “x is even” << endl; • x = 1; • break; • case 1: • cout << “x is odd” << endl; • break; • default: • cout << “This should never happen” << endl; • } The Department of Electrical and Computer Engineering
Cycles • Repeats a single action or a block as long as the condition evaluated is true • It may vary the behavior depending on what cycle is used The Department of Electrical and Computer Engineering
While • Syntax • while(<condition>){ • … • } • Execution • Evaluates the condition and if true executes the while block • Examples • int i = 0; • while(i < 10){ • cout << i << endl; • ++i; • } • while(1){ … } or while(true) { … } The Department of Electrical and Computer Engineering
Do While • Syntax • do{ • … • }while(<condition>); • Execution • Executes the do-while block and then checks the condition (guaranteed that will execute at least once) • Examples • int sum = 0; • do{ • int x = 0; • cout << “Enter the next non-zero number to add to the sum: ”; • cin >> x; • sum += x; • }while(x != 0); • cout << “The sum is “ << sum << endl; The Department of Electrical and Computer Engineering
For • Syntax • for(<data_type> <var> = <init_value>; <condition>; <increment>){ … } • Execution • Starts the variable <var> in <init_value> and checks the condition to see if it is true. • If so it executes the for block and then the increment. • Else exists the for loop. • Examples • for(int i = 0; i < 100; ++i){ cout << i << endl;} • for(double j = 0.0; j < 5; j += 0.5){ … } • for(long k = 1500; k >= 0; --k){ … } • int x = 3; • for(; x > 0; --x); //just runs 3 times and does nothing • for(; ;++x) • for( ; ; ) The Department of Electrical and Computer Engineering
Jump Statements (discouraged?) • Goto: jumps to a specific location in the code • Continue: Skips the rest of the current iteration in a cycle and passes to the check the condition • Break: exits the current control structure (cycle) • Syntax • goto <label> • continue; • break; The Department of Electrical and Computer Engineering
Jump Statements (discouraged?) • Example • int x = 0 • theloop: • cout << x << endl; • if(x == 5) //there is no { and } why ? • continue; • else if(x == 10) • break; • else{ //now there are the { and } • ++x; • goto theloop; • } The Department of Electrical and Computer Engineering
Try and catch • Syntax: • try{ • … throw <value> ... • } • catch(<data_type> <var_name>){ • … • } • Execution: • Tries to execute the block inside of the try • If an exception is thrown the catch block is executed. An exception is an error that shouldn’t occur and causes your program to crash The Department of Electrical and Computer Engineering
Try and catch (cont.) • Example: • try{ • if(x == 0) • throw “Division by zero exception”; • else • div = y/x; • } • catch(char* exp){ • cerr << exp << endl; • } • Note that we can throw strings, int, etc. • Later we will develop our own exceptions The Department of Electrical and Computer Engineering
Try and catch (cont.) • // bad_alloc standard exception • #include <iostream> • #include <exception> • using namespace std; • int main () { • try { • int* myarray= new int[1000]; • } • catch (exception& e) { • cerr << "Standard exception: " << e.what() << endl; • } • return 0; • } The Department of Electrical and Computer Engineering
Try and catch (cont.) The Department of Electrical and Computer Engineering
Questions? ? The Department of Electrical and Computer Engineering