Intro to Computing Concepts - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

Intro to Computing Concepts

Description:

cout 'Computer games' endl; cout 'Coffee' endl; cout 'Aspirin' endl; return 0; ... Multiplication. Division. Modulus. Type. Binary. Binary. Binary. Binary ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 55
Provided by: markfo7
Category:

less

Transcript and Presenter's Notes

Title: Intro to Computing Concepts


1
Intro to Computing Concepts
  • Lecture 4
  • Mark Fontenot

2
Objectives
  • Escape sequences
  • Variables
  • Data types
  • Characters
  • Operators

3
Simple C Program
  • //A simple C Program
  • include ltiostreamgt
  • using namespace std
  • int main()
  • coutltltProgramming is great fun!
  • return 0

Programming is great fun!
4
Simple C Program
  • //A simple C Program
  • include ltiostreamgt
  • using namespace std
  • int main()
  • coutltltProgramming is great fun!
  • return 0

5
Special 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

8
An 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
9
Stream 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
10
An 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
11
Example 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

12
Example 2 Output
  • Following items were top sellers
  • during the month of June
  • Computer games
  • Coffee
  • Aspirin

13
Example 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

14
Example 3 Output
  • Following items were top sellers
  • during the month of June
  • Computer games
  • Coffee
  • Aspirin

Notice its the same as Example 2s output!!!
15
Escape Sequences
  • Allow you to control the way output is displayed
  • All start with a \ (backslash)
  • \ is followed by some control characters

16
Escape 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
17
Escape 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
18
Escape 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
19
Exercise
  • Write a C program that will cause the following
    screen output

He said, Go home!
20
Variables
A variable represents a storage location in
computer memory. Variables are used to store
data in programs
Example Variable Definition int number
21
Variable Example
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int number
  • number 5
  • coutltltThe value in number is
  • ltlt number ltltendl
  • return 0

Variable Declaration
22
Variable Example
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int number
  • number 5
  • coutltltThe value in number is
  • ltlt number ltltendl
  • return 0

Assignment Statement
23
Variable 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
24
Naming 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

25
Naming 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)

26
Check?
Indicate with yes or no whether the following
are proper variables names
aBc193 ______ _tempLocal ______ 39game
______ 2ndHalf ______ Go_Away
______ main ______
27
Data Types for Variables
  • Variables can hold different types of data

Data Types
Character
Number
Decimal
Floating Point
char
int
double
float
short
28
Example
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
29
Example
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
30
Multiple 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
31
Example 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

32
Assignment Statements
number 5
Where the l-value is stored
The r-value
Assignments are always done in this order.
33
The 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

34
The 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
35
Lets 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
36
Lets 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
  • Math in C

38
Arithmetic Operators
Meaning
Operator
Type
Addition

Binary
Subtraction
-
Binary
Multiplication

Binary
Division
/
Binary
Modulus

Binary
39
Some 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
40
Basic 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
41
more 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
42
Write the assignment Stmt
  • Adds 2 to A and stores the result in B

B A 2
43
Write the assignment Stmt
  • multiplies B times 4 and stores the result in A

A B 4
44
Write the assignment Stmt
  • Divides A by 3.14 and stores the result in B

B A / 3.14
45
Write the assignment Stmt
  • Subtracts 8 from B and stores result in A

A B - 8
46
Write the assignment Stmt
  • Stores the value 27 in A

A 27
47
Determine the Output
  • includeltiostreamgt
  • using namespace std
  • int main()
  • int freeze32, boil212
  • freeze 0
  • boil 100
  • coutltltfreezeltltendlltltboilltltendl
  • return 0

0 100 _
48
Determine 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 _
49
Determine 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

50
Output
I am the incredible computing machine and i
will amaze you. _
51
Determine the Output
  • includeltiostreamgt
  • using namespace std
  • int main()
  • coutltltBe careful\n
  • coutltltThis might/n be a trick
  • coutltltquestion\n
  • return 0

52
Output
Be careful This might/n be a trick question _
53
Determine the Output
  • includeltiostreamgt
  • using namespace std
  • int main()
  • int a, x 23
  • a x 2
  • coutltltxltltendlltltaltltendl
  • return 0

23 1 _
54
Questions?
  • ?
Write a Comment
User Comments (0)
About PowerShow.com