Insight Through Computing 3. Introduction to Conditionals Boolean expressions The If-Else Construct And, or, not.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Python Programming Language
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Insight Through Computing More on Conditionals Nested if’s Multiple Alternatives Top-Down Design.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Visual C++ Programming: Concepts and Projects

Lesson Objective: I can…
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
An Introduction to Programming Using Alice Boolean Logic.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Solutions to Equations and Inequalities Lesson 7.01.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
1 2. Program Construction in Java. 2.4 Selection (decisions)
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Algorithms Writing instructions in the order they should execute.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
Logical Thinking CS 104 9/12/11. Agenda Today  College Board survey reminder  Note: Simple “how to” guide on Scratch posted on eLearning  Review HW.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Solution of Mid. Term 2009 Consider the following C++ declarations and assignments. int i, j, k ; float x, y ; char c ; bool test ; i = 35 ; j= 5 ; k =
7-1 boolean Data Type George Boole ( ) boolean variables may have only two values, true or false You define boolean fields or boolean local variables.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Making Choices with if Statements
Chapter 4: Making Decisions.
Section 7.1 Logical Operators
JavaScript: Control Statements I
Topics The if Statement The if-else Statement Comparing Strings
Introduction To Robot Sensors
Expressions and Control Flow in JavaScript
Topics The if Statement The if-else Statement Comparing Strings
And now for something completely different . . .
If statement.
Computers & Programming Languages
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Pages:51-59 Section: Control1 : decisions
Selection Statements.
Computer Science Core Concepts
Python Programming Language
Expressions.
Logic of an if statement
Understanding Conditions
Relational Operators.
Pages:51-59 Section: Control1 : decisions
Nested if’s Multiple Alternatives
L3. Introduction to Conditionals
Presentation transcript:

Insight Through Computing 3. Introduction to Conditionals Boolean expressions The If-Else Construct And, or, not

Insight Through Computing What We Cannot Do So Far If the value of the arithmetic expression Dice1 + Dice2 is seven, then increase the value of the variable GamesWon by one. We cannot make a computation contingent upon other things.

Insight Through Computing The If-Else Construct Solves this Problem We will introduce this language feature by solving problems about the behavior of a given quadratic on a given interval L <= x <= R.

Insight Through Computing Assume Variables b,c,L,R are Initialized E.g., b = input(‘Enter b’:) c = input(‘Enter c’:) L = input(‘Enter L’:) R = input(‘Enter R’:)

Insight Through Computing LR The Situation

Insight Through Computing Write a fragment that prints “yes” if q(x) increases across the interval and “no” if it does not. Problem 1

Insight Through Computing LR No!

Insight Through Computing LR Yes! Requirement: x c <= L

Insight Through Computing Solution Fragment xc = -b/2; if xc <= L disp(‘Yes’) else disp(‘No’) end

Insight Through Computing Write a fragment that prints the maximum value that q(x) attains on the interval. Problem 2

Insight Through Computing LR Maximum at L

Insight Through Computing LR Maximum at R Depends on whether xc is to the right or left of the interval midpoint.

Insight Through Computing Solution Fragment xc = -b/2; Mid = (L+R)/2; if xc <= Mid maxVal = R^2 + b*R + c else maxVal = L^2 + b*L + c end

Insight Through Computing Write a fragment that prints “yes” if xc is in the interval and “no” if xc is not in the interval. Problem 3

Insight Through Computing LR No! Because xc < L

Insight Through Computing LR No! Because R < xc

Insight Through Computing LR Yes! Because L <= xc and xc <= R

Insight Through Computing Solution Fragment xc = -b/2; if (L <= xc) && (xc <= R) disp(‘Yes’) else disp(‘No’) end Illegal: L <= xc <= R

Insight Through Computing Saying the Opposite xc is in the interval [L,R] if L <= xc and xc <= R xc is not in the interval [L,R] if xc < L or R < xc

Insight Through Computing Another Solution Fragment xc = -b/2; if (xc < L) || (R < xc) disp(‘No’) else disp(‘Yes’) end

Insight Through Computing Solution Fragment xc = -b/2; if (L <= xc) && (xc <= R) disp(‘Yes’) else disp(‘No’) end

Insight Through Computing The if-else Construct if end else boolean expression Commands to execute if the expression if TRUE Commands to execute if the expression if FALSE

Insight Through Computing Boolean Expressions Their value is either true or false. Made up of comparisons that are either true or false. Connected by logical operators: and, or, not (xc < L) || (R < xc)

Insight Through Computing Boolean Expressions (xc < L) || (R < xc) Their value is either true or false. Made up of other (simpler) boolean expressions that are connected by boolean operators: and, or, not

Insight Through Computing Arithmetic Expressions Their value is a number. Made up of other (simpler) arithmetic expressions that are connected by arithmetic operators: +, -. *. / (x+3)*(y-z)

Insight Through Computing Relational Operators < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to ~= Not equal to

Insight Through Computing The And Operator && && FFF FT F T F F T T T

Insight Through Computing The Or Operator || || FFF FT T T F T T T T

Insight Through Computing The not Operator ~ ~ FT T F

Insight Through Computing Question Time X = 6; Y = 8; If X < Y Y = Y/2; else X = X/2; end What is the value of X and Y after the following script is executed: A: X is 3 and Y is 4 B: X is 6 and Y is 8 C: X is 5 and Y is 4 D: X is 3 and Y is 8

Insight Through Computing Logical operators (&&, ||) “short-circuit” a > b && c > d true Go on a > b && c > d false Stop Entire expression is false since the first part is false A && condition short- circuits to false if the left operand evaluates to false. A || condition short- circuits to __________ if _________________ ___________________