Lesson2 - Intro To C
Lesson2 - Intro To C
Introduction
to C++
Introduction to C++
return 0;
} Program returns a status
code (0 means OK)
Comments
is used to self-document a program.
- //
- /* */
Comment Example:
// Dave's Homework #1
// This program is awesome!
#include <iostream.h>
/* This program computes the
coefficient of expansion of the
universe to 27 decimal places.
*/
int main() {
cout << 1.0000000000000000000001;
}
#include<iostream.h>
Lines beginning with a pound sign(#) are directives for the
preprocessor
iomanip.h
stdlib.h
time.h
The input/output
statements
cout<<Hello World;
- Is a C++ statement.
<<
- Called the insertion operator.
>>
- Called the extraction operator.
. ; ,
<= != == >=
Word Symbols
(Reserved Words/Keywords)
Keywords
Cannot be redefined within any program; that is, they cannot
be used for anything other than their intended use.
Identifiers
- are names of things that appear in programs, such as
variables, constants, and functions.
Example:
int value;
Variable initialization:
value = 5;
22 3.14159
false 'c'
Arithmetic Operators
+ addition
- subtraction or negation
* multiplication
/ division
Binary operator:
An operator that has two operands.
Example: Arithmetic Expression
Expressions
Some examples:
1+2
(fahr - 32)*(5/9)
1*(2*(3*(4*5)))
C++ Math Operator Rules
Operator Associativity Precedence
() left to right high
* / % left to right middle
+ - left to right low
1. 5 + 2 *4
2. 10 / 2 3
3. 8 + 12 * 2 4
4. 4 + 17 % 2 1
5. 63*2+7-1
Grouping with Parentheses
average = (a + b + c + d) / 4;
Example :
1. (5 + 2) * 4
2. 10 / (5 - 3)
3. 8 + 12 * (6 - 2)
4. (4 + 17) % 2 1
5. (6 - 3) * (2 + 7) / 3
Converting Algebraic Expressions to
Programming Statements
Algebraic Expressions Operation C++ Equivalent
6B 6 times B 6*B
(3)(12) 3 times 12 3 * 12
Z = 3BC + 4
A = 3X + 2
4A - 1
No Exponents Please!
Example:
Area = pow (4,2);
Math Operator Quiz
What are the values printed?
Equality Operators:
== Equal to
!= Not Equal to
Common Escape Sequences
\n - newline
\t - horizontal tab
\a - alarm
\b - backspace
\r - return
\\ - backslash
\ - single quote
\ - double quote
Seatwork1:
10. How may the int variables months, day, and years be
declared in one statement, with months initialized to 2 and
years initialized to 3?
Data types
A set of values together with a set of operations.
- Integer
- only hold whole numbers
- Character
- primarily for storing characters
- Floating Point
- used to declare variables that can hold real numbers.
- Bool
- set to either true or false
Categories of Floating-Point data types:
1. float
2. double
3. long double
a. 3.287 x 106
b. 978.65 x 1012
-3
c. 7.65491 x 10
-4
d. -58710.23 x 10
The char Data type
When character is stored in memory, it is actually the
numeric code that is stored.
#include<iostream>
using namespace std;
int main()
{
char letter;
letter = 65;
cout<<letter<<endl;
letter = 66;
cout<<letter<<endl;
return 0;
}
The C++ string Class
Two ways of storing and working with strings
Note
some pre-standard compilers do not support them
What is the string class?
An abstract data type
A programmer defined that accompanies the C++ language.
return 0;
}
Programming Style
C++ is a free-format language, which means that:
Extra blanks (spaces) or tabs before or after
identifiers/operators are ignored.
Blank lines are ignored by the compiler just like comments.
Code can be indented in any way.
There can be more than one statement on a single line.
A single statement can continue over several lines.
Programming Style (cont. )
In order to improve the readability of your program, use the following
conventions:
Start the program with a header that tells what the program does.
Use meaningful variable names.
Document each variable declaration with a comment telling what the
variable is used for.
Place each executable statement on a single line.
A segment of code is a sequence of executable statements that belong
together.
Use blank lines to separate different segments of code.
Document each segment of code with a comment telling what the
segment does.
What makes a bad program?
#include<iostream.h>
int main()
{
critter = 62.7;
float critter;
cout<<critter<<endl;
return 0;
}
5. Every complete statement ends with a _______________.
6. Preprocessor directives begin with a _________.
7. The following data
after Frank says 2, Peter has to keep this number in his mind.
after Frank says 5, Peter also needs to keep this number in his mind.