Lec4 - 4. More on Conditionals Nested if's Multiple...

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

View Full Document Right Arrow Icon
4. More on Conditionals Nested if’s Multiple Alternatives
Background image of page 1

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

View Full DocumentRight Arrow Icon
Recall the if-else “Template” if end else boolean expression Commands to execute if the expression if TRUE Commands to execute if the expression if FALSE
Background image of page 2
Question Time Variables a, b, and c have whole number values. True or false: This fragment prints “Yes” if there is a right triangle with side lengths a, b, and c and prints “No” otherwise. A. True B . False if a^2 + b^2 == c^2 disp(‘Yes’) else disp(‘No’) end
Background image of page 3

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

View Full DocumentRight Arrow Icon
a = 5; b = 3; c = 4; if a^2 + b^2 == c^2 disp(‘Yes’) else disp(‘No’) end Prints “no” even though we have: 5 4 3
Background image of page 4
if a^2 + b^2 == c^2 disp(‘Yes’) else disp(‘No’) end The boolean expression should be true if a 2 +b 2 =c 2 or a 2 +c 2 =b 2 or b 2 +c 2 =a 2 is true. a^2 + b^2 == c^2 (a^2+b^2==c^2) || (a^2+c^2==b^2) || (b^2+c^2==a^2)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Developing “If” Solutions Illustrate the thinking associated with the design of if statements. The methodology of stepwise refinement . Two examples. ..
Background image of page 6
Write a script that solicits a positive integer Y and prints the number of days in year Y as determined by the Gregorian calendar. Problem 1
Background image of page 7

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

View Full DocumentRight Arrow Icon
Leap Year Rule A non-century year is a leap year if it is divisible by 4. A century year is a leap year only if it is divisible by 400.
Background image of page 8
Will Need the Built-In Function rem 0 7 56 3 6 15 rem(a,b) b a The value of rem(a,b) is the remainder when the value of a is divided by the value of b. (Assume a, b are whole numbers.)
Background image of page 9

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

View Full DocumentRight Arrow Icon
Input y. If y is not divisible by 100 Use the non-century year rule. Otherwise Use the century year rule. “Pseudocode” Solution
Background image of page 10
y = input(‘Enter the Year:’); if rem(y,100) ~= 0 % y is not a multiple of 100 Use the non-century rule else % y is a multiple of 100 Use the century rule end Refine…
Background image of page 11

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

View Full DocumentRight Arrow Icon
Refine the If-Box % y is not a multiple of 100 Use the non-century rule % y is not a multiple of 100 If y is divisible by 4 Print 366 Otherwise Print 365
Background image of page 12
% y is not a multiple of 100 if rem(y,4)==0 % y is divisible by 4 disp(‘366’) else % y is not divisible by 4 disp(‘365’) 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
y = input(‘Enter the Year:’); if rem(y,100) ~= 0 % y is not a multiple of 100 Use the non-century rule else % y is a multiple of 100 Use the century rule end Refine… Å Done Å Next
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.

This note was uploaded on 02/28/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell.

Page1 / 59

Lec4 - 4. More on Conditionals Nested if's Multiple...

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