201Spring08-L3 - CMPSC 201C Spring 2008 Lecture 3 January...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 201C Spring 2008 Lecture 3 January 18, 2008 Flow of Control There are 4 types of control structures that affect the "flow of control" of the program, i.e. in what order the statements are executed. Sequence - statements are executed in order they are written Branched (decision) - some statements may be executed only when a specific condition is met. Loops (repetition) - statements are repeated Functions- a group of statements are executed at various times in the program Sequence Statement 1 Statement 2 Statement 3 Statement 4 Branch (Decision) false Condition true Statement(s) Statement(s) Loops (Repetition) false Condition true Statement(s) Functions Statement(s) Function call Statement(s) Statement(s) Questions Identifiers Identifiers are names (or symbols) used by the programmer to refer to items such as variables, named constants, functions, classes, etc. The "words" used for identifiers must follow specific guidelines for C++ to be valid. Identifiers should be descriptive of what they stand for. Identifier Rules The identifier cannot be a reserved word (keyword), e.g. int, float, if, while, etc. (See Appendix A for a list )Reserved words are indicated in blue in Visual C++.net). The identifier must be comprised of only letters, numbers, or the underscore. The first character must be a letter or underscore (better to be a letter). C++ is case sensitive so thirdbase is not the same identifier as Thirdbase. Variables and Named Constants Data can be stored in RAM (Random Access Memory), or main memory to be used as needed. Variables and symbolic constants are names for memory locations in RAM. Variables refer to memory locations in which the value stored may change throughout the execution of the program. Named or symbolic constants refer to memory locations in which the values do not change. Use a declaration to set aside memory space. Variable Definitions (Declarations) You must define or declare a variable before you may use it. Allocates memory space and associates a name with that space Variable definitions (declarations) have the general format data type variable name; where the variable name must follow the identifier rules. Questions??? Data Types Data or information is stored in memory. To allocate or reserve memory space for this data, you must decide what type of data will be stored. The data type determines how the data will be stored and what types of operations can be performed on this data. Broad divisions of data types are integers (whole numbers), real numbers (may have fractional parts), characters (letters or numbers that you do not want for arithmetic operations, and boolean (true or false). Within these broad categories are subdivisions based on the range of values to be stored. Integer Data Types Integer variables are memory spaces that will store whole numbers (Table 2.2). short unsigned short int unsigned int long unsigned long 2 bytes 2 bytes 4 bytes 4 bytes 4 bytes 4 bytes -32,768 to +32,767 0 to + 65,535 -2,147,483, 648 to +2,147,483,647 0 to + 4,294,967,295 -2,147,483, 648 to +2,147,483,647 0 to + 4,294,967,295 Items to Note Actual ranges may vary with computer system and the C++ compiler being used. Conventional programming practice didn't allocate any more memory than was necessary. Storing a number outside the range has unpredictable results. Can store integers as octal (precede the number by a zero) or as hexadecimal (precede the number by zero and x.) 0124 0x124 Floating Point Data Types Used to store real numbers, numbers with fractional parts. Precision refers to the number of digits that are stored before the values are rounded. Table (2.1) float double long double 4 bytes single precision numbers between 3.4E-38 and 3.4E38 8 bytes double precision numbers 1.7E-308 and 1.7E308 10 bytes numbers between 1.7E-4932 and 1.7E4932 Storing Numbers Floating point numbers are actually stored as integer part and a fractional part (the mantissa). Because numbers are stored as binary, the fractional parts may not be stored precisely (just like we cannot represent 1/3 in decimal precisely). Therefore you can get representational (round-off errors). Other numeric errors occur when you try to store numbers outside the range or try to add (or subtract) very small numbers with a very large numbers. Boolean Data Boolean data is data that is either true or false and can be stored in a bool variable. False is represented as 0 in the computer. True is represented as 1. However, other integers may be stored in a bool variable. Anything other than 0 (zero) is considered to be true. Character Data C++ has a predefine data type char which will hold a single character value. There are two methods to store multiple characters: Cstrings, string objects. C-strings are actually arrays (collections) of characters. We will discuss these more later. char variablename[n]; String objects are variables that have be declared of the string class. To use the string class, you must employ the preprocessor directive. # include <string> string variable name; Declarations Declarations are statements that tell the computer to allocate memory space and what identifier will be used to refer to that space. All variables and symbolic constants must be declared before they can be used! Variable declarations have the format data type identifier; char someLetter; double someWord; Constant declarations have the format const data type identifier = value; const int numberincase = 12; Declarations Constant declarations often occur before main, therefore are global. Variable declarations that occur within a function are local to that function. Variable declarations may occur any time prior to the use of the variable. However for this course, you will usually declare the variables at the beginning of the function in which they occur. More on Declarations Multiple variables of the same data type can be declared with a single statement. int alpha, beta, gamma, delta; double weight, feet; Questions ...
View Full Document

Ask a homework question - tutors are online