0% found this document useful (0 votes)
21 views29 pages

3 1

Uploaded by

Nabeel Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views29 pages

3 1

Uploaded by

Nabeel Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Sample C++ program

Can you find 2 examples of


comments in the program?
// This is a C++ Program
#include <iostream>
#include <conio.h>
using namespace std;
void main() Every C++ program has a function
{ called main
/* The output of this program is
just a stupid string
*/
cout << “Welcome to C++." << endl;
getch();
}

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2


Special Symbols

• Special symbols

+ ?
- ,
* <=
/ !=
. ==
; >=

3
Reserved Words (Keywords)

• Reserved words, keywords, or word symbols


− Include:
• int
• float
• double
• char
• const
• void
• return

4
Problem with integer datatype

• Till now, we studied only integer datatype.


• Lets try to store value of 14.5 into a variable
of integer datatype.
• Lets try to store some character such as ‘d’ to
an integer variable.
• Conclusion: We need more datatypes.

5
Data Types

• Data type: set of values together with a set of


operations
• C++ data types fall into three categories:

6
Simple Data Types

• Three categories of simple data


− Integral: integers (numbers without a decimal)
− Floating-point: decimal numbers
− Enumeration type: user-defined data type

7
Simple Data Types (continued)

• Integral data types are further classified into


eight categories:

8
Simple Data Types (continued)

• Different compilers may allow different ranges


of values

9
int Data Type

• Examples:
-6728
0
78
+763
• Positive integers do not need a + sign
• No commas are used within an integer
− Commas are used for separating items in a list

10
bool Data Type

• bool type
− Two values: true and false
− Manipulate logical (Boolean) expressions
• true and false are called logical values
• bool, true, and false are reserved words

11
char Data Type

• The smallest integral data type


• Used for characters: letters, digits, and special
symbols
• Each character is enclosed in single quotes
− 'A', 'a', '0', '*', '+', '$', '&'
• A blank space is a character and is written ' ',
with a space left between the single quotes

12
Floating-Point Data Types

− float: represents any real number


• Range: -3.4E+38 to 3.4E+38 (four bytes)
− double: represents any real number
• Range: -1.7E+308 to 1.7E+308 (eight bytes)
− On most newer compilers, data types double
and long double are same
13
Floating-Point Data Types
(continued)
• Maximum number of significant digits
(decimal places) for float values is 6 or 7
• Maximum number of significant digits for
double is 15
• Precision: maximum number of significant
digits
− Float values are called single precision
− Double values are called double precision

14
Arithmetic Operators
• C++ arithmetic operators:
− + addition
− - subtraction
− * multiplication
− / division
− % modulus operator
• +, -, *, and / can be used with integral and floating-
point data types (and they sometimes behave
differently with different types!)
• Operators can be unary (one operand) or binary (two
operands)

15
 #include <iostream>
 What is the output?
using namespace std;

 void main()
 {
 cout << "-2 + 5 = " << -2 + 5 << endl;
 cout << "10 - 20 = " << 10 - 20 << endl;
 cout << "2 * 7 = " << 2 * 7 << endl;
 cout << "5 / 2 = " << 5 / 2 << endl;
 cout << "5.0 / 2 = " << 5.0 / 2 << endl;
 cout << "5 / 2.0 = " << 5 / 2.0 << endl;
 cout << "34 % 5 = " << 34 % 5 << endl;
 cout << "4 % 6 = " << 4 % 6 << endl;

 getch();
 }
Operator, operands and operations

 Every arithmetic operation is performed using operator and operands.


 In num1 + num2, num1 and num2 are operands whereas + is operator.
Order of Precedence

• All operations inside of () are evaluated first


• *, /, and % are at the same level of
precedence and are evaluated next
• + and – have the same level of precedence
and are evaluated last
• When operators are on the same level
− Performed from left to right (associativity)
• 3 * 7 - 6 + 2 * 5 / 4 + 6 means
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6

18
Expressions

• If all operands are integers


− Expression is called an integral expression
• Yields an integral result
• Example: 2 + 3 * 5
• If all operands are floating-point
− Expression is called a floating-point
expression
• Yields a floating-point result
• Example: 12.8 * 17.5 - 34.50

20
Mixed Expressions

• Mixed expression:
− Has operands of different data types
− Contains integers and floating-point
• Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2

21
Mixed Expressions (continued)

• Evaluation rules:
− If operator has same types of operands
• Evaluated according to the type of the operands
− If operator has both types of operands
• Integer is changed to floating-point
• Operator is evaluated
• Result is floating-point
− Entire expression is evaluated according to
precedence rules

22
What is the output?
 #include <iostream>
 #include<conio.h>
 using namespace std;

 void main()
 {
 cout << "3 / 2 + 5.5 = " << 3 / 2 + 5.5 << endl;
 cout << "10.6 / 2 + 5 = " << 10.6 / 2 + 5 <<
endl;
 cout << "4 + 5 / 2.0 = " << 4 + 5 / 2.0 << endl;
 cout << "4 * 3 + 7 / 5 - 12.5 = "
 << 4 * 3 + 7 / 5 - 12.5
 << endl;

 getch();
 }
Identifiers

• Consist of letters, digits, and the underscore


character (_)
• Must begin with a letter or underscore
• C++ is case sensitive
− NUMBER is not the same as number
• Two predefined identifiers are cout and cin

24
Identifiers (continued)

• The following are legal identifiers in C++:


− first
− conversion
− payRate

25
Prompt Lines

• Part of good documentation is the use of


clearly written prompts (so that the user
knows what to do)
• Prompt lines: executable statements that
inform the user what to do
cout << "Please enter a number between 1 and 10 and "
<< "press the return key" << endl;
cin >> num;
Documentation

• A well-documented program is easier to


understand and modify
• You use comments to document programs
• Comments should appear in a program to:
− Explain the purpose of the program
− Identify who wrote it
− Explain the purpose of particular statements
Naming Identifiers

• Identifiers can be self-documenting:


− CENTIMETERS_PER_INCH
• Avoid run-together words :
− annualsale
− Solution:
• Capitalize the beginning of each new word
• annualSale
• Inserting an underscore just before a new word
• annual_sale
Form and Style

• Consider two ways of declaring variables:


− Method 1
int feet, inch;
double x, y;
− Method 2
int a,b;double x,y;
• Both are correct; however, the second is hard
to read

You might also like