This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 3 C++ BASICS C++ Variable Declaration Statements You must declare your variables: Syntax: type identifier; Integer types short i; // same as: signed short int i; 2byte signed integer, 32,768 to 32,767 unsigned short int i; 2byte unsigned integer, 0 to 65,535 long i; // same as: signed long int i; 4byte signed integer, +2,147,483,647 unsigned long int i; 4byte unsigned integer, 0 to 4,294,967,295 Floating point types float x; 4byte real, 10 38 to 1038 or 0.0 or 1038 to 10 38 7 significant digits double x; 8byte real, +308 exponent range 15 sig. digits Other types char c; Holds a single character, given in single quotes Example: char c = ‘A’; bool ok; Holds a true / false (0/1) value Example: bool ok = true; bool ok = x  y && (c > d); Examples int minutes; // 4byte signed int short pounds; // 2byte signed int long myBalance; // 4byte signed int float acceleration; // 4byte floating point double length; // 8byte floating point Notes: cont. More than one variable can be declared in one declaration statement Example: int i,j,k = 2 ; Variable Value i ???????? j ???????? k 2 Notes: cont. A variable can be declared and initialized simultaneously. The right side can be an expression. Examples: int hours = 10; double pressure = 3.275 ; int minutes = hours* 60 ; Variable Value hours 10 pressure 3.275 minutes 600 Constant declaration It is customary to type the constant variable names in upper case The value of the constants ( IN_PER_FEET and PI in the examples above) may not be changed later in the program. PI = 3.14159 ; PI = 5.0 ; const int IN_PER_FEET = 12 ; const double PI = 22 / 7.0 ; ERROR!!!! C++ Constants Integer constants 163 0243 // octal, base 8, leading 0 0xa3 or 0XA3 // hexadecimal, base 16 Floating point constants 100. or 100.0 100.25 0.2 or .2 1.0025E12 or 1.0025e12 0.125E7 float x = 100. ; float y = 100.0 ; int z = 100 ; C++ Constants Logical constants true/false // 0 or 0.0 means false, else true String constants “STRING” “ANOTHER !@#$%^& STRING” “He said, \”I\'m OK!\"\n" “123" ”A" C++ Constants Character constants 'A' '\n' //new line; '\r' //carriage return '\x31' //hex char code '\0' //null character ‘\a' //bell; '\t' //tab '\\' //backslash ' \' ' //single quote ' \" ' //double quote C++ Expressions and Assignment Statements Expressions, when evaluated, return a value. Expressions can be used in many places: assignment statements, if stmts., cout stmts., etc 1 + i i + j 7 2 7 / 2 // integer division truncates 7 % 2 // modulo division, remainder 20 + 4 / 2 ( 20 + 4 ) / 2 sin( 1.5 )+j / k C++ Expressions and Assignment Statements Assignment Statements: variable = expression; i = 1; i = i + j; // or i += j; x = sqrt(cos(fabs(y)z)); x = 3 / 4 ; // Integer division truncates!...
View
Full
Document
This note was uploaded on 09/05/2011 for the course CGS 2421 taught by Professor Onal during the Fall '09 term at University of Florida.
 Fall '09
 ONAL

Click to edit the document details