DOC PREVIEW
USF CS 110 - Conditions and if/else

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Conditions and if/elseConditionsComparison OperatorsExamplesLogical OperatorsPrecedenceShort-Circuit EvaluationLogical Assignment and NegationDeMorgan’s TheoremExercisesif Statementif/else StatementNested if StatementsExampleChained ConditionalsSlide 16Using FunctionsSlide 18Conditions and if/elseConditionsscore > 90•Evaluates to true (1) or false (0)•Generally …variable operator variablevariable operator constantComparison Operators•<•>•<=•>=•== –NOT the same as =•!=Examples•x=5 y=8 z=5 MAX=10 initial=‘s’x<yy>MAXx<=zz>=MAXinitial==‘r’x!=zLogical Operators•and•or •not•x=5 y=8 z=5 MAX=10 initial=‘s’x<y and z>MAXx<y or z>MAXnot(x>y)Precedence•function calls•unary operators and binary power (-, **)•* / %•+ - •< <= >= > == !=•not•and•orShort-Circuit Evaluation•Stop evaluation when true/false value is determined•x=6 y=9x>2 or y > 13x<2 and y>13Logical Assignment and Negationin_range = (x>0 and x<=10) # 1 if x between 1-10, 0 otherwisein_range = 0<x<=10 #Java does not allow this!!!same_initials = (first_initial==‘S’and last_initial==‘R’)not_same_initials = not(first_initial==‘S’and last_initial==‘R’)not_same_initials = (first_initial!=‘S’ or last_initial!=‘R’)DeMorgan’s Theorem•not(a and b) => (not(a) or not(b))•not(a or b) => (not(a) and not(b))Exercises1. Determine the results of the following statements given a=6 b=9 c=12 d=-7 e=0 f=12:1. print a > d2. print c <= f3. print d > e4. print c = f5. print c == f6. print c > b and e > f7. print c > b or e > f8. print a or e9. print e and aif Statement•Statements MUST be indentedif condition:statementsif age >= 16:print “You can get a driver’s license.”if age > 21:print “You can purchase alcohol.”print “You can gamble.”if age >= 16 and age < 21:print “You can drive but you cannot gamble.”if/else Statementif condition:statementselse: statementsif grade > 60:print “You passed the class.”print “Next up, CS112.”else:print “Sorry, you did not pass.”print “Try again next semester.”Nested if Statementsif condition:if condition: statementelse:statementelse:statementif grade > 60: print "You passed the class." if grade > 90: print "You passed with an A!"else: print "Sorry, you did not pass."Exampleif num > 0 and num <= 10:print “Your number is between 1 and 10”else:if num > 10:print “Your number is too high”else:print “Your number is too low”Chained Conditionalsif num > 0 and num <= 10:print “Your number is between 1 and 10”else:if num > 10:print “Your number is too high”else:print “Your number is too low”if num > 0 and num <= 10:print “Your number is between 1 and 10”elif num > 10:print “Your number is too high”else:print “Your number is too low”Exampleif grade > 60: print "You passed the class." if grade > 90: print "You passed with an A!"else: print "Sorry, you did not pass.”#Does this work???if grade > 60: print "You passed the class."elif grade > 90: print "You passed with an A!"else: print "Sorry, you did not pass."Using Functionsdef getGrade(score):if score > 90:return “A”elif score > 80:return “B”elif score > 70:return “C”elif score > 60:return “D”else:return “F”Exercises1. Write an if statement that compares two integer variables x and y and prints the largest. For example, your program would print “X is larger than Y” or “Y is larger than X”.2. Modify your program above so that it compares three integers x, y, and z and prints the largest.3. Write a function that takes as input a year and returns true if the year is a leap year and false otherwise. A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it is divisible by 400 as well. -Problem Solving and Program Design in C Hanly and


View Full Document

USF CS 110 - Conditions and if/else

Download Conditions and if/else
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Conditions and if/else and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Conditions and if/else 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?