CIS125GA Week 4 Logical Beginnings

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Java Planning our Programs Flowcharts Arithmetic Operators.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
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,
Loops – While, Do, For Repetition Statements Introduction to Arrays
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
CSC 204 Programming I Loop I The while statement.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Application of Data Programming Blocks. Objectives  Understand the use of data programming blocks and their applications  Understand the basic logic.
Semester Review. As we have discussed, Friday we will have in class time for you to work on a program, this program will come with instructions and you.
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
GameDevClub CODE CHEAT SHEET NOTE: ALL OF THE CODE IS CASE-SENSITIVE AND THE SYNTAX IS STRICT SO A LOT OF YOUR ERRORS WILL PROBABLY COME FROM TYPOS If.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Controlling Program Flow with Decision Structures.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Welcome to Computer Programming II! Computer Programming II Summer 2015.
Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming.
Introduction to Decision Structures and Boolean Variables
Content Programming Overview The JVM A brief look at Structure
Computing and Statistical Data Analysis Lecture 2
Bill Tucker Austin Community College COSC 1315
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Debugging and Random Numbers
Intro to C Tutorial 4: Arithmetic and Logical expressions
Lesson Two: Everything You Need to Know
Fundamental of Java Programming Basics of Java Programming
Variables, Printing and if-statements
Operators and Expressions
Lesson 2: Building Blocks of Programming
Arrays, For loop While loop Do while loop
Conditionals & Boolean Expressions
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Number and String Operations
Variables ICS2O.
Coding Concepts (Sub- Programs)
Outline Boolean Expressions The if Statement Comparing Data
CSI 101 Elements of Computing Spring 2009
Chapter-3 Operators.
We’re moving on to more recap from other programming languages
Selection Control Structure
Week 6: Time and triggers!
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2: Java Fundamentals
Boolean Expressions to Make Comparisons
Life is Full of Alternatives
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Unit 3: Variables in Java
Conditionals and Loops
CIS125GA_03 Things that help
Web Programming and Design
Presentation transcript:

CIS125GA Week 4 Logical Beginnings

Contents: 17 Slides Mathematical Operators Logical Operators Nesting Examples Functions Snippets: Calling(); Random.Range Set.Active

Mathematic Operators & Expressions An EXPRESSION is a Mathematical Operation. For asking a Method Math Questions, We simply use math symbols and abbreviations, such as: / Division * Multiplication + Addition - Subtraction % Modulus And ask it in the same way we would write a Math Problem: Move Bunny Forward the distance of : ((4*8)/2)

Shorthand for Mathematical Operations There is Shorthand too! ++ Increment by 1 -- Decrement by one Timer++ is the same as writing Timer = Timer + 1 Combined Operators like += -= *= /= %= Perform a calculation THEN reassign the new number to the variable. X +=Y is the same as writing X = X+Y HP -= Damage is the same as writing HP = HP - Damage

Relational Operators We can ask a question about how 2 things in our world relate to each other. These answer as True or False, known as RETURNING BOOLEANS! We use symbols for this as well. > Greater than < Less Than >= Greater than OR equal to <= Less than OR equal to && And || Or But because the = sign is used to assign a value, like in KillerBunny.Fur = White we can’t use an “ = “ to show equality. Instead we have: == (It’s really really Equal to!) != (OMG! It’s not Equal!) ! (Not!)

Compare and swap like types Variables have to be the same type to compare. Variables of the same type can also be swapped out / assigned for each other. Valid: (HitPointInt = ManaPointInt) Valid: (GameObjectPlayer = GameObjectPotato) BAD: ( GameObjectPlayer = HitPointInt) You can check if a string is the same as another string (string == string) But not if a GameObject is a String BAD: (GameObject == String) However, the NAME of a Gameobject is a String, you can compare that! Valid: (GameObject.name == string) Valid: ( Int >= Int) Valid: ( Bool != True)

Nesting We can also NEST code by putting a piece within a piece within a piece within a dream within a dream within another block of code. (You will do this a lot!) (BTW, this animation looks terrible) Note how Alice staggers the code & uses colored regions to help you see the nesting and control structures Example:

Nesting With Syntax Pay Special Attention to the Brackets. They MUST bookend each IF’s Instruction set. (Disclaimer : I made up this code to make it easier to read, but still get the point across..) Void update () { If (Lancelot is in the forest) If (The Grail Shaped Beacon is on) Do these Instructions; } Else

Example Time! #1 Nesting The nested IF goes in the instructions of the condition that has to be satisfied first. IF ( Weather != Raining) { Run outside in Undies; } Not raining doesn’t mean sunny. It could be snowing. It could be hailing. It could be the apocalypse. All bad Undie-running weather. NEST AN IF to check a second condition. IF ( Weather != Snowing) You can nest as many times as you want.

Example Time! #2 We can also do this with an else.. And pants.. If ( WeatherYucky == True) { if (Weather == Rain) Bring Umbrella; } else // Meaning the weather IS yucky, but is not rain Put on Pants; Else // Meaning the weather is NOT yucky Run outside in Undies;

Example Time! #3 If you have a dice & Coin available, play along! // Int DiceRoll creates a disposable variable especially for this block of instrcutions, and assigns a random number between 1-7 to it. For Random.Range, the minimum number is Possible in the roll, but it will only go up to the number before the maximum. //Flip Coin If (coinFlip == Heads) { // If condition is satisfied, do these instructions: //Roll Dice Int DiceRoll = Random.Range (1,7); If ( DiceRoll > 3) Erika Wins!; } Else // Meaning the dice rolled 3 or less) You Win!; else //meaning coinFlip == Tails You Escape!;

Example Time! #4 Math and assign! The combination operators -=, +=, /= and *= are really handy shorthand in gaming situations. MyHitPoints -= FireBallDamage; The code takes MyHitPoints int MyHitPoints = 25; Subtracts FireBall Damage int FireBallDamage = 5; And reassigns the number to the variable MyHitPoints. Now int MyHitPoints = 20; This can be done with any mathematical operators * / - + %

Functions Methods / Functions: A named section of a program that performs a specific task. All Methods are functions. Methods are specifically functions that are owned or associated with an object, so in object oriented programming (OOP), all functions are methods.

Making Functions function Return type- Functions need to know what kind of data you want to get back from them (Final product). If you want nothing back, you put void as a null data type. Name- You need to name your functions. Name them logically to save pain and suffering later. () Container- Sometimes we PASS information to a function. We need to put in a container even if we have nothing to give it right now. {Curly Braces}- Tell us where the instructions begin and end, so we know what instructions are owned by what code. void BestFunctionEver () { // Brackets Bookend the instructions Do these Instructions; } Unity has built in functions with names that are Reserved, like Start, Update, FixedUpdate etc. Built in functions work in specific ways.

Calling(); You cannot make a function inside another function. If you need a functions instructions to run inside another function, you call it. Call it by name, add a container() (in case you want to give it some information) and add a SemiColon; at the end, because it’s an instruction. This code runs; Then we call and THAT code runs; BestFunctionEver(); Then we continue here, where we left off;

Random.Range (Min, Max); Random.Range picks a randomized number (Think of it as a dice roll!) It must be assigned to either an INT or FLOAT variable. It needs information to run: Minimum number in range Maximum number in range When using a FLOAT, maximum is inclusive When using an INT, maximum is exclusive, meaning the roll can go UP to that number, but not include that number. MyHP = Random.Range (1, 21);

****.SetActive(bool); Sometimes, you just want things to disappear. In Unity, you can toggle if something is ACTIVE in a scene or not. .SetActive requires a Boolean. Erika.SetActive(false);