Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2000 Scott S Albert Problem Solving Using C++ Structured Programming 256 Chapter 2.

Similar presentations


Presentation on theme: "© 2000 Scott S Albert Problem Solving Using C++ Structured Programming 256 Chapter 2."— Presentation transcript:

1 © 2000 Scott S Albert Problem Solving Using C++ Structured Programming 256 Chapter 2

2 © 2000 Scott S Albert C++ and Other Computer Languages C++ has many similarities to other computer languages –Very similar to Java and Smalltalk Shares many concepts with –Visual Basic, Delphi Inherits many characteristics from –Pascal, PL/1, C, Algol, Fortran, Cobol

3 © 2000 Scott S Albert Variables Used to store data during program execution –Results of calculations –Temporary values –Control information Can hold many different types of data –Numbers, characters, strings of characters and objects ( collections of data and programs )

4 © 2000 Scott S Albert What is a program variable? A named location to store data –a container for data It can hold only one type of data –for example only integers, only floating point (real) numbers, or only characters

5 © 2000 Scott S Albert Post Office Metaphor Post Offices contain a section of PO boxes –Boxes are like memory. Available to store all kinds of information. Boxes are allocated, assigned a name (number). Computer memory –Can store any type of data (numbers, strings etc.) –Available for allocation and assignment

6 © 2000 Scott S Albert Creating variables All program variables must be declared before using them A variable declaration associates a name with a storage location in memory and specifies the type of data it will store: Type Variable_1, Variable_2, …; For example, to create three integer variables to store the number of baskets, number of eggs per basket, and total number of eggs: int numberOfBaskets, eggsPerBasket, totalEggs;

7 © 2000 Scott S Albert Variables Variables store data in a program Variables must be declared (or defined) before they are used –Variable declarations Variable type Variable name (identifier) –Example int nPosition, nHeight, nWidth; char cAnswer;

8 © 2000 Scott S Albert Variable Names Naming rules –Identifiers must start with a letter, may contain letters or numbers or _ –There is essentially no limit to the length of an identifier Variable name should match its use –Use dName rather than a –Use nHeight rather than x

9 © 2000 Scott S Albert Variable Types Primitive types –Basic building blocks of the C++ language. Match intrinsic hardware capabilities. Class types –Contain: One or more primitive types Plus associated C++ statements to manage the primitive types Possibly even other class types

10 © 2000 Scott S Albert Primitive Types shortinteger1 byte intinteger2 bytes unsignedinteger4 bytes longinteger8 bytes floatreal4 bytes doublereal8 bytes charcharacter2 bytes boollogical1 bit/1 byte Use the sizeof() operator for a particular system

11 © 2000 Scott S Albert Primitive Data Types

12 © 2000 Scott S Albert Primitive Types Integer –Signed whole numbers only (no decimal) Real (Scientific Notation) –Signed numbers with two portions An exponent A mantissa with a fixed precision Char –ASCII character

13 © 2000 Scott S Albert Integer vs Real Numbers Why two different systems? –Clearly the range of integers fit into the range of real numbers Integers are computationally very efficient –If you can use integers, do so your program will run much faster Integers are precise –By their nature, real numbers often have very small errors that creep into their representation. Integers do not have this issue.

14 © 2000 Scott S Albert Variable names: Identifiers Rules - these must be obeyed all C++ identifiers must follow the same rules must not start with a digit must contain only numbers, letters, underscore (_) and $ (but avoid using $, it is reserved for special purposes) names are case-sensitive (ThisName and thisName are two different variable names) Good Programming Practice - these should be obeyed always use meaningful names from the problem domain (for example, eggsPerBasket instead of n, which is meaningless, or count, which is not meaningful enough) start variable names with lower case capitalize interior words (use eggsPerBasket instead of eggsperbasket ) avoid using $ since it is reserved for special purposes

15 © 2000 Scott S Albert Two main kinds of types in C++ primitive data types the simplest types cannot decompose into other types values only, no methods Examples: int - integer double - floating point (real) char - character class types more complex composed of other types (primitive or class types) both data and methods

16 © 2000 Scott S Albert Syntax Templates Efficient method to communicate the grammar of a computer language –Similar to a sentence diagram –Indicates the required and optional portions of a statement.

17 © 2000 Scott S Albert Assignment Statements Syntax template Variable = Expression ; Operation –The expression on the right hand side is evaluated and assigned to the memory location named by the variable on the left hand side.

18 © 2000 Scott S Albert Expressions Simple expression –Variable Arithmetic expressions –Binary operators (require two values) + - * / % –Unary operators (only one value needed) + - ++ --

19 © 2000 Scott S Albert Expression Examples int nPosition, nHeight, nWidth; nPosition = 0; nHeight = 5; nWidth = nHeight; nHeight = nHeight * 2; nPosition = nHeight + nWidth * 3 + 5; float nXPos, nYPos; nXPos = 2.3; nYPos = nXPos / 0.3 ; nHeight = -nWidth; nHeight = nWidth * -nPosition;

20 © 2000 Scott S Albert Additional Arithmetic Operators Increment and Decrement –++ will increment a variable by 1 –-- will decrement a variable by 1 –A unary operator that can be prefix or postfix Modulus operator % –Integer division yields an integer result –% Returns the remainder of an integer division

21 © 2000 Scott S Albert Samples int nSomeNumber, nQuotient, nRemainder; nSomeNumber = 17; nQuotient = nSomeNumber / 3; nRemainder = nSomeNumber % 3; float nAnswer; nAnswer = nSomeNumber / 3.0; nSomeNumber++; nQuotient = 7; nSomeNumber = nQuotient ++; nSomeNumber = ++nQuotient ;

22 © 2000 Scott S Albert Samples void main() { cout<<"Hello World”<<endl; int nSomeNumber, nQuotient, nRemainder; nSomeNumber = 17; nQuotient = 7; nSomeNumber = nQuotient++; cout<<"nQuotient postfix is " << nQuotient<<endl; cout<<"nSomeNumber postfix is " << nSomeNumber<<endl; nSomeNumber = ++nQuotient ; cout<<"nQuotient prefix is " +<<nQuotient<<endl; cout<<"nSomeNumber prefix is " << nSomeNumber<<endl; }

23 © 2000 Scott S Albert Examples to run int nCount = 0; int nValue = 0; nValue = nCount++; cout<<"NCount is"+nCount+"nValue is ”<< nValue<<endl; nValue = ++nCount; cout<<"NCount is "+nCount+" nValue is ”<< nValue<<endl; nCount++; ++nCount; nValue = nValue + 1; nCount = nValue--; nCount = nValue - 1;

24 © 2000 Scott S Albert Prefix and Postfix A prefix ++ or -- is evaluated (executed) first and the new value is available in the current expression A postfix ++ or -- is evaluated (executed) after the expression has been evaluated therefore the new value IS NOT available in the current expression

25 © 2000 Scott S Albert Precedence Rules How is the following expression evaluated? –nPosition = nHeigth / nWidth + 5 * 3; Why? Precedence rules (introduction) –First unary operators +, -, ++, -- –Next binary operators *, /, % –Then binary operators +, - –Unless there are parenthesis ( )

26 © 2000 Scott S Albert Precedence Exercise int nCount = 0; int nValue = 0; nValue = 1 + 2 – 3 – 4; nValue = 16 * 4 / 2 * 3; nValue = 7 – 3 * 4 + 2; nValue = 6 * -nCount + 5; nValue = (6 * (-nCount)) + 5;

27 © 2000 Scott S Albert Conversion between types Implicit conversion –C++ will automatically convert some types. This conversion is often call promotion since the conversion always moves to a more expansive type. –short  int  long  float  double –Any type can be promoted to a type on its right byte can be promoted to long float can not be promoted to short

28 © 2000 Scott S Albert Sample int foo; double foobar; foobar = 2.5; foo = foobar; cout<<"foobar is " << foobar;

29 © 2000 Scott S Albert Conversion between types Explicit conversion C++ allows the programmer to explicitly specify a conversion though a cast –This allows a conversion that would not be done through promotion, but should be used with caution. Values are truncated (not rounded) without warning. Special issues when casting characters to numbers

30 © 2000 Scott S Albert Key escape characters Double quote –\” Single quote –\’ New line –\n Carriage return –\r Tab –\t Backslash –\\

31 © 2000 Scott S Albert Console I/O Input from the keyboard, output to a “console” window. –Not Windows programming per se –Rather a rudimentary input and output mechanism useful for simple programs where we don’t want to spend a great deal of time on the user interface No mouse, graphics, multiple windows etc.

32 © 2000 Scott S Albert Output Cout<< –Outputs to the console window Use endl or \n for a new line

33 © 2000 Scott S Albert Input Methods Cin>>

34 © 2000 Scott S Albert A Matter of Programming Style Using a good programming style is essential –Allows you to communicate your design to team –Makes your program more easy to modify later –Communicates a clean and professional message Most programming organizations have development standards –Variable names, class names, comments etc.

35 © 2000 Scott S Albert Style Pointers Use meaningful names for variables and all identifiers –I often use a prefix to the variable that communicates its type. This is optional. Use comments to explain the program –Comments are ignored by the compiler, rather they are used to communicate with other programmers (or yourself).

36 © 2000 Scott S Albert Comments Two types of comments –// -- everything after a // is ignored by the compiler to the end of the line –/* -- everything after a /* is ignored by the compiler until a */ is reached

37 © 2000 Scott S Albert Style Pointers Indenting –Indenting your source code makes it easier to see the beginning and ending of blocks. Named constants –Rather than using values like 100 as a maximum number, use a named constant of MAXNUMBER. –Benefits – easier to understand, and most importantly, easier to modifier later.

38 © 2000 Scott S Albert Named Constants Named constants are of type –const Normally there names are all upper case Examples const int MAXNUMBER = 100; const float MINTEMP = -40.0;

39 © 2000 Scott S Albert Summary We are starting to see the major foundations of the C++ language –Basic types Conversion between types –Assignment statements –Arithmetic expressions –Etc.


Download ppt "© 2000 Scott S Albert Problem Solving Using C++ Structured Programming 256 Chapter 2."

Similar presentations


Ads by Google