Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

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

bool bool - either true or false

Announcements. HW 0 due tonight (check that it compiles on CSE labs) (make sure you submit the code part)

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

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

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

5. Control Statements

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

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

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Decision Making in C

Chapter 3. More Flow of Control

Conditionals and Recursion. Python Part 4

conditional statements

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

5. Selection: If and Switch Controls

Chapter 4: Making Decisions

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Chapter 4: Making Decisions

The following expression causes a divide by zero error:

CS150 Introduction to Computer Science 1. Logical Operators and if/else statement

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

Introduction. C provides two styles of flow control:

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

ISA 563 : Fundamentals of Systems Programming

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

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

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

EECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage

C/C++ Programming Lecture 7 Name:

LECTURE 04 MAKING DECISIONS

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

UNIT- 3 Introduction to C++

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

Boolean Algebra Boolean Algebra

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

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

Logical Operators and if/else statement. If Statement. If/Else (4.3)

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

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

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

CSCE 206: Structured Programming in C++

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

CSI33 Data Structures

Topics. Chapter 5. Equality Operators

Information Science 1

Operators. Java operators are classified into three categories:

Information Science 1

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

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

Fundamental of Programming (C)

Flow Control. So Far: Writing simple statements that get executed one after another.

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

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

Highlights. - Making threads. - Waiting for threads. - Review (classes, pointers, inheritance)

Linear execution of statements. CISC 1600/1610 Computer Science I. Alternatives to linear execution. Linear execution of statements

Score score < score < score < 65 Score < 50

AP Computer Science Homework Set 1 Fundamentals

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

Information Science 1

CSCI 1061U Programming Workshop 2. C++ Basics

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Introduction to Programming using C++

Theory of control structures

REPETITION CONTROL STRUCTURE LOGO

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

Chapter 2. C++ Basics

Why Is Repetition Needed?

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

SFU CMPT Topic: Control Statements

Software Design & Programming I

If Control Construct

Information Science 1

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

SELECTION. (Chapter 2)

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

CIS 130 Exam #2 Review Suggestions

Dr. Chuck Cartledge. 10 June 2015

Decisions. Arizona State University 1

Repetition, Looping CS101

G Programming Languages - Fall 2012

Bits, Words, and Integers

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Boolean evaluation and if statements. Making decisions in programs

Add Subtract Multiply Divide

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

Flow Control. CSC215 Lecture

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

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

Review. Relational Operators. The if Statement. CS 151 Review #4

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Transcription:

Branching

Announcements Homework 0: using cin with 10/3 is NOT the same as 3.3333 (directly) With cin, it will stop as soon as it reaches a type that does not match the variable (into which it is storing) (See: cinmismatchtypes.cpp)

Outline if/else statements cont. boolean expressions

bool bool - either true or false C++ lets you change between fundamental types (casting) with ease Q: 0 is false and 1 is true, right? A: 1.

boolean values Sometimes this might cause an error, such as: int x = 2; if(! x>5 ) will be false Why?

boolean values Sometimes this might cause an error, such as: int x = 2; if(! x>5 ) will be false Why? A: order of operations will do the unary operator first (the '!') if (! x>5) will become if ( (!2) > 5)... if ( (!true) > 5)... if ( false > 5)... if (0 > 5)

if statement Code inside an if statement is only run if the condition is true. Need parenthesis (no semi-colon) Indent (See last week: numberguessing.cpp)

if/else statement Immediately after an if statement, you can make an else statement If the if statement does not run, then the else statement will If you do not surround your code with braces only one line will be in the if (and/or else) statement

if/else statement (See: ATM.cpp)

Logical operators These are all the operators that result in a bool: > (greater than), e.g. 7 > 2.5 is true == (equals), e.g. 5 == 4 is false < (less than), e.g. 1 < 1 is false >= (greater than or equal to), e.g. 1 <= 1 is true!= (not equal to), e.g. 8!= 7 is true <= (less than or equal to), e.g. 6 <= 2 is false! (not, negation), e.g.!true is false

Complex expressions Two boolean operators: && is the AND operations is the OR operations

Complex expressions AND operation removes Ts from the result The OR operation adds Ts to the result Evaluate (!p OR q) AND (p) p T T F F q T F T F!p F F T T!p OR q T F T T (!p OR q) AND (p) T F F F

Complex expressions Write an if statement for checking if a variable (int) x is a positive odd number. Hint: You may want to use the remainder (also called modulus) operator (the % sign). For example, 5 % 3 = 2

Complex expressions Humans tend to use the english word OR to describe XOR (exclusive or) We can have our final exam on the scheduled day (May 13) or the last day of class (May 6). Did you think the statement above meant final exams on both days was a possibility?

Complex expressions Write boolean expressions for each of the following truth tables: 2. 1. XOR 3. 4.

Complex expressions int x = 9, y = 7;

Complex expressions If statements for when x...... is between 10 and 20 (inclusive) Cannot say: 10 <= x <= 20 (why?)... is a vowel (x is type char)

Short-circuit evaluation Short-circuit evaluation is when you have a complex bool expression (&& or ) but you don't need to compute all parts. If this is false, then it will not check next (See: shortcircuit.cpp)

Short-circuit evaluation Simple cases of short-circuit: When you have a bunch of ORs if( expression exp exp exp ) Once it finds any true expression, if statement will be true When you have a bunch of ANDs if( expression && exp && exp && exp ) Once it finds any false expression, if statement will be false

Complex expressions Write a single if-statement that is true on the following range of numbers: sample) int i: 3 Answer: if( i == 3) a) int i:... -2, -1, 0 b) int i: 5, 6, 7, 8,... c) int i: 1, 2, 3, 4, 5 d) int i:... -2, -1, 1, 2, 3,... e) int i:... -2, -1, 5, 6, 7,...

Complex expressions Be careful when negating, that you follow De Morgan's Law: bool a, b;!(a OR b) is equivalent to (!a) AND (!b)!(a AND b) is equivalent to (!a) OR (!b) Neither rainy or sunny means Both not rain and not sunny

Nested if statements You can have as many if statements inside each other as you want.

Nested if statements From a truth table perspective, nested loops are similar to AND The previous if code is equivalent to: However, sometimes you want to do other code between these evaluations

Nested if statements (See: bridgeofdeath.cpp)

Scope Where a variable is visible is called its scope Typically variables only live inside the block (denoted with matching { and } ) A variable lives until the block is closed, so inner blocks can see everything from the block it was created inside

Scope (See: scope.cpp)

If... if... else! When in doubt, use parenthesis and blocks! (Some people like to put the first brace after the if, others on a new line) What happens if you have an if if else? (See: ififelse.cpp)

Multiway if/else This is a special format if you put an if statement after an else. This second if statement only is tested when the first if statement is not true (See: grades.cpp)

Multiway if/else (See: vending.cpp)

Switch A switch statement checks to see if a variable has a specific value. Controlling Variable Case label Break statement

Switch If the value of the controlling variable is found in a case label, all code until a break statement is ran (or the switch ends) Switch statements only test equality with case labels (not greater or less than) (See: switch.cpp)

Switch Switch statements can be written as multiway if/else statements. Could use just if statements but else if shows only one of these will run (See: switchtoif.cpp)

Conditional operator We will not use in this class, but if you use other people's code you will encounter Shorthand for an if-else statement (boolean)? [if true] : [if false] Example: max = (x>y)? x : y; (See: max.cpp)

Loops

if/else vs loops if/else statements makes code inside only sometimes run Loops make code inside run more than once Both use boolean expressions to determine if the code inside is run

while loop A while loop tests a bool expression and will run until that expression is false bool exp., no ; (See: whileloop.cpp)

while loop The bool expression is tested when first entering the while loop And! When the end of the loop code is reached (the } to close the loop)

while loop It can be helpful to manually work out what loops are doing and how variables change in each loop iteration This will build an insight into how loops work and will be beneficial when working with more complicated loops

while loop https://www.youtube.com/watch?v=7-nl4jfdlou

while loop 3 parts to any (good) loop: - Test variable initialized - bool expression - Test variable updated inside loop

for loop A for loop is a compacted version of the while loop (the 3 important parts are together) for loops are used normally when iterating over a sequence of numbers (i.e. 1, 2, 3, 4) Initialization boolean expression Update (See: forloop.cpp)

Loops 99 bottles of beer on the wall, 99 bottles of beer! Take one down, pass it around, 98 bottles of beer on the wall! 98 bottles of beer on the wall, 98 bottles of beer! Take one down, pass it around, 97 bottles of beer on the wall! 97 bottles of beer on the wall, 97 bottles of beer! Take one down, pass it around, 96 bottles of beer on the wall!... Write a program to output the above song (See 99beer.cpp)

continue There are two commands that help control loops: continue tells the loop to start over again break stops the loop

continue continue command can be issued to start at the next iteration of a loop doskip true (See: continue.cpp)

break break will exit the current loop doskip true (See: break.cpp)

Infinite loops We talked about this last time... (See: countingsheep.cpp)

Loops to sum Loops allow you to decide how many times a piece of code should run on the fly (i.e. at run time, not compile time) You can either directly prompt the user how many times or make a special value to exit on (See: sumloop.cpp)

Debugging When your program is not working, it is often helpful to add cout commands to find out what is going on Normally displaying the value of your variables will help you solve the issue Find up until the point where it works, then show all the values and see what is different than you expected