Title: Intro to Computing Concepts
1Intro to Computing Concepts
2Objectives
- Escape sequences
- Variables
- Data types
- Characters
- Operators
3Simple C Program
- //A simple C Program
- include ltiostreamgt
- using namespace std
- int main()
-
- coutltltProgramming is great fun!
- return 0
Programming is great fun!
4Simple C Program
- //A simple C Program
- include ltiostreamgt
- using namespace std
- int main()
-
- coutltltProgramming is great fun!
- return 0
5Special Characters in C
- // (double slash)
- marks the beginning of a comment
- (pound sign)
- marks the beginning of a preprocessor directive
- ltgt (open/close angle brackets)
- Enclose a filename when used with a include
directive
6(more) Special Characters in C
- (open/close braces)
- Encloses a group of statements such as the
contents of a function - ( ) (open/close parentheses)
- Used in naming a function as inint main()
7(yet more) Speical Characters in C
- (open/close quotation marks)
- Encloses a string of characters
- Good Example is a message that is to be printed
to the screen - (semicolon)
- Marks the end of a complete programming statement
8An example
- //A different simple c program
- includeltiostreamgt
- using namespace std
- int main()
-
- cout ltlt Programming is
- cout ltlt great fun!
- return 0
Programming is _
great fun!
Screen
9Stream Manipulator
endl
- Use in cout statements
- Causes output to continue on the next line on the
screen
cout ltlt Hello ltlt endl ltlt World! cout ltlt
Life is good! ltlt endl
10An example
- //Demo of endl
- includeltiostreamgt
- using namespace std
- int main()
-
- cout ltlt Programming is ltlt endl
- cout ltlt great fun!
- return 0
Programming is
great fun!
Screen
11Example 2
- includeltiostreamgt
- using namespace std
- int main()
-
- coutltltFollowing items were top
- ltltsellers ltlt endl
- coutltltduring the month of Juneltltendl
- coutltltComputer gamesltltendl
- coutltltCoffeeltltendl
- coutltltAspirinltltendl
- return 0
12Example 2 Output
- Following items were top sellers
- during the month of June
- Computer games
- Coffee
- Aspirin
13Example 3
- includeltiostreamgt
- using namespace std
- int main()
-
- coutltltFollowing items were top
- ltltsellers\n
- coutltltduring the month of June\n
- coutltltComputer games\nCoffee
- coutltlt\nAspirin\nltltendl
- return 0
14Example 3 Output
- Following items were top sellers
- during the month of June
- Computer games
- Coffee
- Aspirin
Notice its the same as Example 2s output!!!
15Escape Sequences
- Allow you to control the way output is displayed
- All start with a \ (backslash)
- \ is followed by some control characters
16Escape Sequences
Escape Sequence
Description
Moves the cursor to the next line for subsequent
printing
\n Newline
Causes the cursor to skip over to the next tab
stop
\t Horizontal tab
Causes the computer to beep
\a Alarm
17Escape Sequences
Escape Sequence
Description
\\ Backslash
Causes a backslash to be printed
\ Single quote
Causes a single quotation mark to be printed
\ Double quote
Causes a double quotation mark to be printed
18Escape Sequences
Escape Sequence
Description
\r Return
Causes the cursor to go to the beginning of the
current line, not the next line.
\b Backspace
Causes the cursor to backup, or move left one
position
19Exercise
- Write a C program that will cause the following
screen output
He said, Go home!
20Variables
A variable represents a storage location in
computer memory. Variables are used to store
data in programs
Example Variable Definition int number
21Variable Example
- include ltiostreamgt
- using namespace std
- int main()
-
- int number
- number 5
- coutltltThe value in number is
- ltlt number ltltendl
- return 0
Variable Declaration
22Variable Example
- include ltiostreamgt
- using namespace std
- int main()
-
- int number
- number 5
- coutltltThe value in number is
- ltlt number ltltendl
- return 0
Assignment Statement
23Variable Example
- include ltiostreamgt
- using namespace std
- int main()
-
- int number
- number 5
- coutltltThe value in number is
- ltlt number ltltendl
- return 0
Retrieves the value and displays to the screen
24Naming Rules for Variables
- First character must be one of the letters a-z,
A-Z or an underscore(_) - After first letter, use a-z, A-Z, 0-9, or
underscore (_) - Keep length less than 31 characters
25Naming Rules for Variables
- Case sensitive
- num and Num arent the same variable name
- Cannot declare two variables of the same name in
the same program - Cannot give a variable the same name as C
keyword or previously reserved word (such as cout)
26Check?
Indicate with yes or no whether the following
are proper variables names
aBc193 ______ _tempLocal ______ 39game
______ 2ndHalf ______ Go_Away
______ main ______
27Data Types for Variables
- Variables can hold different types of data
Data Types
Character
Number
Decimal
Floating Point
char
int
double
float
short
28Example
include ltiostreamgt using namespace std int
main() int number number 5
coutltltThe value in number is ltlt number ltlt
endl return 0
?
5
Number
The value in number is 5 _
Screen
29Example
include ltiostreamgt using namespace std int
main() int number number 5
coutltltThe value in number is ltlt number ltlt
endl return 0
?
5
Number
The value in number is number _
Screen
30Multiple Assignments
include ltiostreamgt using namespace std int
main() int checking unsigned miles long
days checking -20 miles 4276 days
184086
?
-20
checking
?
4276
miles
?
184086
days
31Example Continued
- coutltlt We have made a long trip of
- ltlt miles ltlt miles.\n
- coutltlt Our checking account balance is
- ltlt checking
- coutltlt\nAbout ltlt days ltlt days
- ltlt ago Columbus stood
- ltlt on this spot.\n
- return 0
32Assignment Statements
number 5
Where the l-value is stored
The r-value
Assignments are always done in this order.
33The char Data Type
- Primarily for storing one single character
- Strictly speaking, its an integer
- Each character (127 different ones) is
represented by an integer, but when displayed, a
character appears. - We use the ASCII encoding scheme
34The char versus the int
How its stored in memory
The character
A
65
B
66
Called the ASCII Value
c
99
\n
9
See App. A in text book
35Lets Talk Characters and Integers
include ltiostreamgt using namespace std int
main() char letter letter A
coutltltletterltltendl letter C
coutltltletterltltendl return 0
65
67
letter
A _
C
36Lets Talk Characters and Integers
include ltiostreamgt using namespace std int
main() char letter letter 65
coutltltletterltltendl letter 67
coutltltletterltltendl return 0
65
67
letter
A _
C
37 38Arithmetic Operators
Meaning
Operator
Type
Addition
Binary
Subtraction
-
Binary
Multiplication
Binary
Division
/
Binary
Modulus
Binary
39Some Arithmetic Examples
Algebraic Exp.
C Expression
f 7
f 7
p - c
p - c
bx
b x
x / y
x/y or x y
r mod s
r s
40Basic Operator Example
?.?
- includeltiostreamgt
- using namespace std
- int main()
-
- float regWages
- float basePay18.25
- float regHours40.0
- float otWages
- float otPay27.78
- float otHours10
- float totalWages
regWages
18.25
basePay
40.0
regHours
?.?
otWages
27.78
otPay
10
otHours
?.?
totalWages
41more operator usage
?.?
730.0
regWages
18.25
basePay
- regWagesbasePayregHours
- otWagesotPayotHours
- totalWagesregWagesotWages
- coutltltWages for this week
- ltltare ltlttotalWages
- return 0
40.0
regHours
?.?
277.8
otWages
27.78
otPay
10
otHours
?.?
1007.8
totalWages
42Write the assignment Stmt
- Adds 2 to A and stores the result in B
B A 2
43Write the assignment Stmt
- multiplies B times 4 and stores the result in A
A B 4
44Write the assignment Stmt
- Divides A by 3.14 and stores the result in B
B A / 3.14
45Write the assignment Stmt
- Subtracts 8 from B and stores result in A
A B - 8
46Write the assignment Stmt
A 27
47Determine the Output
- includeltiostreamgt
- using namespace std
- int main()
-
- int freeze32, boil212
- freeze 0
- boil 100
- coutltltfreezeltltendlltltboilltltendl
- return 0
0 100 _
48Determine the Output
- includeltiostreamgt
- using namespace std
- int main()
-
- int x0, y2
- x y 4
- cout ltlt x ltlt endl ltlt y ltlt endl
- return 0
8 2 _
49Determine the Output
- includeltiostreamgt
- using namespace std
- int main()
-
- coutltltI am the incredible
- coutltlt computing\nmachine
- coutltlt\nand i will\namaze\n
- coutltltyou. ltlt endl
- return 0
50Output
I am the incredible computing machine and i
will amaze you. _
51Determine the Output
- includeltiostreamgt
- using namespace std
- int main()
-
- coutltltBe careful\n
- coutltltThis might/n be a trick
- coutltltquestion\n
- return 0
52Output
Be careful This might/n be a trick question _
53Determine the Output
- includeltiostreamgt
- using namespace std
- int main()
-
- int a, x 23
- a x 2
- coutltltxltltendlltltaltltendl
- return 0
23 1 _
54Questions?