Lesson 3: Basic Programming Concepts

Similar documents
Unit 3. Operators. School of Science and Technology INTRODUCTION

The Order of Operations. Unit 1 Lesson 3

Ex: If you use a program to record sales, you will want to remember data:

CCBC Math 081 Order of Operations Section 1.7. Step 2: Exponents and Roots Simplify any numbers being raised to a power and any numbers under the

Lecture Set 4: More About Methods and More About Operators

Course Outline. Introduction to java

Operators. Java operators are classified into three categories:

Add Subtract Multiply Divide

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Lecture Set 4: More About Methods and More About Operators

CS313D: ADVANCED PROGRAMMING LANGUAGE

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Chapter 2 - Introduction to C Programming

Learning the Language - V

CGS 3066: Spring 2015 JavaScript Reference

Chapter 17. Fundamental Concepts Expressed in JavaScript

1-6 Order of Operations

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

C: How to Program. Week /Mar/05

C++ Programming: From Problem Analysis to Program Design, Third Edition

CSCI 123 Introduction to Programming Concepts in C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Visual C# Instructor s Manual Table of Contents

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

Chapter 1 Operations With Numbers

CSC Web Programming. Introduction to JavaScript

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Chapter 4: Basic C Operators

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Unit E Step-by-Step: Programming with Python

Operators in C. Staff Incharge: S.Sasirekha

Lecture 3 Tao Wang 1

UNIT- 3 Introduction to C++

JavaScript Basics. The Big Picture

Chapter 2: Using Data

Accelerating Information Technology Innovation

ARITHMETIC EXPRESSION

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Full file at

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming.

Chapter 2 Working with Data Types and Operators

QUIZ: What value is stored in a after this

SECTION II: LANGUAGE BASICS

Programming with Java

Homework #3 CS2255 Fall 2012

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Objectives. In this chapter, you will:

Python Unit

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Object-Oriented Programming

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Chapter 2: Using Data

Fundamental of Programming (C)

Logical Operators and switch

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Introduction to Java & Fundamental Data Types

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Reserved Words and Identifiers

CSCE 110 PROGRAMMING FUNDAMENTALS

4. Inputting data or messages to a function is called passing data to the function.

Introduction to Programming using C++

Computer Components. Software{ User Programs. Operating System. Hardware

Welcome to 5 th Grade Math Review Properties of Operations

Week 4 Lecture 1. Expressions and Functions

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CHAD Language Reference Manual

Outline. Data and Operations. Data Types. Integral Types

BITG 1233: Introduction to C++

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

ISA 563 : Fundamentals of Systems Programming

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Math 171 Proficiency Packet on Integers

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CSCE 120: Learning To Code

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Creating a C++ Program

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Excel 2016: Basics 2 Math and Functions

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

COMS 469: Interactive Media II

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Transcription:

3 ICT Gaming Essentials Lesson 3: Basic Programming Concepts LESSON SKILLS After completing this lesson, you will be able to: Explain the types and uses of variables and operators in game programming. Describe the use of events, event handlers and functions in game programming. Describe the use of parameters and arguments in game programming. Describe the use of objects, classes and instances in game programming. Describe the use of properties and methods with objects in game programming. KEY TERMS argument event object assignment operator event handler Object-oriented programming (OOP) Boolean expression operator class function parameter comparative Boolean instance property conditional Boolean method variable

ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-2 Points to Ponder These Points to Ponder are designed to help you focus on key elements in this lesson. They are also suitable for use to spark discussions or individual research. Define "variable." List three properties (or characteristics) of an object in the classroom, such as a chair. Create an example of two string variables; one should be used to contain numbers. Provide the variable name and value for each. What is an event? Provide an example of how it can be used in creating a game. Write the names of three objects that you use on a daily basis. List three properties and methods associated with each object. Create a blueprint for a cell phone object class. List three properties (or characteristics) and three methods that this object class would have.

ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-3 Overview In this lesson, students will learn basic concepts used in programming languages, including variables, events, parameters, objects and properties. These concepts are complex, but students can understand them more easily when they are applied to real-world items. Introduction to Programming Concepts Objectives 10.3.8: Explain the types and uses of variables in game programming. 10.3.9: Describe basic Boolean concepts, including logical operators, order of precedence, and expressions. 10.3.10: Describe the use of events, event handlers and functions in game programming. 10.2.11: Describe the use of parameters and arguments in game programming. 10.3.12: Describe the use of objects, classes and instances in game programming. 10.3.13: Describe the use of properties and methods with objects in game programming. Syntax Just like written languages have words, symbols and rules, so to do programming languages. In written languages, this syntax includes nouns, verbs, pronouns, adjectives and adverbs. Syntax refers to the ways that words and symbols in a programming language are written by a programmer, including correct keywords, spelling and capitalization. This same concept defines programming syntax. Certain words, punctuation marks (such as a semicolon and a curly bracket) and other symbols have different meanings. See the example code in Figure 3-1. Figure 3-1: Example programming code Variables, Expressions and Operators Variables are used to store information in the computer s memory or a database that can be used in a program when executing a program. Variables, then, are like storage containers. Variables help a program work more efficiently. Game programmers use variables to collect and store game and user-input data. This enables the program to interact with users. For example, a variable can be used when prompting the user for his or her name, and the saved name can then be displayed in a welcome message or other alerts. Variables are commonly used for tracking and displaying score information.

ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-4 A variable is assigned a descriptive name that specifies the type of data it stores, such as "playername." The program then refers to the variable name instead of the actual data it holds. For example: If the playername variable stores the value "John," then when the program instructs the computer to print playername, the name John will be displayed on the screen. If the data contained within a variable changes, then the program simply changes that data stored in the variable holder. This eliminates the need to rewrite all of the program code that refers to it. For example, if playername changes to Michael, then the program simply changes the variable data to Michael (playername = Michael). Every reference to the playername variable will update to the new data. Variables can hold different types of data and are classified based on the type of data they hold. Knowing the type of data stored in a variable enables programs to make sure the data is handled and manipulated properly. The five most common data types are listed in Table 3-1. Table 3-1: Common data types Variable Data Type Example Value Description Character String K % John Integer 45 XY3456981182-14 Floating-Point 3.25 5-15.1 Boolean True or False, T or F, 1 or 0 A single letter or symbol A sequence of characters, numbers or symbols Whole numbers that do not contain a decimal Numbers that contain a decimal Used to store the results of a comparison, such as: a > b, x <= y, z == 14 Programmers must tell the computer that a variable exists (known as "declaring a variable"), and they must assign it an initial value (known as "initializing a variable"). If the data stored in the variable is to be calculated or used in any way (such as a Score variable that adds up the points earned during a game), then the programmer must tell the computer how to perform the calculations. This is done using expressions, similar to expressions used in math. Expressions contain a combination of variables, values, functions and operators, which are evaluated and produce another value. This is similar to the expressions used in math. For example, the expression 10-2 evaluates to 8, and (2+8)*(2+1) evaluates to 30. Programming operators are the mathematical symbols that we use every day and expressions are evaluated using the mathematical order of precedence (order of operations): 1. Parentheses ( ) 2. Exponentiation xy

ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-5 3. Multiplication and division * / 4. Addition and subtraction + - You can use the mnemonic Please Excuse My Dear Aunt Sally to remember the order. When both multiplication/division or addition/subtraction are present, then the remaining operations are solved from left to right. Table 3-2: Arithmetic operators Operator Function Example + Addition 10 + 5 = 15 - Subtraction 10 5 = 5 * Multiplication 10 * 5 = 50 / Division 10 / 5 = 2 ++ Increments by 1 10++ returns 11 -- Decrements by 1 10-- returns 9 % Modulus Divides first value by second value and returns the remainder 10 % 3 returns 1 (10 / 3 = 3 remainder 1 The modulus operator returns the remainder) Table 3-3: Comparison operators Operator Function Example == Is equal to 5 = =10 (returns false) cat == dog (returns false)!= Is not equal to 5!= 10 (returns true) cat!= dog (returns true) < Is less than 5 < 10 (returns true) <= Is less than or equal to 5 <= 10 (returns true) > Is greater than 5 > 10 (returns false) >= Is greater than or equal to 5 >= 10 (returns false) Table 3-4: Logical (Boolean) operators Operator Function Example && (AND) Is true if both expressions are true (5 < 10) &&(10 < 20) (returns true) (OR) Is true if either expression is true (5 < 10) (10 > 20) (returns true)! (NOT) Is true if expression is false! 25 < 10

ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-6 The equal sign (=) is considered an assignment operator in most programming languages. The assignment operator links the variable name on the left side of the operator with a value on the right side when initializing a variable, such as var playername = "John". A Boolean (true or false) contains a condition and is often used in repetition and selection structures. Examples include: If this is true then do this, or else if false then do that. While this is true then do this, if false do that. The Scratch example in Figure 3-2 illustrates the two types of Booleans: o o Conditional Booleans check for a specific condition. Comparative Booleans compare values. Figure 3-2: Conditional and comparative Booleans Links to Learn More Scratch Wiki article about variables Overview of Variables (Adobe Flash video, 10 mins)

ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-7 Suggested activities Boolean Operators (Online) Boolean Expressions (Online) The Language of a Programmer (Hands-on) Understanding Variables in Scratch (Hands-on) Objects, Methods and Functions Object-oriented programming (OOP) is a programming language model organized around objects rather than actions and data rather than logic. It relies on defining classes, objects, instances, methods and properties. A class is defined as components that are associated with a certain object. It is a definition or blueprint used to create a particular type of object. A class provides a detailed description of the object's characteristics (such as that the object should include a size and a color), and it specifies what can be done with the object (its functions). When a class is used to build an object, the object is called an instance of the class. Multiple objects (or instances) can be created using the same class. Instances are represented by their class and properties. Properties are the various characteristics of an object, such as size, color and type. Each object created using the class contains the same properties, but the values of the properties may be different. Figure 3-3: An instance of the Football class

ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-8 Functions are actions that a class can do; but, are not part of the class description. They are named units of code that perform a task or cause an action to take place. A function is called upon or executed by a descriptive name, such as "move forward" or "jump." Actions that an object can perform and are a part of the object s description (such as move, rotate or jump) are called methods in programming. The methods that can be applied to the football object in a game program might be to bounce off walls, speed when thrown, or move up or down when the keyboard arrows are pressed. A method is a function of a class in other words, it is actions the class can perform and is part of how the class is defined. Each object created using the class shares the same methods (functions) as all the other objects created from the same class. That is, all objects in a class can do the same things, such as bounce off walls or speed when thrown. Suggested activity Objects, Methods and Functions Simplified (Hands-on) Arguments and Parameters Methods often need more information before they can execute. For example, the methods will need to know how far to move the object or how many degrees to rotate the object. The information is given to the method using arguments and parameters. An argument is the value that is passed to a method when it is called so that it knows what to do. For example, an argument given to the rotate method could be 90 or 180. A parameter is the variable that holds the argument data (value) needed by the method. For example, degrees=90 or steps=10. So in this example, degrees and steps are the parameters. The values assigned to the parameters 90 and 10 are the arguments. Events and Event Handlers An event is an action that takes place while a program is running, such as a mouse click or the pressing of a key. Programmers use functions called event handlers to tell the program what to do when an event occurs. The event handler, then, is the code that is called when an event occurs. For example, if a programmer wants an object to turn and move left when the left-arrow key is pressed, they would write an event handler that contains the instructions to turn and move the object left when the left-arrow key is pressed. Then, every time the left-arrow key is pressed while the program is running, the left-arrow-key event is triggered and the associated event handler will execute so the object will turn and move left.

ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-9 Suggested activity Objects, Arguments, Events and Event Handlers in Scratch (Hands-on) Case Study Programming Your Game and Gathering Feedback (Hands-on)