Flow Control in C++ 1 CS 16: Solving Problems with Computers I Lecture #4

Similar documents
Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

C++ Basics 1 CS 16: Solving Problems with Computers I Lecture #3

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Increment and the While. Class 15

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Pre- Defined Func-ons in C++ Review for Midterm #1

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Information Science 1

Quiz Determine the output of the following program:

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

CIS 130 Exam #2 Review Suggestions

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Information Science 1

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Lec 3. Compilers, Debugging, Hello World, and Variables

C/C++ Programming Lecture 7 Name:

Computational Physics - Fortran February 1997

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

REPETITION CONTROL STRUCTURE LOGO

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Welcome to Solving Problems with Computers I

Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

CSCI Wednesdays: 1:25-2:15 Keller Thursdays: 4:00-4:50 Akerman 211

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

CSI33 Data Structures

C++ Support Classes (Data and Variables)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CIS 110: Introduction to Computer Programming

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Fundamentals of Structured Programming

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Getting started with C++ (Part 2)

C++ Programming: From Problem Analysis to Program Design, Third Edition

Announcements. Homework 0 out! Coming soon: 1. remote access 2. programming instructions 3. lab 1 (graded!) this week

Chapter 3. More Flow of Control

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

INTRODUCTION TO COMPUTER SCIENCE - LAB

QUIZ: What value is stored in a after this

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Conditionals: Making Choices

Building Java Programs

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

B. Subject-specific skills B1. Problem solving skills: Supply the student with the ability to solve different problems related to the topics

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Information Science 1

Add Subtract Multiply Divide

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Chapter 4 - Notes Control Structures I (Selection)

Lecture # 6. Repetition. Review. If Else Statements Comparison Operators Boolean Expressions Nested Ifs. Switch Statements.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Announcements. Lab 1 this week! Homework posted Thursday -Due next Thursday at 11:55pm

Building Java Programs

Introduction. C provides two styles of flow control:

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

CS Exam 2 Study Suggestions

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Transcription:

low Control in C++ 1 CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB

Announcements Homework #3 due today Please take out any staples or paper clips Lab #2 is due on riday A NOON! Use submit.cs Class is closed to new registrakon No more switching lab Kmes Re: Homeworks Please mark your papers cleanly and clearly, especially if you are using pencil We will post grades for HW1, HW2, Lab1 by end of the week on GauchoSpace Re: A Office hours A Magzhan Zholbaryssov has changed his office hours to uesday 8am - 10am 10/4/16 Matni, CS16, a16 2

Lecture Outline Simple low of Control I/ELSE Statements Review of Boolean Operators ruth ables Loops While Do- While or Notes on Program Style 10/4/16 Matni, CS16, a16 3

low of Control Another way to say: he order in which statements get executed Branch: (verb) How a program chooses between 2 alternazves Usual way is by using an if- else statement Example: Program has to calculate taxes owed to the IRS axes owed are 20% of income, if income < $30,000 OR they are 25% of income, if income >= $30,000 How would a program do this calcula2on? 10/4/16 Matni, CS16, a16 4

I/ELSE Statements Recall the general syntax of I/ELSE statements from earlier courses: if (Boolean expression) true statement else false statement If the expression is RUE, then only the true statement gets executed 10/4/16 Matni, CS16, a16 5

ImplemenZng I/ELSE Statements As simple as: in C++ if (income > 30000) taxes_owed = 0.30 * 30000; else taxes_owed = 0.20 * 30000; 10/4/16 Matni, CS16, a16 6

I/ELSE in C++ o do addizonal things in a branch, use the { } brackets to keep all the statements together if (income > 30000) { taxes_owed = 0.30 * 30000; category = RICH ; alert_irs = true; } // end if part of the statement else { taxes_owed = 0.20 * 30000; category = POOR ; alert_irs = false; } // end else part of the statement Groups of statements (somezmes called a block) kept together with { } 10/4/16 Matni, CS16, a16 7

Review of Boolean Expressions: AND, OR, NO Since flow control statements depend on Booleans, let s review some related expressions: AND operator (&&) (expression 1) && (expression 2) rue if both expressions are true OR operator ( ) Note: no space between each character! (expression 1) (expression 2) rue if either expression is true NO operator (!)!(expression) alse, if the expression is true (and vice versa) 10/4/16 Matni, CS16, a16 8

ruth ables for Boolean OperaZons X Y X && Y AND OR NO X Y X Y X! X IMPORAN NOES: 1. AND and OR are not opposites of each other!! 2. AND: if just one condizon is false, then the outcome is false 3. OR: if at least one condizon is true, then the outcome is true 4. AND and OR are commutakve, but not when mixed (so, order malers) X && Y = Y && X X && (Y Z) is NO = (X && Y) Z 10/4/16 Matni, CS16, a16 9

Order of OperaZon for Booleans It s easiest to use parentheses when expressing Boolean condizons Makes it less confusing for later debug, etc If parenthesis are omiled from Boolean expressions, the default precedence of operazons is: Perform! operazons first Perform relazonal operazons such as < next Perform && operazons next Perform operazons last 10/4/16 Matni, CS16, a16 10

Precedence Rules on OperaZons in C++ 10/4/16 Matni, CS16, a16 11

Examples of I Statements if ( (x >= 3) && ( x < 6) ) y = 10; he variable y will be assigned the number 10 only if the variable x is equal to 3, 4, or 5 if ( (x == 3) ( x < 0) ) y = 10; he variable y will be assigned the number 10 if the variable x is either equal to 3 or if it is a negazve number if!(x > 5) y = 10; he variable y will be assigned the number 10 if the variable x is NO larger than 5 (i.e. if x is 4 or smaller) Note: NO operators can be confusing, so use them sparingly 10/4/16 Matni, CS16, a16 12

ranslazng InequaliZes from Math into C++ Be careful translazng inequalizes to C++ If the Math expression is if x < y < z, then it translates into C++ as: if ( ( x < y ) && ( y < z ) ) NO if ( x < y < z ) How would you translate the following Math expressions? b 2 4ac x 3 x 2 + 1 0 10/4/16 Matni, CS16, a16 13

Beware: = vs == ' = ' is the assignment operator Used to assign values to variables Example: x = 3; '= = ' is the equality operator Used to compare values Example: if ( x == 3) he compiler will actually accept this logical error: if (x = 3) It s an error of logic, not of syntax But it stores 3 in x instead of comparing x and 3 Since the result is 3 (non- zero), the expression is true 10/4/16 Matni, CS16, a16 14

Simple Loops 1 while We use loops when an aczon must be repeated C++ includes several ways to create loops while, for, do while, etc he while loop example: int count_down = 3; while (count_down > 0) { cout << "Hello "; count_down - = 1; } Output is: Hello Hello Hello 10/4/16 Matni, CS16, a16 15

he do- while loop Executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given Boolean condizon at the end of the block. So, unlike the while loop, the Boolean expression is checked aaer the statements have been executed int flag = 0; do { cout << "Hello "; flag - = 1; } while (flag > 0); Why is there a semicolon??!? Output is: Hello Simple Loops 2 do- while 10/4/16 Matni, CS16, a16 16

he for loop Simple Loops 3 for Similar to a while loop, but presents parameters differently. Allows you to inizate a counzng variable, a check condizon, and a way to increment your counter all in one line. for (counter declarazon; check condizon statement; increment rule) { } for (int count = 1; count < 5; count++) { cout << "Hello "; } Output is: Hello Hello Hello Hello 10/4/16 Matni, CS16, a16 17

Increments and Decrements by 1 Note that: x += 1 is equivalent to x++ is equivalent to x = x + 1 NOE: he ++ and - - operators only work for inc/dec by 1. Note that: x - = 1 x- - x = x - 1 is equivalent to is equivalent to he other operators can create inc/dec by any number 10/4/16 Matni, CS16, a16 18

Infinite Loops Loops that never stop to be avoided! Your program will either hang or just keep spewing outputs for ever he loop body should contain a line that will eventually cause the Boolean expression to become false Example: Goal: Print all posizve odd numbers less than 6 x = 1; while (x!= 6) { cout << x << endl; x = x + 2; } What simple fix can undo this bad design? while ( x < 6) 10/4/16 Matni, CS16, a16 19

Notes on Program Style he goal is to write a program that is: easier to read easier to correct easier to change Items considered a group should look like a group Use the { } well Indent groups together as they make sense Make use of comments // for a single line comment /*. */ for mulzple line comments If a number comes up o}en in your program (like φ = 1.61803), consider declaring it as a constant at the start of the program: const double PHI = 1.61803; Golden RaZo! Constants, unlike variables, cannot be changed by the program Constants can be int, double, char, string, etc 10/4/16 Matni, CS16, a16 20

O DOs Readings he rest of Chapter 3, of textbook Homework #4 Due on hursday, 10/6 and submit in class Has a programming queszon that requires planning ahead! Lab #2 Due riday, 10/7, at noon 10/4/16 Matni, CS16, a16 21

10/4/16 Matni, CS16, a16 22