© Jalal Kawash 2010 3 Programming Peeking into Computer Science 1.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
© Jalal Kawash Programming Peeking into Computer Science 1.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Making Decisions In Python
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
This will let you see the game you are going to play on the inside. It will help you see what all goes into creating instance to that goes into setting.
© Jalal Kawash Programming Peeking into Computer Science 1.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Decision Structures Chapter 4 Part 2. Chapter 4 Objectives To understand o What relational operators are and how they are used o Boolean logic o Testing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Programming Logic and Design, Second Edition, Comprehensive
Programming Logic and Design Fifth Edition, Comprehensive
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Four Fundamental Pieces Instruction Control Structure Function Expression.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
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.
CONTROLLING PROGRAM FLOW
Programming Logic and Design Sixth Edition Chapter 5 Looping.
© Jalal Kawash 2010Peeking into Computer Science George Boole English Mathematician His The Mathematical Analysis of Logic, 1848 is the first.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
© Jalal Kawash Database Queries Peeking into Computer Science.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
5 Event Handling Interactive Programming Suggested Reading Interaction: Events and Event Handling, Supplemental Text for CPSC 203 Distributed this term.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Structures: Conditionals, If/Else and Loops David Millard
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Chapter 3: Decisions and Loops
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Simple Control Structures
Introduction to pseudocode
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
MSIS 655 Advanced Business Applications Programming
Structured Program
Objectives After studying this chapter, you should be able to:
Understanding the Three Basic Structures
Design and Implementation
Logical Operations In Matlab.
Computer Science Core Concepts
ICT Programming Lesson 3:
Programming with Alice
Using Decision Structures
Using C++ Arithmetic Operators and Control Structures
Structural Program Development: If, If-Else
Presentation transcript:

© Jalal Kawash Programming Peeking into Computer Science 1

© Jalal Kawash 2010Peeking into Computer Science Reading Assignment Mandatory: Chapter 5 – Section 5.5 Jalal’s resources: ◦“How to” movies and example programs available at: JT’s resources: ◦ 2

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Random Numbers To simulate the possibility of different events occurring as a program is a run a random number function can be employed. Typically the function generates a number within a certain range: ◦1 – 2 (coin flip) ◦1 – 6 (normal die roll) ◦1 – 100 (percentage) ◦Etc.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Dice Example It can be invoked by the random function of class ‘World’ InclusiveExclusive The only values generate: 1, 2, 3, 4, 5, 6 (no real numbers) Example 9: random numbers

If-Else Statements Alice makes decisions 5

© Jalal Kawash 2010Peeking into Computer Science Objectives At the end of this section, you will be able to: 1. Understand conditional execution 2. Understand and use the Alice if/else statement 3. Use functions as arguments to other methods 4. Use conditional structures in flowcharts

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Choosing Among Alternative Course Of Action Frequently this is needed when the program must react to user input or the actions of the user. New: series of instructions Open: series of instructions Save: series of instructions Etc.

© Jalal Kawash 2010Peeking into Computer Science Branching/Decision Making It’s the mechanism used in many programming languages used to execute different instructions when alternatives are faced. At the branch (where the decision must be made) a logical statement determines which alternative occurs. ◦Another way of looking at it: a question is asked which has a true or false answer. ◦The answer to the question determines which branch executes. The flowchart symbol used to represent a branch/decision is a diamond. Question

© Jalal Kawash 2010Peeking into Computer Science Branching/Decision Making (2)

© Jalal Kawash 2010Peeking into Computer Science Conditional Execution Programs need to make decisions An ATM program must verify your password to know how to proceed This is called conditional Execution The execution of a group of statements is conditional on a Boolean condition If the password is correct then go to the next step Else give an error message JT (new terminology): body is the part that executes only under a certain condition

© Jalal Kawash 2010Peeking into Computer Science If-Else Flowchart 11 Statement before if Statement after if Test if condition else bodyif body true false

© Jalal Kawash 2010Peeking into Computer Science If-Else Statement in Alice 12 JT: ‘else’ body JT: ‘if’ body

© Jalal Kawash 2010Peeking into Computer Science Example Story Line 13

© Jalal Kawash 2010Peeking into Computer Science Use of Functions 14 Returns a random number between 0.1 and 4 value substituted for distance parameter of move Returns a Boolean value

© Jalal Kawash 2010Peeking into Computer Science When the if condition is true 15

© Jalal Kawash 2010Peeking into Computer Science When the if condition is false 16

© Jalal Kawash 2010Peeking into Computer Science JT, One Action: Do Something When True If you want the program to react when the logical statement evaluates to true (the answer to the question is true). Just use the ‘ if ’ and leave the ‘ else ’ portion empty. Hole wider Than Penguin? Penguin falls into the hole TRUE (Rest of the program) FALSE Indented part after the ‘if’: what executes if the statement is true

© Jalal Kawash 2010Peeking into Computer Science JT: Do Something For Both True/False Cases You want the program to do something when the logical statement evaluates to true and something different when the statement evaluates to false. Add instructions for both the ‘ if ’ and the ‘ else ’ portion of the instruction. Hole wider Than Penguin? Penguin falls into the hole TRU E (Rest of the program) FALSE Penguin: “Wow!”

© Jalal Kawash 2010Peeking into Computer Science JT: Logical Operators Are used in conjunction with logical statements and relate or compare things. The comparison will result in a true or false result. ◦E.g., temperature is above freezing (greater than zero). Accessing the logical operators in Alice. ◦Look under the functions of the ‘ world ’ object.

© Jalal Kawash 2010Peeking into Computer Science JT: Types Of Logical Operators Desired comparison Operator (Alice)Example Equality== Not equal to (inequality) != Greater than> Greater than, equal to >= Less than< Less than, equal to <=

© Jalal Kawash 2010Peeking into Computer Science JT: Multiple Branches: Independent If a number of questions must by asked by a program and the answer to one question has no impact on the answer to the other questions. ◦That means that all questions will be asked regardless of whether any questions answered true or false. Q1 Q2? Do something TRUE FALSE Do something TRUE FALSE Rest of program

© Jalal Kawash 2010Peeking into Computer Science JT: Multiple Branches: Independent If a number of questions must by asked by a program and the answer to one question has no impact on the answer to the other questions. ◦That means that all questions will be asked regardless of whether any questions answered true or false. Q1 Q2? Do something TRUE FALSE Do something TRUE FALSE Rest of program Always ask second question

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Multiple Branches 23 Example 10: multiple successive branches First Boolean question: Always asked Second Boolean question: Not indented, always asked

© Jalal Kawash 2010Peeking into Computer Science Nested Branches Sometimes the answer to one question will determine whether or not other questions will be asked. ◦The answer to one question (true or false) has an effect on whether another question is asked. ◦The asking of the second question is dependent on the answer to the first question. ◦(Alternatively: the second question is nested inside the first question).

© Jalal Kawash 2010Peeking into Computer Science Nested Branches: Flowchart Q1 Q2? TRUE FALSE Do something TRUE FALSE Rest of program Q1 Q2? FALSE TRUE Do something TRUE FALSE Rest of program

© Jalal Kawash 2010Peeking into Computer Science Nested Branches: Flowchart Q1 Q2? TRUE FALSE Do something TRUE FALSE Rest of program Q1 Q2? FALSE TRUE Do something TRUE FALSE Rest of program Answer to first ‘if’ determines whether second ‘if’ executes

© Jalal Kawash 2010Peeking into Computer Science JT: Multiple Branches: Dependent (RHS Case) Multiple if/else statements can be used in succession. Example program 11: nested_branches Also indented: execution is dependent on the first ‘if’ (Only ask about blue if user does not indicate that red is the favorite) Indented: executes if first ‘if’ is true Indented twice: only executes if other cases answer false

© Jalal Kawash 2010Peeking into Computer Science JT: Multiple Branches: Dependent (LHS Case: Nonsensical) 28

Loops Do it again 29

© Jalal Kawash 2010Peeking into Computer Science Objectives At the end of this section, you will be able to: 1. Understand repetition structures 2. Use the loop statement 3. Use the while statement 4. Understand when to use a loop or a while 5. Use loop structures in flowcharts

© Jalal Kawash 2010Peeking into Computer Science Loop Statement A loop statement allows a group of statements to be repeated for a number of times loop for 5 times monkey.monkeyJump monkey.move(up,1 meter) monkey.move(down,1 meter) 31

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Adding A Loop In Alice

© Jalal Kawash 2010Peeking into Computer Science Loop Statement in Alice 33

© Jalal Kawash 2010Peeking into Computer Science Loop in Flowcharts 34 Monkey Jumps Monkey moves up 1m Monkey moves down 1m Loop 5 times Start Stop

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Boolean-Controlled Loops Loops are used to repeat a part of the program (or potentially the entire program itself). They are similar to branches where a logical statement is employed. In the case of a loop if the logical statement evaluates to true then a part of the program will repeat (loop). The repetition will continue until the logical statement evaluates to false.

© Jalal Kawash 2010Peeking into Computer Science While Statement 36 Statement before while Statement after while Test while condition while body true false

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Counting With While 37 Example 11 Starting value Condition for repeating Loop ‘body’: Instructions that are repeated

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Example Flowchart 38 count = 1 count <= 5? START END Troll counts, arm moves up Arm moves downGround cries out Arm moves up, count = count + 1 True False

© Jalal Kawash 2010Peeking into Computer Science While Example 39

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Combining Loops And Branches 40 Example 12 If-Else branch is part of the body of the loop

© Jalal Kawash 2010Peeking into Computer Science While Example 41