Lec3 - 2 Introduction to Conditionals Boolean expressions...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
2. Introduction to Conditionals Boolean expressions The If-Else Construct And, or, not
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What We Cannot Do 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.
Background image of page 2
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. c bx x x q + + = 2 ) (
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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’:)
Background image of page 4
c bx x x q + + = 2 ) ( 2 / b x c = LR The Situation
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Write a fragment that prints “yes” if q(x) increases across the interval and “no” if it does not. Problem 1
Background image of page 6
c bx x x q + + = 2 ) ( 2 / b x c = LR No!
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
c bx x x q + + = 2 ) ( 2 / b x c = LR Yes! Requirement: x c <= L
Background image of page 8
Solution Fragment xc = -b/2; if xc <= L disp(‘Yes’) else disp(‘No’) end
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Write a fragment that prints the maximum value that q(x) attains on the interval. Problem 2
Background image of page 10
c bx x x q + + = 2 ) ( 2 / b x c = LR Maximum at L
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
c bx x x q + + = 2 ) ( 2 / b x c = LR Maximum at R Depends on whether xc is to the right or left of the interval midpoint.
Background image of page 12
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
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Write a fragment that prints “yes” if xc is in the interval and “no” if xc is not in the interval.
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 50

Lec3 - 2 Introduction to Conditionals Boolean expressions...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online