Conditionals Art &Technology, 3rd Semester Aalborg University Programming https://art.moodle.aau.dk/course/view.php?id=33 David Meredith

Slides:



Advertisements
Similar presentations
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Advertisements

If Statements & Relational Operators Programming.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
IAT 800 Foundations of Computational Art and Design Lecture 2.
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
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,
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
Chapter 4 Making Decisions
Chapter 4: Basic C Operators
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
CSC 107 – Programming For Science. Follow-Up From Last Lecture.
Functions Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
CSC 107 – Programming For Science. George Boole  Mathematician from English middle-class  Lived from 1815 – 1864  Started work at age 16 as a teaching.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
CONTROL FLOW The order in which blocks are executed is called the “control flow” of the script So far all our scripts have just executed blocks in the.
Lesson Two: Everything You Need to Know
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
LCC 6310 Computation as an Expressive Medium Lecture 2.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Lesson thirteen Conditional Statement "if- else" ©
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.
Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)
Chapter 6 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
CS 139 – Programming Fundamentals Lecture 08A. Relational/comparison Operators Relational Operator Meaning > is greater than < is less than >= is greater.
CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Computer Programming Boolean Logic.
Relational Operator and Operations
CPS120 Introduction to Computer Science
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Computer Science 210 Computer Organization
p5.js mouse, keyboard and if’s
Chapter 6 Conditionals.
Bool operators and, or, not
Section 7.1 Logical Operators
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
JavaScript conditional
Introduction To Robot Decision Making
JavaScript conditional
.Net Programming with C#
Bools and simple if statements
Relational Operators Operator Meaning < Less than > Greater than
Computer Science 210 Computer Organization
Logical Operations In Matlab.
Introduction To Robot Decision Making
Lecture 6: Conditionals AP Computer Science Principles
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Understanding Conditions
JavaScript conditional
LCC 6310 Computation as an Expressive Medium
boolean Expressions Relational, Equality, and Logical Operators
Conditionals.
Chapter 6 Conditionals.
Presentation transcript:

Conditionals Art &Technology, 3rd Semester Aalborg University Programming David Meredith

Overview Boolean expressions Conditional expressions: “if”, “else”, “else if” Logical operators: “&&” and “||” Multiple rollovers Boolean variables Bouncing ball

Boolean expressions Expressions in normal language can be true, false, ambiguous or meaningless, e.g. – TRUE: “The picture shows George Boole ( ), an English mathematician who invented formal logic” – FALSE: “The picture shows Mickey Mouse” – MEANINGLESS: “Flurby zilches blab wibberously” – AMBIGUOUS: “Slow children at play”

Boolean expressions A boolean expression is one that has a truth value This truth value can be either false or true A boolean expression is usually a statement about the relationship between two numbers or variables that contain numbers, e.g. 12 > 10 (true) 5 <= 10 (true) x < 5 (don’t know – depends on value of x)

Relational operators In order to compare numbers in a boolean expression, we use the following operators > greater than <less than >=greater than or equal to <=less than or equal to ==equal to NB:compare with ‘=‘ which means “becomes equal to” !=not equal to

Examples of boolean expressions Comparing numbers 5 < 5 false 5 <= 5 true 5 = 5 error – ‘=‘ is the assignment operator and means “becomes equal to” 5 == 5 true Comparing variables – what are the values of b and c in the following? int x = 5; int y = 7; boolean b = (x == y); boolean c = (x < y); b is false, c is true

if If expression has format: if (boolean expression) { doThis(); doThat(); } doThis() and doThat() are only done if boolean expression is true

if-else When does it draw a green circle? Format of If-Else is: if (bool exp) { doStuff1(); } else { doStuff2(); } Does doStuff1() if bool exp is true, otherwise it does doStuff2()

if-else if if-else if statement has format: if (condition1) { doStuff1(); } else if (condition2) { doStuff2(); } else if (condition3) { doStuff3(); }… } else { doStuffOtherwise(); }

Logical operators We can deal with single boolean expressions: if (I have a fever) {take me to the doctor;} But what about if (I have a fever OR I have a rash) { take me to the doctor; } or if (I am stung by a bee AND I have an allergy) { take me to the doctor; } or if (I am stung by a bee AND I am NOT breathing) { call an ambulance; } For these, we need to be able to combine boolean expressions using the logical operators, “OR”, “AND” and “NOT”

&&, || and ! In processing, && means logical AND || means logical OR ! means logical NOT

Boolean variables – programming a switch

Bouncing ball Speed has two components, x and y The “speed” is actually how much you change the coordinate value between two consecutive frames When the ball hits an edge, the speed is reversed by negating the xSpeed if it hits a vertical edge and negating the ySpeed if it hits a horizontal edge