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.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Pseudocode and Algorithms
Computer Science 1620 Programming & Problem Solving.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Rob Miles. Creating a Working MoodLight We know that colours in XNA are stored as values which represent the amount of red, blue, green and transparency.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
11 Getting Player Input Using a Gamepad Session 3.1.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
11 Debugging Programs Session Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
11 Adding Vibration Effects Session Session Overview  Describe how the vibration feature of the gamepad works  Show how an XNA program can control.
Rob Miles. Creating a Broken MoodLight An XNA game contains game data which is used by the Draw and Update methods – Update updates the game data – Draw.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Control Structures I Chapter 3
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Selection (also known as Branching) Jumail Bin Taliba by
Control Structures II Chapter 3
Creating a Multi-Player Game
What to do when a test fails
EGR 2261 Unit 4 Control Structures I: Selection
Debugging and Random Numbers
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
IF statements.
Chapter 5 Conclusion CIS 61.
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Learning to program with Logo
Chapter 1 Program Development
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Manipulating Pictures, Arrays, and Loops part 2
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Conditionals & Boolean Expressions
Creativity in Algorithms
Selection CIS 40 – Introduction to Programming in Python
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Programming Fundamentals (750113) Ch1. Problem Solving
An Introduction to VEX IQ Programming with Modkit
1) C program development 2) Selection structure
Do While (condition is true) … Loop
Coding Concepts (Basics)
Java Programming Control Structures Part 1
Introduction to Problem Solving and Control Statements
Conditional Logic Presentation Name Course Name
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Manipulating Pictures, Arrays, and Loops
Solving Linear Equations
Basic Concepts of Algorithm
IAT 800 Foundations of Computational Art and Design
Review of Previous Lesson
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
Solving Linear Equations
Presentation transcript:

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 on the value of data in variables  Investigate how to create sensible logical constructions and how the compiler can help with this  Create a proper MoodLight which fades smoothly between black and white  Change the MoodLight to display a range of colors  Introduce the idea of an algorithm  Show how a program can make logical decisions based on the value of data in variables  Investigate how to create sensible logical constructions and how the compiler can help with this  Create a proper MoodLight which fades smoothly between black and white  Change the MoodLight to display a range of colors Chapter 2.3: Making Decisions in a Program2

1. Overflow MoodLight Chapter 2.3: Making Decisions in a Program3  This is the broken version of the fading MoodLight  The light suddenly changes from white to black when the byte variables overflow  This is the broken version of the fading MoodLight  The light suddenly changes from white to black when the byte variables overflow

Making a Fading MoodLight  The problem with the fading MoodLight is that we have not told the program that we want to count down as well as up  Every single action that we want the program to perform must be specifically described, otherwise it will not perform the action  The computer program doesn’t actually “care” what it does, it simply follows the steps that it is given  The problem with the fading MoodLight is that we have not told the program that we want to count down as well as up  Every single action that we want the program to perform must be specifically described, otherwise it will not perform the action  The computer program doesn’t actually “care” what it does, it simply follows the steps that it is given Chapter 2.3: Making Decisions in a Program4

How To Make the Lights Fade  We can express what we want to do in English: “Make the value of redIntensity bigger each time that you are called. When the value reaches 255, start making it smaller each time you are called until it reaches 0, at which point you should start making it bigger again. Do the same with blue and green.”  This would allow an intelligent human to control the lights, now we need to create some C# that will allow a computer to do this  We can express what we want to do in English: “Make the value of redIntensity bigger each time that you are called. When the value reaches 255, start making it smaller each time you are called until it reaches 0, at which point you should start making it bigger again. Do the same with blue and green.”  This would allow an intelligent human to control the lights, now we need to create some C# that will allow a computer to do this Chapter 2.3: Making Decisions in a Program5

Problems and Algorithms  A sequence of steps that solves a problem is called an algorithm  You can think of a recipe as something that solves the problem “How do I bake this cake?”  The algorithm is the starting point of the program  Once you have the algorithm you then convert it into a program that behaves like the algorithm says it should  The algorithm gives a sequence of steps to solve the problem  A sequence of steps that solves a problem is called an algorithm  You can think of a recipe as something that solves the problem “How do I bake this cake?”  The algorithm is the starting point of the program  Once you have the algorithm you then convert it into a program that behaves like the algorithm says it should  The algorithm gives a sequence of steps to solve the problem Chapter 2.3: Making Decisions in a Program6

Breaking the Solution into Steps  The computer program will run as a series of steps, so we need to break our instructions down into steps as well: 1. If we are counting up, increase the value of redIntensity. 2. If we are counting down, decrease the value of redIntensity. 3. If redIntensity is 255, change to counting down. 4. If redIntensit y is 0, change to counting up.  The computer program will run as a series of steps, so we need to break our instructions down into steps as well: 1. If we are counting up, increase the value of redIntensity. 2. If we are counting down, decrease the value of redIntensity. 3. If redIntensity is 255, change to counting down. 4. If redIntensit y is 0, change to counting up. Chapter 2.3: Making Decisions in a Program7

Storing Data for the Algorithm  To implement the algorithm, the program needs to hold whether or not it is counting up  The C# language provides a type called bool (short for Boolean) which only holds the values true and false  Variables of type bool are declared just like the other types  To implement the algorithm, the program needs to hold whether or not it is counting up  The C# language provides a type called bool (short for Boolean) which only holds the values true and false  Variables of type bool are declared just like the other types Chapter 2.3: Making Decisions in a Program8 // The Game World - our color values byte redIntensity = 0; bool redCountingUp = true; // The Game World - our color values byte redIntensity = 0; bool redCountingUp = true;

Making Decisions in Programs  A conditional statement lets a program perform an action if a given condition is true  The condition must be a Boolean value, so we can use the value of a Boolean variable directly in a test  In the above code the intensity is only increased if the value redCountingUp is true  A conditional statement lets a program perform an action if a given condition is true  The condition must be a Boolean value, so we can use the value of a Boolean variable directly in a test  In the above code the intensity is only increased if the value redCountingUp is true Chapter 2.3: Making Decisions in a Program9

Making Stupid Decisions  The value true can be used directly in a program if you wish  However, this is not always useful since in the above code the intensity value will always be increased, as the condition is true  Note that this is not invalid C#, the compiler will not give you an error, even though the test might as well not be there  The value true can be used directly in a program if you wish  However, this is not always useful since in the above code the intensity value will always be increased, as the condition is true  Note that this is not invalid C#, the compiler will not give you an error, even though the test might as well not be there Chapter 2.3: Making Decisions in a Program10 if (true) redIntensity++;

Detecting Stupid Decisions  If the program uses the value false in a condition it “switches off” the statement that follows  The compiler will notice this and give a warning, since it can tell that there is a statement which will never be obeyed  You should always heed warnings from the compiler, as they often indicate that the program might not be correct  If the program uses the value false in a condition it “switches off” the statement that follows  The compiler will notice this and give a warning, since it can tell that there is a statement which will never be obeyed  You should always heed warnings from the compiler, as they often indicate that the program might not be correct Chapter 2.3: Making Decisions in a Program11 if (false) redIntensity++;

2. Conditional Red Light Chapter 2.3: Making Decisions in a Program12  This version of MoodLight just uses the color red  It can be controlled by a condition as we have just seen  This version of MoodLight just uses the color red  It can be controlled by a condition as we have just seen

Adding an else Part to a Conditional Statement  An if construction can select between two statements  One is performed if the condition is true  The other is performed if the condition is false  The above statement will perform the first two steps of the algorithm we are implementing  The operator - - will reduce the value in a variable by 1  An if construction can select between two statements  One is performed if the condition is true  The other is performed if the condition is false  The above statement will perform the first two steps of the algorithm we are implementing  The operator - - will reduce the value in a variable by 1 Chapter 2.3: Making Decisions in a Program13 if (redCountingUp) redIntensity++; else redIntensity--;

Tests in Conditions  A condition can be a test, rather than a Boolean value  A condition works out to either true or false  The == operator in the condition above is performing a test for equality  A condition can be a test, rather than a Boolean value  A condition works out to either true or false  The == operator in the condition above is performing a test for equality Chapter 2.3: Making Decisions in a Program14

Fun with Conditions  Some of these conditions are stupid  Some are not legal C# because they combine things in an incorrect way  One of them is sensible, if not the best way to do the job  Can you tell which is which?  Some of these conditions are stupid  Some are not legal C# because they combine things in an incorrect way  One of them is sensible, if not the best way to do the job  Can you tell which is which? Chapter 2.3: Making Decisions in a Program15 if (false == true) redIntensity++; if ( redCountingUp == 1 ) redIntensity++; if ( false == 0 ) redIntensity++; if ( redCountingUp == false ) redIntensity--; if (false == true) redIntensity++; if ( redCountingUp == 1 ) redIntensity++; if ( false == 0 ) redIntensity++; if ( redCountingUp == false ) redIntensity--;

Fun with Conditions  Orange lines are silly but would run (with warnings)  Red lines are not legal C#  Things are being combined in a way that is not correct  Green lines would work fine, and are even sensible in the program we are writing  Although using an else part would make more sense  Orange lines are silly but would run (with warnings)  Red lines are not legal C#  Things are being combined in a way that is not correct  Green lines would work fine, and are even sensible in the program we are writing  Although using an else part would make more sense Chapter 2.3: Making Decisions in a Program16 if (false == true) redIntensity++; if ( redCountingUp == 1 ) redIntensity++; if ( false == 0 ) redIntensity++; if ( redCountingUp == false ) redIntensity--; if (false == true) redIntensity++; if ( redCountingUp == 1 ) redIntensity++; if ( false == 0 ) redIntensity++; if ( redCountingUp == false ) redIntensity--;

Completing the Algorithm  This is all the steps of the algorithm converted into C#  It would allow us to create a MoodLight that fades smoothly from black to red and back again  This code would go into the Update method  The Draw method would not need to change  This is all the steps of the algorithm converted into C#  It would allow us to create a MoodLight that fades smoothly from black to red and back again  This code would go into the Update method  The Draw method would not need to change Chapter 2.3: Making Decisions in a Program17 if (redCountingUp) redIntensity++; else redIntensity--; if (redIntensity == 255) redCountingUp = false; if (redIntensity == 0) redCountingUp = true; if (redCountingUp) redIntensity++; else redIntensity--; if (redIntensity == 255) redCountingUp = false; if (redIntensity == 0) redCountingUp = true;

3. Fading Red Light Chapter 2.3: Making Decisions in a Program18  This version of MoodLight just uses the color red  It can be controlled by a condition as we have just seen  This version of MoodLight just uses the color red  It can be controlled by a condition as we have just seen

Setting Initial Variable Values  The C# language lets us set initial values for variables that we declare  By default (i.e. if we don’t do anything), numbers are 0 and bool values are false  However, we can set them to any starting value we like, as shown above  The C# language lets us set initial values for variables that we declare  By default (i.e. if we don’t do anything), numbers are 0 and bool values are false  However, we can set them to any starting value we like, as shown above Chapter 2.3: Making Decisions in a Program19 // Game World byte redIntensity = 0; bool redCountingUp = true; // Game World byte redIntensity = 0; bool redCountingUp = true;

Statements and Blocks  You can put a block of statements after a condition rather than a single statement  This lets you use a single condition to select a large amount of code  You can put a block of statements after a condition rather than a single statement  This lets you use a single condition to select a large amount of code Chapter 2.3: Making Decisions in a Program20 if (redCountingUp) { redIntensity++; } else { redIntensity--; }

Customer Question  At this point the customer comes in with a request for our red MoodLight  Customers always want to change what you have made!  The customer wants the MoodLight to start off bright and get darker, rather than start off dark and get brighter  Do you have any idea how we could achieve this?  At this point the customer comes in with a request for our red MoodLight  Customers always want to change what you have made!  The customer wants the MoodLight to start off bright and get darker, rather than start off dark and get brighter  Do you have any idea how we could achieve this? Chapter 2.3: Making Decisions in a Program21

Starting with a Bright Display  These initial settings should make the program start with the light at full brightness (255) and then make the light get dimmer as the intensity value is reduced on each Update  This is the direct reverse of the original starting condition  These initial settings should make the program start with the light at full brightness (255) and then make the light get dimmer as the intensity value is reduced on each Update  This is the direct reverse of the original starting condition Chapter 2.3: Making Decisions in a Program22 // Game World byte redIntensity = 255; bool redCountingUp = false; // Game World byte redIntensity = 255; bool redCountingUp = false;

4. Start Bright Fading Light Chapter 2.3: Making Decisions in a Program23  This version of MoodLight starts bright and then fades down  The algorithm is just the same, but it starts from different values  This version of MoodLight starts bright and then fades down  The algorithm is just the same, but it starts from different values

Spot the Bug  These initial conditions will cause the program to do something that you might not want  The programmer intended to make the screen get steadily brighter, but when the program runs this does not happen  Run the program manually to see why  These initial conditions will cause the program to do something that you might not want  The programmer intended to make the screen get steadily brighter, but when the program runs this does not happen  Run the program manually to see why Chapter 2.3: Making Decisions in a Program24 // Game World byte redIntensity = 0; bool redCountingUp = false; // Game World byte redIntensity = 0; bool redCountingUp = false;

5. Broken Start Condition Light Chapter 2.3: Making Decisions in a Program25  This version of MoodLight is supposed to fade up from 0  But instead it goes the wrong way  Why?  This version of MoodLight is supposed to fade up from 0  But instead it goes the wrong way  Why?

Working Through the Code  In the first statement, because redCountingUp is false, the program will try to subtract 1 from redIntensity  This means it will subtract 1 from 0  The byte type does not support negative values, and so it underflows to the value 255  This is like overflow, but heading in the other direction  In the first statement, because redCountingUp is false, the program will try to subtract 1 from redIntensity  This means it will subtract 1 from 0  The byte type does not support negative values, and so it underflows to the value 255  This is like overflow, but heading in the other direction Chapter 2.3: Making Decisions in a Program26 if (redCountingUp) redIntensity++; else redIntensity--; if (redIntensity == 255) redCountingUp = false; if (redIntensity == 0) redCountingUp = true; if (redCountingUp) redIntensity++; else redIntensity--; if (redIntensity == 255) redCountingUp = false; if (redIntensity == 0) redCountingUp = true;

Fixing the Problem  The simplest way to fix this bug is to change the order of the tests and the updates  This means that the count direction is always set correctly before the update is performed  Remember that the order statements are performed can have a great effect on what they end up doing  The simplest way to fix this bug is to change the order of the tests and the updates  This means that the count direction is always set correctly before the update is performed  Remember that the order statements are performed can have a great effect on what they end up doing Chapter 2.3: Making Decisions in a Program27 if (redIntensity == 255) redCountingUp = false; if (redIntensity == 0) redCountingUp = true; if (redCountingUp) redIntensity++; else redIntensity--; if (redIntensity == 255) redCountingUp = false; if (redIntensity == 0) redCountingUp = true; if (redCountingUp) redIntensity++; else redIntensity--;

Summary  The starting point of any program has to be an algorithm that solves the problem that the program is being written to solve  C# provides a Boolean data type called bool that can be either true or false and be used as the basis of conditions in decisions  The C# if construction allows a program to selectively execute program statements  Conditional operators allow a program to compare items and make decisions on their values  The starting point of any program has to be an algorithm that solves the problem that the program is being written to solve  C# provides a Boolean data type called bool that can be either true or false and be used as the basis of conditions in decisions  The C# if construction allows a program to selectively execute program statements  Conditional operators allow a program to compare items and make decisions on their values Chapter 2.3: Making Decisions in a Program28

True/False Revision Quiz  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality.  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality. Chapter 2.3: Making Decisions in a Program29

True/False Revision Quiz  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality.  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality. Chapter 2.3: Making Decisions in a Program30

True/False Revision Quiz  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality.  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality. Chapter 2.3: Making Decisions in a Program31

True/False Revision Quiz  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality.  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality. Chapter 2.3: Making Decisions in a Program32

True/False Revision Quiz  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality.  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality. Chapter 2.3: Making Decisions in a Program33

True/False Revision Quiz  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality.  An algorithm is like a recipe.  The C# bool type holds the values 0 and 1.  A condition in a C# program must evaluate to true or false.  An if construction in C# must always have an else part.  The C# compiler will produce warnings if it thinks a program might not be correct.  The = operator compares two values for equality. Chapter 2.3: Making Decisions in a Program34