Lecture 3 Style and Variables - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Lecture 3 Style and Variables

Description:

... Software Engineer from the programmer ... of what the program does for the programmer ... should explain everything the programmer needs to know about the ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 28
Provided by: jimand
Category:

less

Transcript and Presenter's Notes

Title: Lecture 3 Style and Variables


1
Lecture 3Style and Variables
  • ENGR17 Engineering Programming
  • Section 1
  • Fall 2001

9/13/01
2
Outline
  • Style
  • Variables
  • Literal Constants
  • Select an appropriate name, data type, and
    initial value for a variable
  • Create and initialize a variable
  • Perform explicit type conversion

3
Style
There is no programming language, no matter how
structured, that will prevent programmers from
writing bad programs. -- L. Flon
  • Style is the most important part of programming!
  • It separates the Software Engineer from the
    programmer
  • Engineers have a reputation of writing awful
    programs
  • Mainly due to lack of style
  • Programs serve two purposes
  • Set of instructions for the computer
  • Clear description of what the program does for
    the programmer
  • Make your programs easy to understand

4
Comments
  • More important than the code itself
  • Makes the program readable for you and others
  • Two basic flavors
  • Multi-Line / comment // this is a
    multi-line comment /
  • Single-Line // comment// single-line comment
  • Which is better
  • Working program without comments
  • Broken program with comments
  • Comments should explain everything the programmer
    needs to know about the program

5
Where to Put Comments
  • Header Comment at top of each file
    (multi-line)/ Your Name Your USD ID
    number ENGR 17 HW1 September 7, 2001
    Program ltyour initialsgt_HW1.cpp
    ltprogram descriptiongt /
  • Comments before each function or class//
    Function name Main// This function starts the
    program ...int main()

6
Where to Put Comments (cont.)
  • Comments next to each variable (single-line)float
    currentPay // the users current pay
  • Comments above each significant section of
    code/ The next section of code gathers input
    from the user and stores it in variables ///
    Input current pay from consolecin gtgt
    currentPay// Input raise rate from consolecin
    gtgt raiseRate
  • Comments above each significant line of code//
    output the new pay to the consolecout ltlt "New
    Pay is " ltlt newPay

7
Variable Names
  • Badfloat xfloat y
  • Betterfloat currentPayfloat raiseRate
  • Goodfloat currentPay // current yearly
    payfloat raiseRate // percentage raise
  • Three Styles
  • All Lowercase (Variables) current_pay
  • All Uppercase (Constants) CURRENT_PAY
  • Mixed-Case (Local/Global Variables)currentPay
    CurrentPay

8
Complexity
  • Do not be clever
  • Cleverness makes for unreadable/unmaintainable
    code
  • Badwhile (\n ! p q)
  • Goodwhile (1) destination_ptr
    source_ptr destination_ptr
    source_ptr if ((destination_ptr-1)
    \n) break // exit loop if done
  • Good compiler will generate same machine code for
    both

9
Indentation
  • Indent for each new block or condition
  • Development Environment will take care of this
    for you
  • Two Styles
  • Short formif (totallt 0) cout ltlt "Total is
    less than zero\n" else cout ltlt "Total is
    greater than or equal to zero\n"
  • Long formif (totallt 0) cout ltlt "Total is
    less than zero\n"else cout ltlt "Total is
    greater than or equal to zero\n"

Use the short form!
10
Other Styles Issues
  • Single function should be no longer than 1 or 2
    pages
  • Avoid complex logic and nested ifs
  • Avoid long statements
  • Split single code lines into multiple small ones
  • Make sure the program is simple and easy to
    understand
  • ReminderStyle is the most important part of
    programming!(thats why we talked about it
    first)

11
Program Components
  • Variables
  • Memory locations for temporary data storage
  • Constants
  • Named
  • Literal
  • Arithmetic operators
  • Used in equations
  • Functions

12
Using Variables to Store Information
  • Data may be
  • Entered by user at keyboard
  • Read from a file
  • The result of a calculation
  • Contents can change as program runs
  • Select an appropriate name, data type, and value
    for it
  • Variable Names
  • Use a descriptive name
  • Be consistent
  • Follow specific rules

13
Rules for Names
14
Example Names
15
Key Words
16
Selecting an Appropriate Data Type
17
Special Characters
18
Guidelines for Selecting Data Type
  • Consider the memory requirement
  • Short Integer, Integer, or Long Integer
  • Use when a variable will always contain whole
    numbers
  • Float or Double
  • Use to store numbers with a decimal fraction
  • Character
  • Assign if the variable will always contain one
    character of data
  • General guidelines
  • Almost never use a short
  • Usually use long and double

19
Using Literal Constants
  • An item of data whose value does not change while
    the program is running
  • Types of literal constants
  • Numeric
  • Character
  • String

20
Using Literal Constants
21
Declaring a Variable
  • Must declare/create a variable before it can be
    used
  • Enter a C statement that specifies
  • Data type and name of the variable
  • Beginning, or initial value of the variable
  • Syntax
  • datatype variablename initialvalue
  • Declare variables at the beginning of the main
    function
  • Initialize the variables that you create

22
Declaring a Variable
23
Initializing Variables
  • Initialize a variable in the statement that
    creates the variable
  • Assign a literal constant to it of the same data
    type
  • If you dont initialize the variable, it will
    contain garbage until a value is assigned to it
  • Examples// declare variableschar response
    short hours 0int sales 0long
    total_sales 0

24
Explicit Type Conversion
  • Precede the item with the C keyword that
    represents the desired data type enclose the
    keyword in parentheses
  • Also known as type casting
  • Example// declare variablesdouble salesAmt
    0.0float taxRate (float) 0.0

25
Summary of Key Points Variables
  • Memory location for temporary data storage
  • Contents can change as program runs
  • Assign both a data type and a name
  • Initialize each variable in its declaration
    statement
  • Short Integer, Integer, and Long Integer to the
    integer 0
  • Double variables to the Double type number 0.0
  • Character variables to a space enclosed in single
    quotes
  • Use a type cast to initialize a Float variable

26
Questions
  • What is the most important part of programming?
  • Style
  • What two purposes does a program serve?
  • Set of instructions for the computer
  • Clear description of what the program does for
    the programmer
  • Give an example of the two types of comments.
  • Multi-line
  • Single line
  • What type of variable to store letters?
  • char
  • What type of variable to store integers between 0
    64?
  • short

27
Questions
  • What type of variable to store integers between
    -64,000 and 64,000?
  • int or long
  • What type of variable to store the number
    1.37e100?
  • double
  • What type of variable to store the number 57.3?
  • float
  • Whats the new line character?
  • \n
  • Whats the tab character?
  • \t
  • How do I print the message I said, Not me
  • cout ltlt I said, \Not me\\n
Write a Comment
User Comments (0)
About PowerShow.com