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