Discussion 4 eecs 183 Hannah Westra.

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

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Logical Operators and Conditional statements
Selection. Structured Components There are three basic constructs that can be used to build programs Sequence One statement follows another. Selection.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Flow of Control Part 1: Selection
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Decision Statements, Short- Circuit Evaluation, Errors.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Repetition Statements
The if…else Selection Statement
Week 3 Part 2 Kyle Dewey.
CprE 185: Intro to Problem Solving (using C)
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
COMP 14 Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
The switch Statement, and Introduction to Looping
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
Control Structures – Selection
Arrays, For loop While loop Do while loop
Chapter 4: Control Structures I (Selection)
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Selection Statements.
Chapter 4: Control Structures I (Selection)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
Chapter 3 Debugging Section 3.4
Welcome back! October 11, 2018.
Controlling Program Flow
How to allow the program to know when to stop a loop.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Chapter 13 Control Structures
Presentation transcript:

Discussion 4 eecs 183 Hannah Westra

Upcoming Deadlines Assignment 2 due September 29th (this Friday) Project 2 will be due October 7th (a week from Friday) Get started early!

Class Exercise: Nested Conditionals Take 5 minutes to try to write out the following scenario in pseudocode – we will turn it into full code as a class Scenario: It costs 10000 dollars to go on a trip to Tuscany, and 7000 dollars to go on a trip to London Your user will run your program and you will read in how much money the user has There is a function called doYouHaveVacationTime() that will return true or false whether the user has vacation time (ask the user) You can’t travel anywhere unless you have vacation time – you will watch Netflix at home instead If you can go to Tuscany you will; London is your second choice; if you can’t afford either you will go somewhere in the US

Short-circuit evaluation Because of short-circuit evaluation, if the first element in an or statement conditional evaluates to true, the rest of the conditional won’t even be checked What will print??

Find the complement! (cats < dogs) || (dogs < guineaPigs)

Comparing doubles You have to be careful when comparing doubles, since their values are not exact Use: if (fabs(x - y) < .0001) instead This is taking the absolute value (in float/double form) of the difference between x and y This is essentially the same as comparing the two for equality, but you have to check that the result is LESS THAN .0001

Comparing strings You can use the comparison operator with strings (==) Make sure you understand that case matters “Apple” will not be evaluated as equal to “apple” The comparison does a character-by-character comparison of each ascii value Capital “A” is less than lowercase “a” in the ascii table, therefore “Apple” < “apple” A longer string will evaluate as greater than a shorter string Apple1 < Apple2 Apple111 < Apple2

Switch Statements: Switch statements are a simpler way to organize code if all the else if statements have the same kind of condition For instance: All the else if are checking if x == y, where x and y are variable (can change) Each case is like an else if statement, but simpler and with less code to type There is a default case, which works like an else statement

Output: n is 1

Switches A switch statement can only have an integer or char value as the condition If you are comparing strings, a switch statement is not the way to go – use branching instead The ‘break’ tells the program that the work for that case is done, and the switch statement should be exited What happens if the break isn’t there? The program execution will fall through to the next case of the switch statement

Output: n is 1 n is something else A break stops the code so it doesn’t fall through to the next case What if we take out the default?? Output: n is 1 n is something else

Switch vs. If Else Readability: Errors: -switch statements are very readable -think about having many statments of each!  Errors: -harder to create bugs in coding logic with switch statements  Generality: -switch syntax limits the context you can use them in -cannot switch on doubles, strings, or anything but a char or int Efficiency: -switch typically runs faster -not a huge different with modern compilers

What is a loop?

A block of code: -Executed repeatedly -Until a certain condition is met

Compound Assignment Operators += -= %= /= *= Variable = variable + (expression) ~~ is the same as~~ Variable += expression

While loops condition Loop body update Event controlled—know how many times the block of code will be executed while I still have candy Loop body update

While loops What prints? Output: 1 2 3 4 First, what is this operator called? Pre-increment Output: 1 2 3 4

What prints? Output: 1 2 3 4

Questions?

Upcoming Deadlines Assignment 2 due Jan 29th (this Friday) Project 2 will be due October 7th (a week from Friday) Get started early!