These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Fundamental of C programming
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Subject: Information Technology Grade: 10
Conditional Statements Introduction to Computing Science and Programming I.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
The If/Else Statement, Boolean Flags, and Menus Page 180
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Selection in C.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Introduction to Computing Using Python Imperative Programming  what is a Python program?  print() statement  input() statement  type conversion statements.
Flow of Control Part 1: Selection
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.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Conditional Execution Chapter 3 Python for Informatics: Exploring Information
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python Conditionals chapter 5
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Decision Structures, String Comparison, Nested Structures
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Introduction to Python Selection Non-Linear Programs.
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 4: Control Structures I
Selection: Non-linear programs
Chapter 4: Making Decisions.
IF statements.
Pseudo Code.
Topics The if Statement The if-else Statement Comparing Strings
If, else, elif.
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Bools & Ifs.
Selection By Ramin && Taimoor
Decision Structures, String Comparison, Nested Structures
Topics The if Statement The if-else Statement Comparing Strings
Decision Structures, String Comparison, Nested Structures
Program Flow Control Selection & repetition
Java Programming Control Structures Part 1
3. Decision Structures Rocky K. C. Chang 19 September 2018
Selection Statements.
Life is Full of Alternatives
Programming Concepts and Database
CS2011 Introduction to Programming I Selections (I)
Life is Full of Alternatives
CHAPTER 5: Control Flow Tools (if statement)
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

These Guys? Wait, What? Really?

 Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent on a condition if hungry take a bite if thirsty take a drink

 If statements must have a condition  The statement, “It’s 100 degrees outside” ◦ Is either true or false; we say, “evaluates to true or false” ◦ Helps us determine what to do next

 Comparison Operators help us evaluate conditions to true or false OperatorMeaningSample Condition Evaluates To ==Equal to5 == 5True !=Not equal to 8 != 5True >Greater than 3 > 10False <Less than5 < 8True >=Greater than or equal to 5 >= 10False <=Less than or equal to 5 <= 5True

 By indenting a line, it becomes a block  A block is one or more consecutive lines indented by the same amount  Indenting sets lines off visually and logically If (authenticated is True) : print(“Access Granted”) print(“Welcome!”)

 If  Followed By A Condition  Followed By A Colon  Followed By A Block  If the condition evaluates to ‘True’, the block is executed

 Helps make a choice based on a condition  Do one thing if the condition is true, else do something else if it is false password = input(“Please enter password”) if (password == ‘secret’) : print(“Access Granted”) else : print(“Access Denied”)

 elif is short for ‘else if’; programmers are lazy and don’t want to type ‘else if’ when they can type ‘elif’  Allows the programmer to create a sequence of conditions to be evaluated

if (testScore >= 90) : grade = “A” elif (testScore >= 80) : grade = “B” elif (testScore >= 70) : grade = “C” elif (testScore >= 60) : grade = “D” else : grade = “F”

 You can add more than one condition in the overall condition by adding ‘and’ and ‘or’  The overall condition is still evaluated to True or False if (hungry and thirsty) : takeABite() takeADrink() if (hungry or thirsty) stopAtMcDonalds()

 Write a main function  Ask the user for his/her age and store it in a variable ‘age’  Convert age to an integer using the int() function  If your age is greater than 18, print, “You can vote” If the age is less than 18 print, “You cannot vote”

 Write a main function that asks the user for his/her age and store it in a variable ‘age’  Ask the user if he or she is a citizen. (yes or no) and store the result in a variable ‘citizen’  If your age is > 18 and you are a citizen, print, “You can vote”. Otherwise, print, “You cannot vote”

 Write a main function. Ask the user to enter the temperature; convert it to an integer  If the temperature is > 80, turn the air conditioner on and make sure the heater is off  If the temperature is between 65 and 79 turn the air conditioner off and make sure the heater is off  If the temperature is below 65 turn the heater on and make sure the air conditioner is off