Lesson 9: "if-else-if" and Conditional Logic

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Propositional Logic 7/16/ Propositional Logic A proposition is a statement that is either true or false. We give propositions names such as p, q,
Intro to Discrete Structures
BUILDING COMPUTER CIRCUITS prepared by Burak Galip ASLAN September, 2006 BOOLEAN LOGIC AND GATES CONTROL CIRCUITS.
Invitation to Computer Science, Java Version, Second Edition.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
Branches and Program Design
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
How do I show that two compound propositions are logically equivalent?
5.04 Apply Decision Making Structures
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Discrete Structures for Computer Science Presented By: Andrew F. Conn Slides adapted from: Adam J. Lee Lecture #1: Introduction, Propositional Logic August.
JavaScript: Conditionals contd.
JavaScript Controlling the flow of your programs with ‘if’ statements
Introduction to Decision Structures and Boolean Variables
CNG 140 C Programming (Lecture set 3)
Unit 3: Lesson 1 - The Need for Programming Languages
Section 7.1 Logical Operators
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
The Need for Programming Languages
The Need for Algorithms 2 days
Chapter 6: Conditional Statements and Loops
Compound Conditional Logic
Algorithm and Ambiguity
Lesson 10: Building an App: Color Sleuth
CS 240 – Lecture 11 Pseudocode.
Microsoft Visual Basic 2005 BASICS
Boolean logic Taken from notes by Dr. Neil Moore
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Conditional Statements
More Selections BIS1523 – Lecture 9.
Lesson 8: Boolean Expressions and "if" Statements
Creativity in Algorithms
Conditions and Ifs BIS1523 – Lecture 8.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Lesson 1: Fundamentals of Programming
Lesson 15: Processing Arrays
UNIT 3 CHAPTER 1 LESSON 4 Using Simple Commands.
HAPPY NEW YEAR! Lesson 7: If-statements unplugged
Looping and Random Numbers
Boolean Logic In today’s lesson we will look at:
Programming in JavaScript
Boolean Logic Boolean Logic is considered to be the basic of digital electronics. We know that a computer’s most basic operation is based on digital electronics.
Visual Basic – Decision Statements
Algorithm and Ambiguity
Problem Solving Designing Algorithms.
Understanding Problems and how to Solve them by using Computers
Programming in JavaScript
PYTHON: BUILDING BLOCKS Sequencing & Selection
Unit 3 lesson 2-5 The Need For Algorithms- Creativity in Algorithms – Simple Commands - Functions Day 18.
Boolean Expressions to Make Comparisons
Boolean logic Taken from notes by Dr. Neil Moore
CHAPTER 4: Conditional Structures
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Basic Concepts of Algorithm
Copyright © Cengage Learning. All rights reserved.
WRITING AN ALGORITHM, PSEUDOCODE, AND FLOWCHART LESSON 2.
Lecture 5 – Unit 1 – Chatbots Python – More on Conditional statements
Structural Program Development: If, If-Else
Presentation transcript:

Lesson 9: "if-else-if" and Conditional Logic UNIT 5 – LESSON 9 – HAPPY COLD DAY!!

There is a worksheet for this lesson There is a worksheet for this lesson. If you did not take one in class on Thursday, you can print one out of Code Studio. Compound Conditionals 

VOCABULARY ALERT: Boolean - A single value of either TRUE or FALSE Boolean Expression - in programming, an expression that evaluates to True or False. Conditionals - Statements that only run under certain conditions. If-Statement - The common programming structure that implements "conditional statements". Selection - A generic term for a type of programming statement (usually an if-statement) that uses a Boolean condition to determine, or select, whether or not to run a certain block of statements. Pseudocode - an informal high-level description of the operating principle of a computer program or other algorithm. It uses the structural conventions of a normal programming language, but is intended for human reading than machine reading.

Purpose: Write and test conditional expressions using Boolean operators AND (&&) OR (||) and NOT (!) Given an English description write compound conditional expressions to create desired program logic Use a "chain" of if-else-if statements to implement desired program logic When given starting code add if-else-if statements or compound boolean expression to express desired program logic

First, do Just page 1 of (Optional) Compound Conditionals - Worksheet and work on the questions on the first sheet.

Much like the previous lesson you will complete a series of short exercises to write code into "toy" programs to get practice using if-else if constructs and the logical operators AND, OR, and NOT.

Read the Introduction in Code Studio Watch the video – Points to pay attention to in the video: An if-else-if statement lets you check more than one condition at a time. The order of conditions matters. Since conditions are checked sequentially from top to bottom, the code that gets executed will be the first statement to evaluate to true.

Do Levels 4, 5, 6, 7 Watch second video – Points to pay attention to: The "Boolean operators" are AND and OR. In JavaScript you write them as && for AND and || for OR You use them to combine boolean expressions into a single statement when you need to check multiple conditions before executing a particular segment of code.

Do the next section. Then do Levels 10 and 11 Do the next section Do the next section. Then do Levels 10 and 11 Do the next section. Then do Levels 13, 14, 15

What’s the trickiest logical statement you encountered in this lesson What’s the trickiest logical statement you encountered in this lesson? What made it tricky? We often use “and” and “or” in English in imprecise ways, or at least in ways that could have multiple meanings. In programming logic, AND and OR have very precise meanings and they don’t always map directly to English.

True or False: the Boolean operators AND, OR and NOT, enable us to express boolean conditions that we couldn't before? False. Anything that you can express with AND, OR and NOT, can be expressed with a chain or nesting of if-else statements. Certainly, it allows us to expression complex Boolean conditions more succinctly, and makes our code MUCH easier to read. But in terms of program logic, we can do everything with just if-else statements

In English, we sometimes use OR in the same way it’s used in programming - to mean either or both. “Do you want cream or sugar in your coffee?” But we often use OR to mean exactly one thing or the other, not both. “Is the elevator going up or down?” The programming-logic answer to that question is: yes. Because it is the case that the elevator is either going up or it’s going down.

AND can get really tricky because in English we sometimes use the word “or” to convey a logical AND. For example: In English you might say: “If it’s not Saturday or Sunday, then it’s a weekday.” In programming you might express this as: In other words: "It is not the case that the day is Saturday or Sunday"

But you might also express the same condition in code as: In other words: "It is the case that BOTH the day is not Saturday AND the day is also not Sunday."

Logic can get tricky Because logic can get convoluted and tricky, even professionals mess it up. However, as a programmer, you can take steps to make sure you’ve got it right by testing your code thoroughly to make sure you get expected results. Because the Boolean operators essentially take binary values (T/F) as input, you can easily figure out how many possible inputs there are for any complex Boolean expression and test them all.

For example if you have a statement like: There are 3 expressions there, and each can be either true or false, so there are 8 possible ways to assign true or false to: (TTT, TTF, TFT, TFF, FTT, FTF, FFT, FFF) You can test all 8 to make sure you get the right outputs.

Finish off the Unit 5 – Lesson 9 in Code Studio Finish off the Unit 5 – Lesson 9 in Code Studio. Turn in the worksheet for credit. You will have a vocabulary test on Tuesday, + Clean and Green, + Unit 5 Assessments