CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Computer Science 1620 Loops.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Lecture 1 The Basics (Review of Familiar Topics).
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Chapter 5: Control Structures II (Repetition)
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
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.
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.
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.
Chapter 05 (Part III) Control Statements: Part II.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Recap……Last Time [Variables, Data Types and Constants]
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Introduction to Computer Programming
CSE 220 – C Programming Expressions.
A mechanism for deciding whether an action should be taken
Chapter 2 Assignment and Interactive Input
Engineering Problem Solving with C++, Etter/Ingber
Control Structures II (Repetition)
Expression Review what is the result and type of these expressions?
Chapter 4: Control Structures I (Selection)
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Chapter 7 Additional Control Structures
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Repetition Control Structure
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Summary of what we learned yesterday
Fundamental Programming
Control Statements Paritosh Srivastava.
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
CSC215 Lecture Control Flow.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational operators and expressions == = != Watch out for the “future equivalence operator”, = Logical operators and expressions ! && || Assignment operators and statements = *= /= %= += -= &= |= Execution control in a nutshell Iteration: increment and decrement operators Loop control statements for, while, do while, continue, break Conditional control statements if, else, else if, operator ?:, switch

CSE 332: C++ execution control statements Expressions vs. Statements A statement can stand alone in a C++ program (compilable) –Or can be part of another statement –Examples include i = 7; if (argc < 3) { usage(); } usage (); cout << “hello, world!” << endl; An expression cannot stand alone –But rather must be part of a statement –Examples include argc < 3 a + b - 7 Some expressions are easily made into statements –By adding a semicolon, the return keyword, etc. –Examples include i++  i++; i < 3  return i < 3;

CSE 332: C++ execution control statements Arithmetic Operators and Expressions Semantics (meaning) varies with the types of the operands Result type also depends on the types of the operands Operators * / % have higher precedence than (binary) + - See pp in textbook (Prata 5 th Edition) Semantics are similar for some operators * + - Watch out: over/under-flow, and some operators differ more / Modulus (remainder) operator can be very useful if (a % 2 == 0) { // even number } else { // odd number }

CSE 332: C++ execution control statements Relation Operators and Expressions Relational operators: == = != Very useful for control execution Test relationships between values of variables Return type is bool Can be used to build complex Boolean expressions Watch out for the “future equivalence operator”, =, when you meant ==

CSE 332: C++ execution control statements Logical Operators and Expressions Logical operators (high  low precedence) ! && || Input and result types are both bool Often used with relational operators Help to build logical formulas Very useful for execution control

CSE 332: C++ execution control statements Assignment Operators and Statements Basic assignment operator is = –Statement made up of two parts (one on either side of the operator) –Left hand side is called an l-value Must be mutable (non-const variable visible in scope) –Result of right hand side is called an r-value Any well formed expression of type matching l-value –Example a = b + 9; // b may be const, a cannot be. Other assignment operators do some other operation on r- value, then assign the result –May be slightly more efficient than expanded form –May introduce or reduce operator side effects –Examples *= /= %= += -= &= |=

CSE 332: C++ execution control statements Execution Control in a Nutshell Loops: return control to top of loop, under a given condition –Overall structure (details differ between different kinds of loops): Conditionals: select from alternatives, under given conditions –Overall structure (details differ between different kinds of branches): initialization test loop body increment test1 branch1test2 branch2alternative (case b)(case a) (case c) (default/else)

CSE 332: C++ execution control statements Iteration, Progress, Termination Iteration is fundamental to loop control –Loop is a (hopefully ;-) bounded series of steps Iteration progresses through a range of steps –Loop is unbounded if no progress is made For example if someone left off the increment: ++i; –Loop is unbounded if range does terminate For example, if test is badly formed: while (c < 32767) Three main questions to answer –How does the loop’s range start –How does the loop’s range stop –How to move from one step to the next Many kinds of ranges –Examples include integer, exponent, logical

CSE 332: C++ execution control statements Increment and Decrement Operators Useful for iteration –Increase or decrease a pointer, index, or value –Steps through a logical “range” (more later on STL) A few details that we’ll revisit throughout the course –Increment ( ++ ) increases value/position/index –Decrement ( -- ) decreases value/position/index –Prefix form is called with operator to left of operand ++i --i –Postfix form is called with operator to right of operand i++ i-- –Prefix form takes no arguments when declared operator++ () operator–- () –Postfix form takes an int argument when declared operator++ (int) operator–- (int) –Prefix returns “after” value, postfix returns “before” value

CSE 332: C++ execution control statements Loop Control Statements For loops While loops Do loops initialization test loop body increment test loop body (put increment there) test

CSE 332: C++ execution control statements For Loop with Simple Integer Range for (int i = 0; i < 10; ++i) { cout << i << endl; } Output:

CSE 332: C++ execution control statements For Loop with Exponential Range for (int i = 1; i < 1024; i *= 2 ) { cout << i << endl; } Output:

CSE 332: C++ execution control statements While Loop with Logical Range int balance = 2000; while (balance > 0) { int withdrawal; cout << "your balance is now: " << balance << endl << "how much would you like to withdraw? "; cin >> withdrawal; balance -= withdrawal; } Output: your balance is now: 2000 how much would you like to withdraw? 1270 your balance is now: 730 how much would you like to withdraw? 700 your balance is now: 30 how much would you like to withdraw? 30

CSE 332: C++ execution control statements Do Loop with Logical Range char c; // can be in any scope visible to loop do { cout << "Enter Q or q to quit: " << endl; cin >> c; } while (c != 'Q' && c != 'q'); Output: Enter Q or q to quit: a Enter Q or q to quit: B Enter Q or q to quit: q

CSE 332: C++ execution control statements Converting Between For and While Loops for (int i = 1; i < 1024; i *= 2) { cout << i << endl; } int i = 1; while (i < 1024) { cout << i << endl; i *= 2; }

CSE 332: C++ execution control statements Converting Between Do and While Loops char c; // simple declaration fine, could be inside do { cout << "Enter Q or q to quit: " << endl; cin >> c; } while (c != 'Q' && c != 'q'); char c = 0; // must initialize outside (why?) while (c != 'Q' && c != 'q') { cout << "Enter Q or q to quit: " << endl; cin >> c; }

CSE 332: C++ execution control statements Continue and Break Statements Two additional statements used for loop control Put them within body of loop to control execution Continue statement –Skips the rest of the loop –Control moves directly to loop’s test statement –Useful when testing validity of user inputs, for example –Syntax: continue; Break statement –Leaves the loop mid-way through its body –Control moves to the next statement after the loop –Useful when conditions can change mid-way through loop –Syntax: break;

CSE 332: C++ execution control statements If, Else, Conditional Control Statements If / else if / else statements if (a < b) { cout << a << “ < ” b << endl; } else if (a > b) { cout ” b << endl; } else { cout << a << “ == ” b << endl; } Conditional operator return (a < b) ? a : b;

CSE 332: C++ execution control statements Switch Statements Use when all cases are based on values of a single expression – notice default case, break statements switch (num % 4) { case 0: cout << num << " is divisible by both 2 and 4" << endl; break; case 2: cout << num << " is divisible by 2 but not by 4" << endl; break; default: cout << num << " is not divisible by 2 or by 4" << endl; break; }

CSE 332: C++ execution control statements Summary: Execution Control in C++ Expressions –Built from operators and operands Statements –Built from expressions (operators, operands) Execution control determines order in which program statements are run Conditional control statements allow choices Loop control statements allow repetition Things to think about in every program –When is a branch or loop executed? –If it’s a loop, when does it stop? –If it’s a loop, is progress made each time?

CSE 332: C++ execution control statements For Next Time Topic: C++ functions Assigned reading – pages – pages