Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

No Downloads

Total views

19,303

On SlideShare

0

From Embeds

0

Number of Embeds

29

Shares

0

Downloads

1,467

Comments

5

Likes

23

No embeds

No notes for slide

- 1. C++
- 2. The Task of Programming <ul><li>Programming a computer involves writing instructions that enable a computer to carry out a single task or a group of tasks </li></ul><ul><li>A computer programming language requires learning both vocabulary and syntax </li></ul><ul><li>Programmers use many different programming languages, including BASIC, Pascal, COBOL, RPG, and C++ </li></ul><ul><li>The rules of any language make up its syntax </li></ul><ul><li>Machine language is the language that computers can understand; it consists of 1s and 0s </li></ul>1
- 3. The Task of Programming <ul><li>A translator (called either a compiler or an interpreter) checks your program for syntax errors </li></ul><ul><li>A logical error occurs when you use a statement that, although syntactically correct, doesn’t do what you intended </li></ul><ul><li>You run a program by issuing a command to execute the program statements </li></ul><ul><li>You test a program by using sample data to determine whether the program results are correct </li></ul>1
- 4. Programming Universals <ul><li>All programming languages provide methods for directing output to a desired object, such as a monitor screen, printer or file </li></ul><ul><li>Similarly, all programming languages provide methods for sending input into the computer program so that it can be manipulated </li></ul><ul><li>In addition, all programming languages provide for naming locations in computer memory </li></ul><ul><li>These locations commonly are called variables (or attributes ) </li></ul>1
- 5. Programming Universals <ul><li>Ideally, variables have meaningful names, although no programming language actually requires that they meet this standard </li></ul><ul><li>A variable may have only one value at a time, but it is the ability of memory variables to change in value that makes computers and programming worthwhile </li></ul><ul><li>In many computer programming languages, including C++, variables must be explicitly declared , or given a data type as well as a name, before they can be used </li></ul>1
- 6. Programming Universals <ul><li>The type determines what kind of values may be stored in a variable </li></ul><ul><li>Most computer languages allow at least two types: one for numbers and one for characters </li></ul><ul><li>Numeric variables hold values like 13 or -6 </li></ul><ul><li>Character variables hold values like ‘A’ or ‘&’ </li></ul><ul><li>Many languages include even more specialized types, such as integer (for storing whole numbers) or floating point (for storing numbers with decimal places) </li></ul>1
- 7. Procedural Programming <ul><li>Procedural programs consist of a series of steps or procedures that take place one after the other </li></ul><ul><li>The programmer determines the exact conditions under which a procedure takes place, how often it takes place, and when the program stops </li></ul><ul><li>Programmers write procedural programs in many programming languages, such as COBOL, BASIC, FORTRAN, and RPG </li></ul><ul><li>You can also write procedural programs in C++ </li></ul>1
- 8. A main( ) Function in C++ <ul><li>C++ programs consist of modules called functions </li></ul><ul><li>Every statement within every C++ program is contained in a function </li></ul><ul><li>Every function consists of two parts: </li></ul><ul><ul><li>A function header is the initial line of code in a C++ which always has three parts: </li></ul></ul><ul><ul><ul><li>Return type of the function </li></ul></ul></ul><ul><ul><ul><li>Name of the function </li></ul></ul></ul><ul><ul><ul><li>Types and names of any variables enclosed in parentheses, and which the function receives </li></ul></ul></ul><ul><ul><li>A function body </li></ul></ul>1
- 9. Creating a main( ) Function <ul><li>A C++ program may contain many functions, but every C++ program contains at least one function, and that function is called main( ) </li></ul><ul><li>If the main function does not pass values to other programs or receives values from outside the program, then main( ) receives and returns a void type </li></ul><ul><li>The body of every function in a C++ program is contained in curly braces, also known as curly brackets </li></ul>1
- 10. Creating a main( ) Function <ul><li>Every complete C++ statement ends with a semicolon </li></ul><ul><li>Often several statements must be grouped together, as when several statements must occur in a loop </li></ul><ul><li>In such a case, the statements have their own set of opening and closing braces within the main braces, forming a block </li></ul>1
- 11. Working with Variables <ul><li>In C++, you must name and give a type to variables (sometimes called identifiers ) before you can use them </li></ul><ul><li>Names of C++ variables can include letters, numbers, and underscores, but must begin with a letter or underscore </li></ul><ul><li>No spaces or other special characters are allowed within a C++ variable name </li></ul><ul><li>Every programming language contains a few vocabulary words, or keywords , that you need in order to use the language </li></ul>1
- 12. Common C++ Keywords
- 13. Working with Variables <ul><li>A C++ keyword cannot be used as a variable name </li></ul><ul><li>Each named variable must have a type </li></ul><ul><li>C++ supports three simple types: </li></ul><ul><ul><li>Integer — Floating point — Character </li></ul></ul><ul><li>An integer is a whole number, either positive or negative </li></ul><ul><li>An integer value may be stored in an integer variable declared with the keyword int </li></ul><ul><li>You can also declare an integer variable using short int and long int </li></ul>1
- 14. Working with Variables <ul><li>Real or floating-point numbers are numbers that include decimal positions, such as 98.6, 1000.00002, and -3.85 </li></ul><ul><li>They may be stored in variables with type float, double , and long double </li></ul><ul><li>Characters may be stored in variables declared with the keyword char </li></ul><ul><li>A character may hold any single symbol in the ASCII character set </li></ul><ul><li>Often it contains a letter of the alphabet, but it could include a space, digit, punctuation mark, arithmetic symbol, or other special symbol </li></ul>1
- 15. Working with Variables <ul><li>In C++, a character value is always expressed in single quotes, such as ‘A’ or ‘&’ </li></ul><ul><li>To declare a variable, you list its type and its name </li></ul><ul><li>In addition, a variable declaration is a C++ statement, so it must end with a semicolon </li></ul><ul><li>If you write a function that contains variables of diverse types, each variable must be declared in a statement of its own </li></ul><ul><li>If you want to declare two or more variables of the same type, you may declare them in the same statement </li></ul>
- 16. Working with Variables <ul><li>Explicitly stating the value of a variable is called assignment , and is achieved with the assignment operator = </li></ul><ul><li>The variable finalScore is declared and assigned a value at the same time </li></ul><ul><li>Assigning a value to a variable upon creation is often referred to as initializing the variable </li></ul>1
- 17. The const Qualifier <ul><li>A variable that does not change in a program should not be declared as a variable </li></ul><ul><li>Instead, it should be a constant </li></ul><ul><li>The statement const double MINIMUM_WAGE = 5.75; declares a constant named MINIMUM_WAGE that can be used like a variable, but cannot be changed during a program </li></ul>1
- 18. Creating Comments <ul><li>Comments are statements that do not affect the compiling or running of a program </li></ul><ul><li>Comments are simply explanatory remarks that the programmer includes in a program to clarify what is taking place </li></ul><ul><li>These remarks are useful to later program users because they might help explain the intent of a particular statement or the purpose of the entire program </li></ul><ul><li>C++ supports both line comments and block comments </li></ul>1
- 19. Creating Comments <ul><li>A line comment begins with two slashes (//) and continues to the end of the line on which it is placed </li></ul><ul><li>A block comment begins with a single slash and an asterisk (/*) and ends with an asterisk and a slash (*/); it might be contained on a single line or continued across many lines </li></ul>1
- 20. Using Libraries and Preprocessor Directives <ul><li>Header files are files that contain predefined values and routines, such as sqrt( ) </li></ul><ul><li>Their filenames usually end in .h </li></ul><ul><li>In order for your C++ program to use these predefined routines, you must include a preprocessor directive , a statement that tells the compiler what to do before compiling the program </li></ul><ul><li>In C++, all preprocessor directives begin with a pound sign (#), which is also called an octothorp </li></ul><ul><li>The #include preprocessor directive tells the compiler to include a file as part of the finished product </li></ul>1
- 21. C++ Binary Arithmetic Operators <ul><li>Often after data values are input, you perform calculations with them </li></ul><ul><li>C++ provides five simple arithmetic operators for creating arithmetic expressions: </li></ul><ul><ul><li>addition (+) – subtraction (-) </li></ul></ul><ul><ul><li>multiplication (*) – division (/) </li></ul></ul><ul><ul><li>modulus (%) </li></ul></ul><ul><li>Each of these arithmetic operators is a binary operator; each takes two operands, one on each side of the operator, as in 12 + 9 or 16.2*1.5 </li></ul><ul><li>The results of an arithmetic operation can be stored in memory </li></ul>2
- 22. C++ Binary Arithmetic Operators 2
- 23. C++ Binary Arithmetic Operators <ul><li>In Figure 2-2, each operation is assigned to a result variable of the correct type </li></ul><ul><li>The expression a + b has an integer result because both a and b are integers, not because their sum is stored in the intResult variable </li></ul><ul><li>If the program contained the statement doubleResult = a+b; the expression a+b would still have an integer value, but the value would be cast , or transformed, into a double when the sum is assigned to doubleResult </li></ul>2
- 24. C++ Binary Arithmetic Operators <ul><li>The automatic cast that occurs when you assign a value of one type to another is called an implicit cast </li></ul><ul><li>The modulus operator (%), which gives the remainder of integer division, can be used only with integers </li></ul><ul><li>When more than one arithmetic operator is included in an expression, then multiplication, division, and modulus operations always occur before addition or subtraction </li></ul><ul><li>Multiplication, division, and modulus are said to have higher precedence </li></ul>2
- 25. Shortcut Arithmetic Operators <ul><li>C++ employs several shortcut operators </li></ul><ul><li>When you add two variable values and store the result in a third variable, the expression takes the form result= firstValue + secondValue </li></ul><ul><li>When you use an expression like this, both firstValue and secondValue retain their original values; only the result is altered </li></ul><ul><li>When you want to increase a value, the expression takes the form firstValue = firstValue + secondValue </li></ul>2
- 26. Shortcut Arithmetic Operators <ul><li>C++ provides the -= operator for subtracting one value from another, the *= operator for multiplying one value by another, and the /= operator for dividing one value by another </li></ul><ul><li>As with the += operator, you must not insert a space within the subtraction, multiplication, or division shortcut operators </li></ul><ul><li>The options shown in Figure 2-4 means replace the current value of count with the value that is 1 more than count, or simply increment count </li></ul>2
- 27. Shortcut Arithmetic Operators <ul><li>As you might expect, you can use two minus signs (--) before or after a variable to decrement it </li></ul>2
- 28. Shortcut Arithmetic Operators <ul><li>The prefix and postfix increment and decrement operators are examples of unary operators </li></ul><ul><li>Unary operators are those that require only one operand, such as num in the expression ++num </li></ul><ul><li>When an expression includes a prefix operator, the mathematical operation takes place before the expression is evaluated </li></ul><ul><li>When an expression includes a postfix operator, the mathematical operation takes place after the expression is evaluated </li></ul>2
- 29. Shortcut Arithmetic Operators <ul><li>The difference between the results produced by the prefix and postfix operators can be subtle, but the outcome of a program can vary greatly depending on which increment operator you use in an expression </li></ul>2
- 30. Evaluating Boolean Expressions <ul><li>A boolean expression is one that evaluates as true or false </li></ul><ul><li>All false relational expressions are evaluated as 0 </li></ul><ul><li>Thus, an expression such as 2>9 has the value 0 </li></ul><ul><li>You can prove that 2>9 is evaluated as 0 by entering the statement code <<(2>9); into a C++ program </li></ul><ul><li>A 0 appears on output </li></ul><ul><li>All true relational expressions are evaluated as 1 </li></ul><ul><li>Thus, the expression 9>2 has the value 1 </li></ul>2
- 31. Evaluating Boolean Expressions <ul><li>The unary operator ! Means not, and essentially reverses the true/false value of an expression </li></ul>2
- 32. Selection <ul><li>Computer programs seem smart because of their ability to use selections or make decisions </li></ul><ul><li>C++ lets you perform selections in a number of ways: </li></ul><ul><ul><li>The if statement </li></ul></ul><ul><ul><li>The switch statement </li></ul></ul><ul><ul><li>The if operator </li></ul></ul><ul><ul><li>Logical AND and Logical OR </li></ul></ul>2
- 33. Some Sample Selection Statements within a C++ Program 2
- 34. The if Statement <ul><li>If the execution of more than one statement depends on the selection, then the statements must be blocked with curly braces as shown in the code segment in Figure 2-8 </li></ul>2
- 35. The if Statement 2
- 36. Multiple Executable Statement in an if-else 2
- 37. The if Statement <ul><li>Any C++ expression can be evaluated as part of an if statement </li></ul>2
- 38. The switch Statement <ul><li>When you want to create different outcomes depending on specific values of a variable, you can use a series of ifs shown in the program statement in Figure 2-14 </li></ul><ul><li>As an alternative to the long string of ifs shown in Figure 2-14, you can use the switch statement </li></ul><ul><li>The switch can contain any number of cases in any order </li></ul>2
- 39. The if Operator <ul><li>Another alternative to the if statement involves the if operator (also called the conditional operator ), which is represented by a question mark (?) </li></ul><ul><li>E.g. </li></ul><ul><li>cout<<(driveAge<26)?”The driver is under 26”:”The driver is at least 26”; </li></ul><ul><li>The if operator provides a concise way to express two alternatives </li></ul><ul><li>The conditional operator is an example of a ternary operator, one that takes three operands instead of just one or two </li></ul>2
- 40. Logical AND and Logical OR <ul><li>In some programming situations, two or more conditions must be true to initiate an action </li></ul><ul><li>Figure 2-16 works correctly using a nested if —that is, one if statement within another if statement </li></ul><ul><li>If numVisits is not greater than 5, the statement is finished—the second comparison does not even take place </li></ul><ul><li>Alternatively, a logical AND (&&) can be used, as shown in Figure 2-17 </li></ul>2
- 41. Logical AND and Logical OR 2
- 42. Logical AND and Logical OR <ul><li>A logical AND is a compound boolean expression in which two conditions must be true for the entire expression to evaluate as true </li></ul><ul><li>Table 2-3 shows how an expression using && is evaluated </li></ul><ul><li>An entire expression is true only when the expression on each side of the && is true </li></ul>2
- 43. Using the Logical OR <ul><li>In certain programming situations, only one of two alternatives must be true for some action to take place </li></ul><ul><li>A logical OR (||) could also be used </li></ul><ul><li>A logical OR is a compound boolean expression in which either of two conditions must be true for the entire expression to evaluate as true </li></ul><ul><li>Table 2-4 shows how C++ evaluates any expression that uses the || operator </li></ul>2
- 44. Using the Logical OR 2
- 45. Using the Logical OR <ul><li>When either expression1 or expression2 is true (or both are true), the entire expression is true </li></ul><ul><li>On pages 53 and 54 of the textbook, perform the steps so you can write a program that makes several decisions </li></ul>2
- 46. A Typical Run of the Decisions.cpp Program 2
- 47. The while Loop <ul><li>Loops provide a mechanism with which to perform statements repeatedly and, just as important, to stop that performance when warranted </li></ul><ul><ul><li>while (boolean expression) </li></ul></ul><ul><ul><li>statement; </li></ul></ul><ul><li>In C++, the while statement can be used to loop </li></ul><ul><li>The variable count, shown in the program in Figure 2-21, is often called a loop-control variable , because it is the value of count that controls whether the loop body continues to execute </li></ul>2
- 48. The while Loop 2
- 49. The while Loop 2
- 50. The for Statement <ul><li>The for statement represents an alternative to the while statement </li></ul><ul><li>It is most often used in a definite loop , or a loop that must execute a definite number of times </li></ul><ul><li>It takes the form: </li></ul><ul><ul><li>for ( initialize; evaluate; alter) </li></ul></ul><ul><ul><li>statement ; </li></ul></ul>2

No public clipboards found for this slide

east or west slide share is d bst