Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 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

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
CS0007: Introduction to Computer Programming
Selection (decision) control structure Learning objective
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
 Control structures  Algorithm & flowchart  If statements  While statements.
ITEC113 Algorithms and Programming Techniques
Conditional Statements Introduction to Computing Science and Programming I.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Functions and Modules CSIS 1595: Fundamentals of Programming and Problem Solving 1.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Simple Control Structures IF, IF-ELSE statements in C.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
© Jalal Kawash Programming Peeking into Computer Science 1.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Branches and Program Design
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Controlling Program Flow with Decision Structures.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
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)
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
Chapter 7 JavaScript: Control Statements, Part 1
Control Flow (Python) Dr. José M. Reyes Álamo.
Programming Fundamentals
Programming Fundamentals
Control Structures.
Lecture 2: Logical Problems with Choices
And now for something completely different . . .
Computers & Programming Languages
Structured Program
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
3 Control Statements:.
Selection Statements.
Types, Truth, and Expressions (Part 2)
CHAPTER 5: Control Flow Tools (if statement)
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
Types, Truth, and Expressions
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Structural Program Development: If, If-Else
Presentation transcript:

Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1

Sequential Program Flow Sequential flow: – Statements executed one after another from beginning to end of program Statement 1 Statement 2 Statement 3 … Python syntax: By default statement ends at end of line – Other languages have symbols to indicate statement end ; – Can use \ to continue statement onto multiple lines (readbility) distance = math.sqrt((x1 – x2)**2 \ (y1 – y2)**2)

Branching Executing statements only under certain conditions Condition: Expression that evaluates to either True or False condition statement in branch statement in branch … statement after branch Do these if condition true Skip if condition false

Absolute Value Requirement: Input number and print its absolute value Pseudocode: 1.Input number (converting to float) 2.If the number is less than 0, multiply itself by -1 3.Print the number Condition: If the number is less than 0 Statements executed only if true: multiply itself by -1

Python Syntax if condition: statement to be executed if true statement to be executed if true statement to be executed if true … first statement executed regardless condition is a boolean expression Indentation used to indicate what is part of branch

Absolute Value Example

Branching and Indentation How does Python know where a branch ends? Indentation used in Python – Lines after if indented only executed if condition true – Other languages use { … }, etc. Readability: All statements in same branch should be lined up (same indentation)

Branching and Indentation “ Negative! ” only printed if number < 0 (inside branch) “ Negative! ” always printed (outside branch)

If-Else Branching Executing another set of statements if condition false if condition: statements to be executed if true … else: statements to be executed if false … first statement executed regardless

Example: Coin Flip Goal: Randomly print “Heads” or “Tails” random module: simple random number generators – random.random()  random float between 0 and 1 Algorithm: – Generate random number between 0 and 1 – If number > 0.5, print “Heads” – Otherwise, print “Tails” – In either case, print “Thanks for playing” afterwards

Example: Coin Flip