ITP 342 Mobile App Dev. Code

Similar documents
CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

COMP Primitive and Class Types. Yi Hong May 14, 2015

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

The C++ Language. Arizona State University 1

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

Chapter 2: Basic Elements of 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++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Data types, variables, constants

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Computer System and programming in C

ITP 342 Mobile App Dev. Fundamentals

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Creating a C++ Program

ITP 342 Mobile App Dev. Fundamentals

More about Binary 9/6/2016

BASIC ELEMENTS OF A COMPUTER PROGRAM

LECTURE 02 INTRODUCTION TO C++

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Reserved Words and Identifiers

REVIEW. The C++ Programming Language. CS 151 Review #2

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

Programming for Engineers Introduction to C

Basics of Java Programming

Programming and Data Structures

Chapter 2: Using Data

CS102: Variables and Expressions

Declaration and Memory

Lecture 3 Tao Wang 1

A First Program - Greeting.cpp

Outline. Data and Operations. Data Types. Integral Types

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter 1 & 2 Introduction to C Language

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Getting started with Java

BITG 1233: Introduction to C++

Arithmetic Expressions in C

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

EXPRESSIONS AND ASSIGNMENT CITS1001

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

ISA 563 : Fundamentals of Systems Programming

Maciej Sobieraj. Lecture 1

Bits, Words, and Integers

Information Science 1

Course Outline. Introduction to java

Introduction to Programming

Unit 3. Constants and Expressions

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Full file at

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

From Pseudcode Algorithms directly to C++ programs

Chapter 1 Introduction to Computers and C++ Programming

Chapter 3: Operators, Expressions and Type Conversion

LECTURE 3 C++ Basics Part 2

Object-Oriented Programming

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

CGS 3066: Spring 2015 JavaScript Reference

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

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

Objectives. Data Types (continued) Data Types 4. การเข ยนโปรแกรมพ นฐาน ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

Midterms Save the Dates!

Work relative to other classes

Arithmetic type issues

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

UNIT- 3 Introduction to C++

DaMPL. Language Reference Manual. Henrique Grando

printf( Please enter another number: ); scanf( %d, &num2);

Program Elements -- Introduction

Fundamental of Programming (C)

Lecture 4 CSE July 1992

C/C++ Programming for Engineers: Working with Integer Variables

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Program Fundamentals

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

Data types Expressions Variables Assignment. COMP1400 Week 2

2. Numbers In, Numbers Out

Full file at

BEGINNING PROBLEM-SOLVING CONCEPTS FOR THE COMPUTER. Chapter 2

Objectives. In this chapter, you will:

Transcription:

ITP 342 Mobile App Dev Code

Comments Variables Arithmetic operators Format specifiers if - else Relational operators Logical operators Constants Outline 2

Comments For a single line comment, use // The compiler ignores any characters that follow these slashes, up to the end of the line // Button_FunViewController.h! #import <UIKit/UIKit.h>! @interface Button_FunViewController : UIViewController {!UILabel *statustext; // Displays a message! }! @property (nonatomic, retain) IBOutlet UILabel *statustext;! - (IBAction) buttonpressed:(id)sender;! @end! 3

Comments For a multi-line comment, use /* */ Use when comments span many lines of code /*! Button_FunViewController.h! Created by Trina Gregory! */! #import <UIKit/UIKit.h>! @interface Button_FunViewController : UIViewController {!UILabel *statustext;! }! @property (nonatomic, retain) IBOutlet UILabel *statustext;! - (IBAction) buttonpressed:(id)sender;! @end! 4

Instance Variables Specify the types of data to be stored in your class along with the names of those data types Data types can be: C basic types like int, float, char Objective-C defined types like BOOL Other classes that you have created or ones that are already created such as NSString Names Begin with a letter or underscore (_) Followed by any combo of letters (upper- or lowercase), underscores, or the digits 0-9 Cannot be a reserved word like int 5

Variables Variables allow us to store information The C programming language is a statically typed language We have to tell upfront what kind of data a variable is going to hold Format to declare a variable: type name; Examples: int num1; float score; Declare once, use many 6

C Data Types There are five basic data types associated with variables: int integer, a whole number float floating point value (i.e., a number with a fractional part) double a double-precision floating point value char a single character void valueless special purpose type 7

Defined Types The principal types used in Objective-C are defined in objc/objc.h 8

Assignment To assign variables a value, use = The value has to match the type of the variable Format: name = value; Example: num = 20; Sometimes you will want to declare and assign at the same time Format: type name = value; Example: int num = 20; 9

Strings Strings of characters are surrounded by double quotes In Objective-C, pass strings around in NSString objects NSString class provides an object wrapper for strings with advantages such as: Built-in memory management for storing arbitrary-length strings Support for Unicode printf-style utilities Shorthand notation for creating NSString objects NSString* name = @"Trina Gregory"; 10

Arithmetic Operators Arithmetic expressions can be formed using the +, -, *, and / operators together with variables or numbers referred to as operands When both operands are of the same type, the result is of that type When one of the operands is a floating-point type and the other is an integer, the result is a floating point type Example: int hoursworked = 40; float payrate = 8.25; Operator Function + Addition - Subtraction / Division * Multiplication % Modulus float pay = hoursworked * payrate; // will equal 500.0 11

The Division Operator The division operator (/) behaves as expected if one of the operands is a floatingpoint type When both operands are integer types, the result is truncated, not rounded Example: 14.0 / 5 has a value of 2.8 14 / 5 has a value of 2 99 / 100 has a value of 0! 12

The mod Operator The mod (%) operator is used with operators of integer type to obtain the remainder after integer division 14 divided by 4 is 3 with a remainder of 2 Hence, 14 % 4 is equal to 2 The mod operator has many uses, including determining if an integer is odd or even determining if one integer is evenly divisible by another integer 13

Example Code int num1 = 10; int num2 = 3; int result; result = num1 + num2; NSLog(@"%i + %i = %i", num1, num2, result); result = num1 - num2; NSLog(@"%i - %i = %i", num1, num2, result); result = num1 * num2; NSLog(@"%i * %i = %i", num1, num2, result); result = num1 / num2; NSLog(@"%i / %i = %i", num1, num2, result); result = num1 % num2; NSLog(@"%i mod %i = %i", num1, num2, result); Output 10 + 3 = 13 10-3 = 7 10 * 3 = 30 10 / 3 = 3 10 mod 3 = 1 14

Format Specifiers When using NSString or NSLog, we format depending on the type of the variable Specifer" %@ Description" object such as NSString" %% % character" %d,%d,%i %u,%u integer (int)" unsigned integer" %f floating point number (double)" %e floating point number in scientific notation" %g floating point number in %e or %f" %s array of characters (C-string)" %c character (char)" 15

Flow of Control Flow of control is the order in which a program performs actions Up to this point, the order has been sequential A branching statement chooses between two or more possible actions A loop statement repeats an action until a stopping condition occurs 16

if - else Statement A branching statement that chooses between two possible actions if (booleanexpressionistrue) { statement1; statement2;... } else // booleanexpressionisfalse { statement3; statement4;... }" 17

Boolean Expressions The value of a boolean expression is either TRUE or FALSE (YES or NO) Examples: num > 0 num < 20 balance <= 0 time < limit The relational and logical operators together form the Boolean operators since their result is always TRUE or FALSE 18

Relational Operators Operator Function > Greater than >= Greater than or equal to == Equal to!= Not equal to <= Less than or equal to < Less than 19

Logical Operator - AND Boolean expressions can be combined using the "and (&&) operator Example if ((score > 0) && (score <= 100)) Not allowed if (0 < score <= 100) True when both expressions are true "If you eat your peas and your carrots, then you will get dessert." 20

Logical Operator - OR Boolean expressions can be combined using the or ( ) operator Example if ((quantity > 5) (cost < 10)) True when one or both expressions are true "If you eat your peas or your carrots, then you will get dessert." 21

Logical Operator - NOT A boolean expression can be negated using the not (!) operator Syntax!(booleanExpression) True when boolean expression is false 22

Logic Table A B A && B A B!(A) YES YES YES YES NO YES NO NO YES NO NO YES NO YES YES NO NO NO NO YES 23

Constants Sometimes we want to give a value to something that will not change (not be variable) To define constants, you can use #define #define kshowsegmentindex 0 #define kmaxplayers 4 When this symbol is encountered in your code, it will be replaced as follows: if (numplayers < kmaxplayers) {... } becomes if (numplayers < 4) {... } 24

#define The #define command can also be used to define functions, like this: #define kpi 3.141f #define square(x) ((x) * (x)) When used in your code, this... float area = kpi * square(r) becomes this... float area = 3.141f * ((r) * (r)) 25

#define Why did we use all those parentheses? Consider this: #define square(x) x * x! When used as follows, this... int a = square(b + c)! becomes this... int a = b + (c * b) + c! It s important to realize that #define is evaluated by the preprocessor, not the compiler Think of it as a textual search-and-replace in your code, done before the compiler ever sees it 26