@extends('template') @section('title') Lab 4: Predicates and Conditionals @stop @section('content') # Lab 4 Predicates and Conditionals {{-- ## [Solutions](/content/labs/lab04/solutions/) --}} {{-- ## [Worksheet solns](https://docs.google.com/document/d/1XH3N7TOIITWi2Xb-b4_fNrv4WDseXZCZh8b4D-YPJIk/edit?usp=sharing) --}} ## Goals + Practice writing predicates + Practice with conditionals + “Divide, Solve, and Combine” (DSC): functions with conditionals @include('/labs/_setup', ['folder' => 'lab04']) {{-- - [Here is the worksheet as a google doc](https://docs.google.com/document/d/15XbSXyb5H-95k4-j9zcRqUzt5o7BghfYvN01tfiudbI/edit?usp=sharing) - Open the document, then File -> Make a Copy. - Then you can edit your copy with your answers. --}} @include('/labs/lab04/_toc') ## Big Questions {.bigqs} - *What is a predicate, and how can you build a complex predicate out of multiple simpler predicates?*
Show Answer A predicate is a function that always returns a boolean value. To build a complex predicate from simple ones, the complex predicate function should call the simple functions and combine their result values using boolean operators into a single boolean that it can return.
- *What is the difference between writing a series of `if` statements and a single `if` statement with multiple `elif` parts?*
Show Answer A series of if statements operate independently, and it's possible for more than one to trigger. A chain of if/elif statements guarantees that only the first statement whose condition is true will trigger, and others will be skipped even if their conditions are also true.
@stop