By Hector M Lugo-Cordero September 3, 2008

Slides:



Advertisements
Similar presentations
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Advertisements

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4 Program Control Statements
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Computer Programming -1-
The Ohio State University
Branching statements.
C ++ MULTIPLE CHOICE QUESTION
Introduction to Computer Programming
Review 1.
Chapter 3 Control Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Repetition Control Structure in C++ Program
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Why exception handling in C++?
Engineering Problem Solving with C++, Etter/Ingber
Programming Fundamentals
Control Structures - Repetition
For & do/while Loops.
Additional Control Structures
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Repetition Control Structure
Chapter 6: Repetition Statements
Summary Two basic concepts: variables and assignments Basic types:
Computing Fundamentals
If Statements.
Chapter 4: Control Structures I (Selection)
Control Structures Part 3
2.6 The if/else Selection Structure
Branching statements Kingdom of Saudi Arabia
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Presentation transcript:

By Hector M Lugo-Cordero September 3, 2008 Flow Control By Hector M Lugo-Cordero September 3, 2008 Department of Electrical and Computer Engineering

The 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

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

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

The Department of Electrical and Computer Engineering Truth Tables A B A && B A || B !A False True The Department of Electrical and Computer Engineering

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 6

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 7

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 8

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 9

The Department of Electrical and Computer Engineering Switch Syntax switch(<var>) { case <value1>: … break; case <value2>: 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 10

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; default: cout << “This should never happen” << endl; } The Department of Electrical and Computer Engineering 11

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 12

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 13

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; 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 14

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 15

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 16

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 17

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 18

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 19

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 20

The Department of Electrical and Computer Engineering Try and catch (cont.) Exception Description bad_alloc thrown by new on allocation failure bad_cast thrown by dynamic_cast when fails with a referenced type bad_exception thrown when an exception type doesn't match any catch The Department of Electrical and Computer Engineering 21

The Department of Electrical and Computer Engineering Questions? ? The Department of Electrical and Computer Engineering