91.166 Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Computer Science 1620 Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
1 Engineering Problem Solving with C++ An Object Based Approach Chapter 2 Simple C++ Programs.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
Chapter 5: Control Structures II (Repetition)
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
1 Chapter 9 Scope, Lifetime, and More on Functions.
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 While’s – The Basic Idea Set of operations is repeated as.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
C++ Programming: Basic Elements of C++.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CPS120: Introduction to Computer Science Operations Lecture 9.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
CSC 107 – Programming For Science. The Week’s Goal.
Chapter 05 (Part III) Control Statements: Part II.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
Simple C++ Programs Program Structure Constants and Variables
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 INPUT STREAMS ifstream xin; // declares an input stream.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
1 Chapter 9 Scope, Lifetime, and More on Functions.
COMP Loop Statements Yi Hong May 21, 2015.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 5: Control Structures II (Repetition)
Computing and Statistical Data Analysis Lecture 2
Introduction to Programming in Java
Chapter 9 Scope, Lifetime, and More on Functions
2.6 The if/else Selection Structure
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 5: Control Structures II (Repetition)
Programming Fundamental
Presentation transcript:

Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements // Body } If-Then-Else: if (boolean exp) { statements // True part } else { statements // False part } Multi-Way If: if (boolean exp) { statements // Part 1 } else if (boolean exp) { statements // Part 2 } else if (boolean exp) { statements // Part 3 … // and so on } else { // an else part is optional statements // Else part } While (pre-test): while (boolean exp) { statements // Body } Do While (post-test): do { statements // Body } while (boolean exp); For Loop: for (exp1;exp2;exp3) { statements } is equivalent to: exp1; while (exp2) { same statements exp3; } Break: break; causes an exit from the immediately enclosing loop. MODEL PROGRAM #include int add (int x, int y) { int result; result = x + y; return result; } int main () { int a, b, c; cout > a >> b; c = add (a, b); cout << “The answer is “ << c << endl; return 0; } EXPRESSIONS is greater than = is greater than or equal to == is equal to != is not equal to || OR (either side is true) && AND (both sides are true) ! NOT (changes true to false, false to true) % modulus (gives remainder from division) X++ means” use value of X, then increment X” X-- means “use value of X, then decrement X” ++X means “increment X, then use new value” --X means “decrement X, then use new value” X += Y is equivalent to X = X + (Y) (same idea for -=, *=. and /=) CALL BY … int sample (int a, int &b, int c[]); “a” is call by value (just like a regular variable but given a value when the function is called). “b” is call by reference. all operations on “b” actually operate on the variable supplied. “c” is call be reference. all operations on “c” actually operate on the array supplied. ARRAYS // sample declaration int a[4] = {a1, 2, 4, 12 }; // typical use sum = 0; for (i = 0; i < array_size; i++) { sum += array[i]; } // typical function void write array (int a[], int n) { int i; for (i = 0; i < n; i++) { cout << array[i] << endl; } } SIMULATION x = rand () /(RAND_MAX + 1.0); produces a random value between zero and 1 (0 <= value < 1) STRINGS string = a character array with ‘\0’ at the end of the of the data. typical loop: for (i = 0; str[i] != ‘\0’; i++) { } BISECTION SEARCH to find x such that F(x) = 0, first pick an x that is too low (low) and an x that is too high (high) loop pick mid = (low + high) / 2 if low and high are close enough to each other, mid is the answer compute F(mid) if the result is acceptably close to zero, mid is the answer. if F(low) and F(mid) same sign low = mid else high = mid end loop STRUCTURES struct point { double lat, lng; }; // *** note the semi-colon point pnt; // declares variable pnt.lat. = 45; // access lat component

Copyright © 2002, Department of Systems and Computer Engineering, Carleton University INPUT ifstream xin; // declares an input stream object (for reading files) xin.open(string); // attaches the input stream object to the file specified xin >> resetiosflags(ios::ws); // turns off whitespace skipping when reading characters xin >> setw(size_of_char_array) >> string; // prevents overflow when reading strings xin.fail() // produces true if the stream is in the failed state (something has gone wrong) xin.eof() // produces true if the program has tried to read past the end of the file xin.clear() // resets the failure flag xin.ignore (count, ch); // discards input characters until “count” characters have been discarded // or character “ch” has been discarded (whichever comes first) xin.get(array, size, ch); // copies input characters into character array “array” until “size – 1” // characters have been copied or character “ch” is seen. in the second // case character “ch” is not read. xin.get(); // gets the next input character xin.getline(array, size); // copies the next line of input (everything up to the next ‘\n’) into the // character array supplied. the ‘\n’ which ends the input process is // discarded. at most “size – 1” characters will get read (if the line is // too long to be read, the extra characters are just left in the pipeline) OUTPUT ofstream xout; // declares an output stream object (for reading files) xout str2, returns a value greater than zero if str1 == str2, returns zero if str1 < str2, returns a value less than zero int stricmp (const char str1[], const char str2[]); as for “strcmp” but ignores case double atof (const char str[]); converts a string to a floating point value int atoi (const char str[]); converts a string to an integer value int rand(void); returns a “randomly” chosen value between 0 and RAND_MAX – 1 void randomize (void); Borland only – intended to be called once before making use of “rand”. not really necessary. bool isalnum(char ch); returns true if the character is alphanumeric (letter or digit) bool isdigit(char ch); returns true if the character is a digit (one of ‘0’ through ‘9’) bool isalpha(char ch); returns true if the character is a letter char toupper(char ch); returns the uppercase equivalent of a character