Get 2 days of unlimited access
Class Notes (1,000,000)
US (420,000)
BU (8,000)
CAS CS (100)
Lecture 5

CAS CS 111 Lecture Notes - Lecture 5: Boolean ExpressionPremium


Department
Computer Science
Course Code
CAS CS 111
Professor
David Sullivan
Lecture
5

This preview shows pages 1-3. to view the full 14 pages of the document.
Flow&of&Control
Flow&of&control&=&order&in&which&statements&are&executed
By&default,&a&program's&statements&are&executed&sequentially,&from&top&
to&bottom
Conditional&Execution
To&solve&many&types&of&problems,&we&need&to&change&the&standard&flow&
of&control
Conditional&execution&allows&your&code&to&decide&whether&to&do&
something,&based&on&some&condition
Example:
§
§
Recall:&Conditional&Execution&in&Scratch
Simple&Decisions:&if&Statements
Syntax:
§
§Where:
Condition&is&an&expression&that&is&true&or&false
True&block&is&one&of&more&intended&statements
§
§Example:
Two-Way&Decisions:&if-else&Statements
Syntax:
§
Example:
§
Expressing&Simple&Conditions
Python&provides&a&set&of&relational&operators&for&making&comparisons:
Boolean&Expressions
A&condition&has&one&of&two&values:&True&or&False
§
True&and&False&are&not&strings
§They&are&literals&from&the&bool&data&type
§
An&expression&that&evaluates&to&True&or&False&is&known&as&a&boolean&
expression
Forming&More&Complex&Conditions
Python&provides&logical&operators&for&combining/modifying&boolean&
expressions:
A&Word&About&Blocks
A&block&can&contain&multiple&statements
§
A&new&block&begins&whenever&we&increase&the&amount&of&indenting.
A&block&ends&when&we&either:
§Reach&a&line&with&less&indenting&that&the&start&of&the&block
§Reach&the&end&of&the&programs
Nesting
We&can&"nest"&one&conditional&statement&in&the&true&block&or&false&
block&of&another&conditional&statement.
What&is&the&output&of&this&program?
What&does&this&print?&(not&the&changes!)
What&does&this&print?&(note&the&new&changes!)
Multi-Way&Decisions
The&following&function&doesn't&work.
§
Example:
§
Here's&a&fixed&version:
§
Example:
§
Multi-Way&Decisions:&if-elif-else&Statements
Syntax:
§
The&conditions&are&evaluate&in&order.&The&true&block&of&the&first&
condition&is&executed.
If&none&of&the&conditions&are&true,&the&false&block&is&executed
Flowchart&for&an&if-elif-else&Statement
How&many&lines&does&this&print?
How&many&lines&does&this&print?
What&is&the&output&of&this&code?
Common&Mistake&When&Using&and/or
When&using&and/or,&both&sides&of&the&operator&should&be&a&boolean&
expression&that&could&stand&on&its&own.
§
Unfortunately,&Python&doesn't&complain&about&code&like&the&
problematic&code&above.
§But&it&wont&typically&work&the&way&you&want&it&to!
Avoid&Overly&Complicated&Code
The&following&also&involves&decision&based&on&a&person's&age:
§
How&could&is&be&simplified?
Friday's)Lecture
Monday,&17&September& 2018
11:57&PM

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Subscribers Only
Flow&of&Control
Flow&of&control&=&order&in&which&statements&are&executed
By&default,&a&program's&statements&are&executed&sequentially,&from&top&
to&bottom
Conditional&Execution
To&solve&many&types&of&problems,&we&need&to&change&the&standard&flow&
of&control
Conditional&execution&allows&your&code&to&decide&whether&to&do&
something,&based&on&some&condition
Example:
§
Examples&of&calling&this&function&from&the&Shell:
§
Recall:&Conditional&Execution&in&Scratch
Simple&Decisions:&if&Statements
Syntax:
§
§
Where:
Condition&is&an&expression&that&is&true&or&false
True&block&is&one&of&more&intended&statements
§
§Example:
Two-Way&Decisions:&if-else&Statements
Syntax:
§
Example:
§
Expressing&Simple&Conditions
Python&provides&a&set&of&relational&operators&for&making&comparisons:
Boolean&Expressions
A&condition&has&one&of&two&values:&True&or&False
§
True&and&False&are&not&strings
§They&are&literals&from&the&bool&data&type
§
An&expression&that&evaluates&to&True&or&False&is&known&as&a&boolean&
expression
Forming&More&Complex&Conditions
Python&provides&logical&operators&for&combining/modifying&boolean&
expressions:
A&Word&About&Blocks
A&block&can&contain&multiple&statements
§
A&new&block&begins&whenever&we&increase&the&amount&of&indenting.
A&block&ends&when&we&either:
§Reach&a&line&with&less&indenting&that&the&start&of&the&block
§Reach&the&end&of&the&programs
Nesting
We&can&"nest"&one&conditional&statement&in&the&true&block&or&false&
block&of&another&conditional&statement.
What&is&the&output&of&this&program?
What&does&this&print?&(not&the&changes!)
What&does&this&print?&(note&the&new&changes!)
Multi-Way&Decisions
The&following&function&doesn't&work.
§
Example:
§
Here's&a&fixed&version:
§
Example:
§
Multi-Way&Decisions:&if-elif-else&Statements
Syntax:
§
The&conditions&are&evaluate&in&order.&The&true&block&of&the&first&
condition&is&executed.
If&none&of&the&conditions&are&true,&the&false&block&is&executed
Flowchart&for&an&if-elif-else&Statement
How&many&lines&does&this&print?
How&many&lines&does&this&print?
What&is&the&output&of&this&code?
Common&Mistake&When&Using&and/or
When&using&and/or,&both&sides&of&the&operator&should&be&a&boolean&
expression&that&could&stand&on&its&own.
§
Unfortunately,&Python&doesn't&complain&about&code&like&the&
problematic&code&above.
§But&it&wont&typically&work&the&way&you&want&it&to!
Avoid&Overly&Complicated&Code
The&following&also&involves&decision&based&on&a&person's&age:
§
How&could&is&be&simplified?
Friday's)Lecture
Monday,&17&September& 2018 11:57&PM

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Subscribers Only
Flow&of&Control
Flow&of&control&=&order&in&which&statements&are&executed
By&default,&a&program's&statements&are&executed&sequentially,&from&top&
to&bottom
Conditional&Execution
To&solve&many&types&of&problems,&we&need&to&change&the&standard&flow&
of&control
Conditional&execution&allows&your&code&to&decide&whether&to&do&
something,&based&on&some&condition
Example:
§
Examples&of&calling&this&function&from&the&Shell:
§
Recall:&Conditional&Execution&in&Scratch
Simple&Decisions:&if&Statements
Syntax:
§
§Where:
Condition&is&an&expression&that&is&true&or&false
True&block&is&one&of&more&intended&statements
§
§
Example:
Two-Way&Decisions:&if-else&Statements
Syntax:
§
Example:
§
Expressing&Simple&Conditions
Python&provides&a&set&of&relational&operators&for&making&comparisons:
Boolean&Expressions
A&condition&has&one&of&two&values:&True&or&False
§
True&and&False&are&not&strings
§They&are&literals&from&the&bool&data&type
§
An&expression&that&evaluates&to&True&or&False&is&known&as&a&boolean&
expression
Forming&More&Complex&Conditions
Python&provides&logical&operators&for&combining/modifying&boolean&
expressions:
A&Word&About&Blocks
A&block&can&contain&multiple&statements
§
A&new&block&begins&whenever&we&increase&the&amount&of&indenting.
A&block&ends&when&we&either:
§Reach&a&line&with&less&indenting&that&the&start&of&the&block
§Reach&the&end&of&the&programs
Nesting
We&can&"nest"&one&conditional&statement&in&the&true&block&or&false&
block&of&another&conditional&statement.
What&is&the&output&of&this&program?
What&does&this&print?&(not&the&changes!)
What&does&this&print?&(note&the&new&changes!)
Multi-Way&Decisions
The&following&function&doesn't&work.
§
Example:
§
Here's&a&fixed&version:
§
Example:
§
Multi-Way&Decisions:&if-elif-else&Statements
Syntax:
§
The&conditions&are&evaluate&in&order.&The&true&block&of&the&first&
condition&is&executed.
If&none&of&the&conditions&are&true,&the&false&block&is&executed
Flowchart&for&an&if-elif-else&Statement
How&many&lines&does&this&print?
How&many&lines&does&this&print?
What&is&the&output&of&this&code?
Common&Mistake&When&Using&and/or
When&using&and/or,&both&sides&of&the&operator&should&be&a&boolean&
expression&that&could&stand&on&its&own.
§
Unfortunately,&Python&doesn't&complain&about&code&like&the&
problematic&code&above.
§But&it&wont&typically&work&the&way&you&want&it&to!
Avoid&Overly&Complicated&Code
The&following&also&involves&decision&based&on&a&person's&age:
§
How&could&is&be&simplified?
Friday's)Lecture
Monday,&17&September& 2018 11:57&PM
You're Reading a Preview

Unlock to view full version

Subscribers Only