Week 3 - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Week 3

Description:

The operating system (XP) 'Loads' the program. Think of it as XP calling the 'main' ... Your programs will usually entail. Reserving space to store information ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 48
Provided by: Christ348
Category:
Tags: entail | week

less

Transcript and Presenter's Notes

Title: Week 3


1
Week 3
  • Get some information in,
  • Do something with it,
  • Display the result.

2
Learning Points
  • Structure of a simple C Program
  • TYPES of information
  • Simple and Complex
  • Variables, Constants and Literals
  • Identifiers to Name memory
  • Declarations
  • Getting information into your program basics
  • Operator basics

3
Shortest C Program.A Skeleton program
type of returned value
name of function
int main ( ) return 0 //Write and
run this Chris
4
What happens?
  • The operating system (XP) Loads the program
  • Think of it as XP calling the main function.
  • Instructions in main are carried out one at a
    time.
  • Notice the int in front of main this line is
    like a contract to the operating system
  • I will send you a piece of information in the
    form of an integer.
  • Hence the NEED for a return instruction. return 0
    sends a zero to windows XP

5
Discussion of structure
  • Your program must have a function called main
  • Note that Main, MAIN, mAiN will cause an error
  • The round brackets ( and )after main tells
    the computer that main is a function rather than
    a variable
  • The curly brackets and mark the beginning
    and end of instructions that form the body of
    main.
  • The semi-colon after return 0 is an
    instruction terminator or separator.

6
The Hello World Program
accesses standard toolbox, i.e. we are using
the display (cout) tool
include ltiostreamgt using namespace std int
main ( ) cout ltlt Hello World ltlt endl
return 0 //Write and run this Chris
Instructions that tell computer that I want to
use iostream toolbox and access the standard
(std) tools for input and output
7
Discussion of Hello World Program
  • There are now two instructions in main
  • we added a display information instruction using
    a tool cout
  • cout represents the display screen. It hides all
    the complicated business of displaying things
    from you. All you need to know is how to use
    cout.
  • cout receives information using the ltlt send to
    operator (insertion operator).
  • So we literally send the string Hello world to
    the display!
  • endl (last letter is lowercase L not number 1!)
    is also sent to the display, this is interpreted
    by cout as a new-line command.

8
Discussion of Hello World
  • cout is an OBJECT that represents our screen.
  • Screens are hardware.
  • Sending information to screens varies greatly
    from machine to machine.
  • So cout became standard. Customised couts were
    created for all the major types of computer in
    the world.
  • So that you can write one piece of code that
    should compile on all the main types of computer
    in the world.

9
Discussion of Hello World
  • Because cout NOT BUILT IN we need to tell the
    computer that we want to make use of the
    standard facilities for input and output
  • HENCE the lines
  • include ltiostreamgt
  • using namespace standard

This is one of the original reasons for C
popularity its relative ease of porting from one
type of computer to another
10
Some Definitions
  • Statements
  • A statement is what programmers often call an
    instruction.
  • Your code consists of many instructions/statements
  • Statements end with a semi-colon
  • blocks
  • Any section of code which is surrounded by curly
    brackets is a block

11
Example A Block of 4 statements
  • cout ltlt"A fraction "ltlt5.0/8.0 ltltendl
  • cout ltlt"Big "ltlt 7000.07000.0ltltendl
  • cout ltlt8 5 ltlt" is the sum of 8 5\n"
  • cout ltlt Hello world

Block denoted by Curly braces
12
TYPES of information computers use
  • Simple (needs little or no effort to use)
  • To hold whole numbers (integers)
  • To hold numbers with fractions (float and double)
  • To hold individual characters (char)
  • Complex (needs a little more work to use)
  • Strings
  • cin and cout

13
Simple Information
int
  • Integer (Whole numbers)
  • For counting things
  • To represent values which only have whole numbers
  • Pounds, Pence?
  • Grades, Scores (Chelsea 0 Penge United 10)
  • Categories (days of week codes as numbers 1-7)

14
Simple Information
float
double
  • Floating points numbers
  • For representing numbers that may contain
    fractions
  • Averages, measurements, money, pi

15
Simple information
char
  • Single characters
  • Can represents your initials
  • Can represent single key responses to questions
  • y for yes and n for no
  • Not much else

16
What about Strings
include ltstringgt
string
  • Some languages this is a simple piece of
    information
  • In C it is not. A string is complex in that it
    is made up of lots of chars.
  • In C we use the standard string tools

17
Recap!
  • Your programs will usually entail
  • Reserving space to store information
  • Getting information into the space you reserved
  • This information will either be whole numbers,
    floating point numbers, single characters or
    strings.
  • More complex information we will cover at a later
    date.
  • Doing something (operate on) with the information
    (int, float, double, char or string)
  • Displaying results

18
Constants, Variables and Literals
Fundamental building blocks of programs
  • Remember at school
  • Area of a circle is pr2
  • Circumference of a circle is 2 pr
  • p is a constant representing the number 3.142
  • r is a variable representing the radius of a
    circle
  • 2 literally represents itself, it is a literal

19
Key programming concept
  • Programs use, variables, constants and literals
    to store information needed to solve problems

20
Fundamental task isGetting information in to the
computer
  • Key ideas
  • We enter values of literals directly into code.
  • We instruct the computer to reserve space for a
    constant and we enter the value of the constant
    directly into code
  • We instruct the computer to reserve space for a
    variable. We have a choice on how we enter values
    of variables.
  • We can enter values directly in code
    (assignments)
  • Values can be set interactively with a user.
  • Values can be set interactively with a file on
    disk.

21
Using literals
  • char, string, integer, and double values are
    referred to by value not by a name.
  • We type these directly in to code

include ltiostreamgt using namespace std int
main() cout ltltThe sum of one plus three is
ltlt 1 3 ltlt endl return 0
string literal
2 integer literals
22
Char literals
include ltiostreamgt using namespace std int
main() cout ltltFirst letter of the alphabet is
ltlt A ltlt endl return 0
char literals in SINGLE QUOTES
23
floating point literals
  • floating point number can contain fractions.
  • floating point literals are double

include ltiostreamgt using namespace std int
main() cout ltltone point one plus three point
two is ltlt 1.1 3.2 ltlt endl return 0
2 double literals
24
Discussion
  • You can enter information directly into code
    using literals
  • This is obviously very limiting if a program
    wanted to reuse a value we need to keep typing in
    its value every time.
  • Also if a program makes use of a value many times
    it is hard work to change all the CORRECT
    references in a long program, especially is the
    number, say 10 refers to a count in one part of
    the program and the number 10 means a grade in
    another. How can we differentiate between them?
  • It can also be very confusing for another person
    to understand what the numbers mean.

25
SolutionVariables and Constants
  • These are NAMED areas of memory.
  • The programmer instructs the computer to reserve
    space for the kind of information he/she wants
    and at the same time tell the computer the name
    that will be used to refer to that bit of
    information.
  • This kind of instruction is called a DECLARATION

26
DECLARATION
  • We can declare
  • Integers
  • floating point numbers
  • chars
  • strings

27
Constants and Variable Declarations
  • When a variable is declared the computer marks
    the bit of memory reserves so that it allows its
    contents to change (vary) at any time.
  • When a constant is declared the computer
    effectively locks the memory reserved and
    prevents the contents being updated.

28
Creating Constants and Variables
include ltiostreamgt using namespace std int
main() const double Pi 3.142 double
radius double Area, Circumference Area
Piradiusradius Circumference
2Piradius return 0
Declarations are instructions to reserve
memory space big enough to store our
information. It also creates an identifier (a
name) for us to refer to this memory space
29
Rules for Creating Constants
const lttypegt ltidentifiergt ltvaluegt
lttypegt choose one of int char float double string
ltvaluegt You provide the constant value e.g. for
Pi value was 3.142
ltidentifiergt You create a name. It must obey the
rules for identifiers (see rules in a minute)
30
Examples
const int maxnumber 10 const double Pi
3.142 const char AGrade A const string
MyName Chris
31
Rules for Creating Variables
lttypegt ltidentifiergt or lttypegt ltidentifiergt
ltvaluegt or lttypegt ltidentifiergt, ltidentifiergt,

32
Examples
int number double Result char response string
UserName
int n1, n2, n3, n4
Declare a double variable and set its initial
value
double x 5.6
33
Rules for Creating Identifiers
  • An identifier must start with a letter or
    underscore, and be followed by zero or more
    letters
  • (A-Z, a-z), digits (0-9), or underscores
  • VALID
  • age_of_dog taxRateY2K
  • PrintHeading ageOfHorse
  • NOT VALID (Why?)
  • age 2000TaxRate Age-Of-Cat
  • Age of Cat

34
Meaningful Identifiers
Age-Of-Cat Age of Cat
ILLEGAL!!
Use underscore to link words or run words
together and capitalise the start of each word
LEGAL!!
Age_Of_Cat AgeOfCat
35
More About Identifiers
  • C is case sensitive so
  • NUMBER Number number
  • are all legitimate but DIFFERENT identifiers
  • BE CONSISTENT in your code!
  • It is NOT good practice to have long identifiers
  • Why?

36
Long Identifier Names
  • Some C compilers recognize only the first 32
    characters of an identifier as significant
  • then these identifiers are considered the same
  • age_Of_This_Old_Rhinoceros_At_My_Zoo
  • age_Of_This_Old_Rhinoceros_At_My_Safari
  • Also it is very annoying to keep typing in!

37
Meaningful Identifiers
  • It is common sense to try to create identifiers
    that are
  • Meaningful- they describe the information they
    refer to
  • E.g. Height, Count, BloodPressure, CarSpeed
  • Terse- They are only as long as necessary to
    convey meaning.

38
Reserved Words
  • Identifiers CANNOT be a reserved word.
  • Reserved words are built in words that have
    special meanings in the language. They must be
    used only for their specified purpose. Using them
    for any other purpose will result in a error.
  • e.g. do if switch while else return


39
Reserved Words
  • C is case sensitive. ALL reserved words are
    lower case

WHILE While while Are all different identifiers
only the last is a reserved word
40
Operators
  • All the data types we have seen (int, float,
    double, char and string) have a set of operators
    that can be applied to them
  • E.g. Numerical data uses the familiar - / for
    add subtract and divide.
  • The asterisk is used for multiplication
  • These work in the usual manner

41
string operators
  • arithmetic is not meaningful when applied to
    strings
  • You do not multiply first names!
  • strings operators do other things
  • E.g. The operator applied to strings joins them
    (concatenates) together
  • We will see other operations we want to do with
    strings like searching a string for a substring
    or comparing strings.

42
Expressions
  • A VALID arrangement of variables, constants,
    literals and operators

Expressions are evaluated and compute to a VALUE
of a given type E.g. Expression 9 4 computes
to 13
43
A special OPERATOR
The assignment operator
Causes much confusion! IT DOES NOT WORK LIKE
EQUALS IN MATHS
44
variable expression
Interpretation of Takes the value of
  • number 4
  • result 10 number
  • number number 1

expression simply consists of the literal int 4.
number takes the value of 4
expression evaluates to 40. result takes the
value of 40
expression evaluates to 5 number takes the value
of 5
KEY POINT expression is evaluated BEFORE it is
applied
45
Using assignments to get information into the
computer
  • int myage
  • string myname
  • double mysalary
  • myage 21
  • myname Chris
  • mysalary 1000.00

46
Interactive input formally covered later
  • using cin and cout
  • using data files

47
Bye Bye
Write a Comment
User Comments (0)
About PowerShow.com