Topic: Conditional Statements – Part 1

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Project on VB Control Structures
Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables.
Conditional Statements Introduction to Computing Science and Programming I.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Control Flow (Python) Dr. José M. Reyes Álamo.
Control Structures I Chapter 3
Topic: Python Lists – Part 1
CMPT 120 Topic: Python’s building blocks -> More Statements
Topic: Introduction to Computing Science and Programming + Algorithm
CNG 140 C Programming (Lecture set 3)
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Recursion – Part 2
Topic: Functions – Part 1
Topic: Programming Languages and their Evolution + Intro to Scratch
Topic: Iterative Statements – Part 1 -> for loop
CMPT 120 Topic: Python Modules.
Topic: Introduction to Computing Science and Programming + Algorithm
Topic: Python’s building blocks -> Statements
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Conditional Statements – Part 2
Python: Control Structures
CMPT 120 Topic: Searching – Part 1
CMPT 120 Topic:  Case Study.
CMPT 120 Topic: Functions – Part 4
CMPT 120 Topic: Python strings.
Topic: Functions – Part 2
If, else, elif.
Chapter 6: Conditional Statements and Loops
Learning to Program in Python
TOPIC 4: REPETITION CONTROL STRUCTURE
Conditional Statements
Introduction to Programming
And now for something completely different . . .
Learning to Program in Python
Conditions and Ifs BIS1523 – Lecture 8.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Introduction to Programming
Faculty of Computer Science & Information System
3. Decision Structures Rocky K. C. Chang 19 September 2018
Selection Statements.
PYTHON: BUILDING BLOCKS Sequencing & Selection
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Javascript Chapter 19 and 20 5/3/2019.
Introduction to Programming
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Topic: Iterative Statements – Part 2 -> for loop
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
CMPT 120 Lecture 6 – Unit 1 – Chatbots
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Lecture 5 – Unit 1 – Chatbots Python – More on Conditional statements
Lecture 17 – Practice Exercises 3
Lecture 17 – Practice Exercise 3 SOLUTIONS
Presentation transcript:

Topic: Conditional Statements – Part 1 CMPT 120 Topic: Conditional Statements – Part 1

Last Few Lectures Strings & Lists Indexing Slicing Strings -> immutable and Lists -> mutable Functions Methods

Learning outcomes At the end of this course, a student is expected to: Describe and apply fundamental concepts and terminology of Python: Conditions and execution flow Create (design) small to medium size programs using Python: Use the core features of Python to design programs to solve problems: conditionals

Today’s Menu Introduce Python Statements #5 -> Conditional Statements Syntax #1 Syntax #2 Syntax #3

Python statements Categories: Assignment statement Input statement Conversion function Output statement Operational statements Mathematical/arithmetic operators String manipulation operators Conditional statement Iterative statement Some of them are built-in function or method Today We’ll see this statement soon!

Introducing Conditional Statements So far, the execution of our Python programs has flowed from top to bottom Starting at the first statement and executing each statement all the way down sequentially Conditional statements allow us to alter the flow of the execution of our Python programs

Conditional statements – In real world! Making decisions Every day, we make decisions based on conditions Example: If I am hungry, I eat. If it rains this afternoon, I will take the bus, otherwise, I will walk. On Tuesdays and Thursdays, I can sleep in, but on Mondays, Wednesdays and Fridays, I can’t, I need to wake up early and go to my lecture.

Conditional statements – In computer world! Definition: A statement that allows our Python program to decide whether to do something or to do something else (but not both), depending on a condition hence affecting the sequential flow of execution

Conditional statements – Syntax #1 if statement Syntax #1: < > < > Boolean expression This small empty space made of a few white space (or blank) characters is called an indentation and its presence is very important in a conditional statement in Python.

How syntax #1 works? The condition is evaluated -> True or False If the condition is True, the indented statement(s) below the if <condition> : is/are executed Then Python interpreter moves to the next statement, i.e., the statement after the if statement (statement that is indented at the same level as the if statement) and resumes its execution

Let’s practice: Step 1 - Problem Statement Solution: Login_Syntax_1.py Write a Python program that allows a user to login User enters a password The program displays “Log in successfully” or terminates if not Solution: Login_Syntax_1.py How would we test this program?

Let’s practice: Step 2 – Design Algorithm and Data Step 3 – Solution Selection (we’ll do this step later on this semester) Step 4 – Implementation Solution: Login_Syntax_1.py Step 5 – Testing How would we test this program?

Test Cases Test Case # Test Data Expected Results

Improving our solution Right now, when the user enters the wrong password, our program simply terminates What can we do to improve the usefulness (i.e., the user interaction) of our program? How can we make our solution clearer to our user (i.e., more user-friendly)?

Conditional statements – Syntax #2 if else statement Syntax #2: < > < > Indentation < > Boolean expression

Syntax #2 Can you guess how this second conditional statement works?

How syntax #2 works? The condition is evaluated -> True or False If the condition is True, the indented statement(s) directly below the if <condition> : is/are executed If condition is False, the indented statement(s) directly below the else : is/are executed Once step 2. or 3. above has executed, Python interpreter moves to the next statement, i.e., the statement after the if else conditional statement i.e., the next statement that is indented at the same level as the if else conditional statement, and resumes its execution

OK! Back to the password program! Let’s improve our program: Login_Syntax_2.py

Back to Scratch Does Scratch have conditional statements? If so, what do they look like?

Another example -> Guessing game: Step 1 - Problem Statement Write a guessing game in Python in which the computer picks a number (between 1 and 10, inclusively) and the player (i.e., user) tries to guess this number Once the player has entered a guess, our program displays an appropriate message: "Wow! You got it!" OR "Sorry! You missed! The number was <#>."

Guessing game: Step 2 – Design Algorithm and Data Step 3 – Solution Selection (we’ll do this step later on this semester) Step 4 – Implementation Solution: GuessingGame_1.py Step 5 – Testing How would we test this program?

Test Cases Test Case # Test Data Expected Results

Improving our solution Let’s improve our guessing game by letting the user know when s/he has entered a guess that is outside the range of numbers s/he could pick from Solution: GuessingGame_2.py Step 5 – Testing How would we test this program?

Conditional Statements – Syntax #3 nested if statement < > < > Indentation

Can become: nested if statement < > < > > < This is just like our if else statement (see Syntax #2)

OR this: nested if statement nested if statement < > < >

OR that: nested if statement nested if statement < > < > 2 (b) < 3 > < > (c) …

OR even that: if <condition 1> : if <condition 2> : <some statements A> if <condition 3> : <some statements B> else : <some statements C> if <condition 4> : <some statements D> <some statements E> <some statements F> <some statements G>

Bottom Line The way we nest our conditional statements in our program is dictated by the design of our solution to the problem we are solving, i.e., our algorithm

Summary Introduce Python Statements #5 -> Conditional Statements Syntax #1 Syntax #2 Syntax #3

Next Lecture Conditional Statements -> Syntax #4 Introduction to yet another data type -> bool How to construct conditions