Você está na página 1de 81

CS 31:

Introduction to
Computer Science I
Topic 1

Introduction to
Programming
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-1

Introduction to Programming

PROGRAMMING
PROCESS
EXAMPLE
Driving a Car
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-2

Introduction to Programming
Programming Process Example
Types of Contol
Sequential: Step-by-step
Selection: One of choices
Repetition: Repeated step
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-3

Program Control Flow

Sequential
One Step at a Time

Begin

Compute

Compute

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

Compute

9/2010 John A. Rohr


All Rights Reserved

Compute

End

JAR 1-4

Program Control Flow

Selection
One of Choices
Begin

Condition

Compute

End

Compute

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-5

Program Control Flow

Repetition
Repeated Step
Begin

Initialize

Compute

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

Update

9/2010 John A. Rohr


All Rights Reserved

Condition

End

JAR 1-6

Program Control Flow

Composition
Replace any Sequential Step
with
Two or more Sequential Steps,
a Selection, or a Repetition

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-7

Program Control Flow

Composition
Example
Guess a Number Between 0 and 100

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-8

Program Control Flow

Example
Guess a Number Between 0 and 100
Start
Set

Set

G to 50

C to 25

G = N?

G < N?

Subtract
C from G

C is the current change

Done:

Add

Divide

N is the chosen number

G is N

C to G

C by 2

G is the current guess

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-9

Introduction to Programming

Programming Steps
1.
2.
3.
4.
5.
6.

Specification:
Description of the Task
Analysis:
Understanding the Task
Test Data:
Complete Set of Tests
Design:
Data and Algorithms
Implement:
Write the Program
Test and Debug: Make It Work

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-10

Algorithm Design

Tools
Flow Charts
Pseudocode

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-11

Algorithm Design

Flow Charts
Graphical Symbols
Easy to Visualize Flow
Difficult to Draw in Comments
Examples: Flow of Control Diagrams

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-12

Algorithm Design

Flow Chart Example


Guess a Number Between 0 and 100
Start
Set

Set

G to 50

C to 25

G = N?

G < N?

Subtract
C from G

C is the current change

Done:

Add

Divide

N is the chosen number

G is N

C to G

C by 2

G is the current guess

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-13

Algorithm Design

Pseudocode
Structured Natural Language
Easy to Understand
Easy to Use in Comments
Example: Following Program Comments

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-14

Algorithm Design

Pseudocode Example
Guess a Number Between 0 and 100
1.
2.
3.
4.

Set the guess to 50.


Set the change to 25.
If the guess is the number, DONE!
If the guess is less than the number,
add the change to the guess.
5. If the guess is greater than the number,
subtract the change from the guess
6. Divide the change by 2.
7. Go to Step 3.
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-15

Algorithm Design

Pseudocode Example
// Get the input values
// If the input values are not valid
// Display an error message
// Else
// For each input value
// Calculate the result
// Display the results
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-16

Programming Basics

Programming Practices
Design before implementing
Use comments liberally
Develop and test incrementally
Reuse functions
Start early
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-17

Programming Basics

Learning to Program
Program
Program
Program

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-18

Programming Language Levels

Machine vs. Human


Machine Language
Assembly Language
High-Level Language
Natural Language

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-19

Programming Language Levels

Machine Language

Language of the Computer


Consists of 0s and 1s
Machine Dependent
Lowest Level
No Limits
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-20

Programming Language Levels

Assembly Language

Simplified Computer Language


Consists of Symbols and Operations
Machine Dependent
Second Lowest Level
No Limits: Same as Machine Language
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-21

Programming Language Levels

High-Level Language

Mathematical Language
Consists of Mathematical Statements
Machine Independent (Mostly)
Highest Level (Except Natural)
Limited Capability: Not All Operations
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-22

Programming Language Levels

Uses

Machine Language:
Assembly Language:
High-Level Language:
Natural Language:

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

Debugging Only
Critical Tasks
Most Common
Not Yet

9/2010 John A. Rohr


All Rights Reserved

JAR 1-23

Programming Language Levels


Examples
High-Level:

Sum = First + Second;

Assembly:

LOAD
ADD
STORE

Machine:

210A = 0010000100001010
410B = 0010000100001011
310C = 0011000100001100

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

First
Second
Sum

9/2010 John A. Rohr


All Rights Reserved

JAR 1-24

Compilation Process

High-Level Machine
1.
2.
3.
4.
5.
6.

Enter high-level program


Translate to Assembly Language
Translate to Machine Language
Combine with Library Functions
Load into Computer
Run Program

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-25

Compilation Process
C++
Files

Obj/Asm
Files

Compiler

Obj

Asm

Source
Files

Assembler

Object
Files

Library
Files

Linker

Execute
File

Loader

Main
Memory

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-26

Compilation Process

User Steps
1.
2.
3.
4.
5.
6.

Enter high-level program with editor


Compile the program
Fix the syntactic errors
Run the program
Fix the semantic errors
Complete the documentation

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-27

High-Level Languages

Many Types

FORTRAN:
First, Mathematical
COBOL:
Early, English-Like
LISP,SNOBOL: Special-Purpose
ADA:
DOD Standard
C:
Operating System Implementation
C++: Object-Oriented Programming
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-28

High-Level Languages

C/C++

C:

C++: Enhancement of C
Object-Oriented Programming
Language for this Course

Invented to Program UNIX O. S.


General Purpose

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-29

Functions and Programs

Function
A single unit of computation
Accomplishes one thing
May use other functions
May produce a result
Sequence of statements

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-30

Functions and Programs

Program
Implementation of a complete solution
Solves a specified problem
Includes input and output
May use functions

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-31

C++ Basics

Keywords/Identifiers
Requirements

Letter or _ followed by letters, _, & digits


Case sensitive
Unlimited length:
Compiler may use 32

Conventions

Dont begin with _: Used by compiler


Multiple words:
Capitalize or use _
Constants:
Capitalize

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-32

C++ Basics

Keywords/Identifiers
Identifiers:
Keywords:
Examples:
Invalid:

Names of data
Identifiers reserved for
specific language use
x, data1, myName, ab123c,
big_variable, v1234
3z, XY 3, #a3

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-33

C++ Basics

Data Types
Hardware

Integer:
Real:

int
float Limited precision
double Extended precision
Character: char One character only

Software

Logical:

bool

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

True or False

9/2010 John A. Rohr


All Rights Reserved

JAR 1-34

C++ Basics

Integer Data

Discrete values
Counting numbers
Positive, negative, or zero
Values specified by decimal digits
No decimal point
Examples: 24, -80, 0, 16
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-35

C++ Basics

Real Data

Continuous values
Positive, negative, or zero
Values specified by decimal digits
Optional decimal point
Optional scientific notation
Examples: 3.14, -25.8, 0, 123, -3.45e8
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-36

C++ Basics

Boolean Data

True or False only


true and false are keywords in C++
No Boolean data type in C
False represented by zero value
True represented by any nonzero value
True value of one generated by compiler
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-37

C++ Basics

Character Data

Single items of a character set


Machine-dependent: Based on hardware
ASCII/ANSI
EBCDIC
Unicode
Characters are enclosed in single quotes
Examples: 'A', 'z', '3', '*', ' ', '~'
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-38

C++ Basics

String Data

Zero or more items of a character set


Can be empty (No characters)
Can be one character
Can be multiple characters
Strings are enclosed in double quotes
Examples: "A String", " ", "", "AbC!#21"
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-39

C++ Basics

Characters vs. Strings

Characters: Built-in to C++


char data type
Single character only
Enclosed in single quotes

Strings: Available using #include string


string data type
Zero or more characters
Enclosed in double quotes
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-40

C++ Basics

Constants and Variables

Elementary unit of information


Single data item
Name is an identifier
Type is specified in declaration
Constant is not changed once defined
Variable is set by computation
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-41

C++ Basics

Literals

Constant unit of information


Single data item
Specific value
Integer: Decimal number; No decimal point
Float: Decimal number; With decimal point
Character: One character of set
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-42

C++ Basics

Literal Examples

Integer:
Float:
Double:
Long Dbl:
Character:
String:

80
8.42F, 8.42f, 8.42E2F, 8.42e2f
16.24, 1.624E1, 1.624e1
8.42L, 8.42l, 8.42E2L, 8.42e2l
'*'
"String Literal"

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-43

C++ Basics

Escape Sequences
Characters with special meaning
Specified in C++ with preceding backslash
Limited set

\n
\'
\"
\\
\0

Newline
Single Quote
Double Quote
Backslash
Character String (Null) Terminator

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-44

C++ Basics

Data Declaration
Must be declared before use
Give compiler information about the data
Name
Type
Size (Not for elementary data types)
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-45

C++ Basics

Data Declaration Examples

int
int
float
double
bool
char

i;
x1, x2, abc;
f1, volts, power;
weight, v3, inp;
state;
ch, letter;

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-46

C++ Basics

Data Initialization
Must be initialized before first use
Two ways to initialize
Declaration
Assignment

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-47

C++ Basics

Data Initialization Examples

int
int
float
double
bool
char

i = 0;
x1 = 3, x2 = -5, abc = 3 * 5 + 9;
f1 = 0, volts = 120.5, power = 0.1;
weight = 100, v3 = 0.03, inp = -2;
state = false;
ch = '*', letter = 'X';

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-48

C++ Basics

Arithmetic Operators

Unary syntax
operator Operand: -value

Binary syntax
Operand operator Operand: a + 5

Parentheses
(number)
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-49

C++ Basics

Arithmetic Operators

+
*
/
%

Addition
Subtraction
Multiplication
Division
Modulus, Remainder

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-50

C++ Basics

Division

Two integer operands Integer result


Example: 12 / 5 = 2
Two real operands Real result
Example: 12.0 / 5.0 = 2.4
Mixed operands Real result
Example: 12.0 / 5 = 2.4
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-51

C++ Basics

Data Hierarchy
int/char
float
double
long double
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-52

C++ Basics

Increment and Decrement

++ Preincrement/Postincrement
Preincrement: Increment before use
Postincrement: Increment after use

--

Predecrement/Postdecrement

Postdecrement: Decrement before use


Postdecrement: Decrement after use
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-53

C++ Basics

Expressions

Operands and operators


Arithmetic calculations
Type conversion where allowed
Implicit: Automatic
Explicit: Cast

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-54

C++ Basics

Expression Examples

var1
item1 * 5
a * (b + c)
0.5 * base * height
x * (p * q / (m n) + t)
var = (num / div) * div
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-55

C++ Basics

Automatic Type Conversion

Promotion: No loss of accuracy


Example: Integer Float
10 Float variable becomes 10.0

Demotion: May lose accuracy


Example: Float Integer
10.1 Integer variable becomes 10
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-56

C++ Basics

C++ Statements

Terminated by semicolon
One action
Assignment
Input/Output
Control

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-57

C++ Basics

Assignment Statement

Set the value of a variable


Variable name (identifier) on left side
= operator
Value expression on right
Examples: value = 1.23;
data = base + 5;
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-58

C++ Basics

Assignment Statement

Order of evaluation not guaranteed


a + b is the same as b + a
Problem: n + (++n): Avoid this use

Shorthand notation for some statements


Variable value changed by +, -, *, /, or %
Form is variable += expression;
Same as variable = variable + expression;
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-59

C++ Basics

Assignment Examples

ans = factor1 * factor2;


q = v1 + v2 * (v3 + v4 / v5);
d2 += 16;
z *= size1 base;
a *= PI * r;
r %= d;
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-60

C++ Basics

Basic String Operations

Declaration: string string1;


string string2, string3;
Initialization: string string4 = "A String";
Assignment: string1 = "";
string2 = "Another string";
string3 = string4;
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-61

C++ Basics

Console Input/Output

Uses iostream library


#include <iostream>
using namespace std;

Input: cin stream extraction operator


Output: cout stream insertion operator
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-62

C++ Basics

Console Input: cin

Reads values from keyboard (stdin)


Values separated by white space
Space, tab, or newline

Lines ended with newline (<return>)


Examples
cin >> myData;
cin >> firstValue >> secondValue;
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-63

C++ Basics

Console Input: cin

Input values can be on multiple lines


Each input variable receives one item
Examples
Single line:
Multiple lines:

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

297 3.15 -12.574<return>


297 3.15<return>
-12.574<return>
9/2010 John A. Rohr
All Rights Reserved

JAR 1-64

C++ Basics

Console Input: cin.ignore()

May need to advance to next line


Must skip over newline at end of line
cin.ignore provides the capability
Skip up to 10000 character to newline
Stop on first character of next line
cin.ignore(10000, '\n');
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-65

C++ Basics

Console Output: cout

Displays values on the screen (stdout)


Strings, values, and expressions
Multiple items not separated
End lines with \n or endl (preferred)
Example: cout << "Example" << endl;
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-66

C++ Basics

Console Output: cout

Number formatting is automatic


Compiler decides what format to use
Can be explicitly controlled: e.g. 123.45
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-67

C++ Basics

Console Output: cout

Output lines ended explicitly


No new line unless specified
One statement: Multiple output lines
cout << a << endl << b << endl;

Multiple statements: One output line


cout << a; cout << " "; cout << b << endl;
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-68

C++ Basics

Console Output: cout

Use cout for text prompts


cout << "Enter a value: ";
cin >> myValue;

End final output with end of line


cout << endl;
Puts subsequent output on next line
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-69

C++ Basics

Console Output: cerr

Alternate output to screen (stderr)


Works like cout
Can be redirected to different file
Example
cerr << "Error message!" << endl;

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-70

C++ Basics

Console I/O Example


cout << "Enter data value:";
cin >> value;
cout << "The value is "
<< value
<< endl;
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-71

C++ Basics

More String Operations

String length: string.length() or string.size()


Single character extraction: string[0]
Substring: string.substr(start)
Substring: string.substr(start, count)
String output: cout << string;
String input: cin >> string;
(1 Word)
String input: getline(cin, string); (1 Line)
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-72

C++ Basics

String Operation Examples

string st = "Data string";


st.length() or st.size() is 11
st[0] is 'D'
st[6] is 't
st[11] does not exist
st.substr(3) is "a string
st.substr(8,2) is "in
st.substr(9,3) is illegal
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-73

C++ Basics

String Input

cin >> string;


Reads one word: Up to white space
Stops on white space character

getline(cin, string);
Reads one complete line
Stops on first character of next line
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-74

C++ Basics

String Input Examples

Input line: This is some text


string st1, st2, st3;
getline(cin, st1) puts
"This is some text" in st1
and stops at the start of the next line
cin >> st2; cin >> st3; puts
"This" in st2 and "is" in st3
and stops at the space after "is"

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-75

C++ Basics

Libraries

Provide built-in capabilities


Included by preprocessor
Made available with #include statement
Examples
#include <iostream> Stream input/output
#include <string>
Strings
#include <cstlib>exit, rand, and other
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-76

C++ Basics

Namespaces

Provide "last names" for identifiers


Resolve name conflicts with libraries
Standard libraries use std
Namespace name specified with using
Example: using namespace std;

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-77

C++ Basics

Comments

Two types can be intermixed


Single line comments (preferred)
Begin with //
End at end of line

Multiple line comments (alternate)


Begin with /*
End with */
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-78

C++ Basics

Comment Examples

// This is a single-line comment


x = 1; // This is a partial-line comment
/*
This is a mulitple-line comment
There can be many lines
This is the last line of the comment
*/
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-79

C++ Basics

Programming Style

Write understandable code


Use meaningful data names
Use names for constants
Use comments liberally
Protect against user errors
CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-80

UPDATES
Constant & Variable Examples
More Example Programs

CS 31: Introduction To CS I
Topic 1: Introduction to Programming

9/2010 John A. Rohr


All Rights Reserved

JAR 1-81

Você também pode gostar