introduction to c++ september 12, 2007. today’s agenda quick review check your programs from...

18
Introduction to C++ September 12, 2007

Upload: aubrey-wade

Post on 17-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Introduction to C++

September 12, 2007

Page 2: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Today’s Agenda

Quick Review Check your programs from yesterday Another Simple Program: Adding Two

Numbers Rules for naming variables Classwork/Homework Tomorrow we will work with Arithmetic

Expression. Quiz on Friday!

Page 3: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Review! Take out your notebook

Comments! Single one line comment begins with //. Multi-line comments begin with /* and

end with a */ Comments are used for documentation

of the program and improve program readability.

Help other people (programmers) read and understand your program.

Comments are ignored by the compiler and do not cause any execution.

Page 4: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Example of an comment!

//A first program in C++// John Smith

-or-

/* John Smith Computer Science Class This program will perform the …….

*/

Page 5: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

#include <iostream.h> ?

Is called a preprocessor directive.

It tells the processor to include in the program the contents of the input/output stream header file iostream.h.

This file must be included for any program that outputs data to the screen or inputs data from the keyboard.

Forgetting to include this file will cause and error!

Page 6: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

main( ) ?

Is a part of every C++ program.

The parentheses after main indicate that main is a program building block called a function.

Page 7: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Left/Right braces { and } ?

The left brace { must begin the body of the program.

The right brace } must end the body of the program.

Page 8: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

cout Statements ?

Instructs the computer to print on the screen the string of characters contained between the quotation marks.

Example: cout<<“Welcome to C++”;

Every statement must end with a semi-colon.

Page 9: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Backslash \ ? Is called an escape character. It indicated that a special character it to be

output.Escape Sequence Description\n Newline\t Horizontal tab\a Alert. Sound the

system bell\\ Used to print a backslash

character.\* Used to print a double quote

character.

Page 10: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Return 0; ?

Indicates that program ended successfully.

Exits the function main( )

Page 11: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Check you work!

Write a program that prints a box, circle, and a diamond as follows:

**** ** ** * * * * ** * * * * ** * * * * * **** ** *

Page 12: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Another Simple Program: Adding Two Integers

//This program will add two numbers#include<iostream.h>main( ){

int integer1, integer2, sum;//declaration

cout<<“Enter first integer\n”;//promptcin>>integer1; //read an integer

Page 13: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

cout<<“Enter second integer\n”;// read an integercin>>integer2;sum = integer1 + integer2;

//calculationscout<<“Sum is “<< sum << endl;//print the sum to the screenreturn 0;}

Page 14: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Run your program?

What happens?

Page 15: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Declaration???? The line

int integer1, integer2, sum; is a declaration! The words integer1 and integer2 and sum

are the names of variables. A variable is a location in the computer’s

memory where a value can be stored for used by a program.

int means that these variables will hold integer values (whole numbers).

All variables must be declared with a name and a data type before they can be used in a program.

Page 16: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Rules for naming your Variables

Is a series of characters consisting of letters, digits, and underscore(_).

Cannot begin with a digit. Upper and lower case letters are different. Ex: a1 and A1 is different. Choose meaningful variable names helps

a program to be self-documenting. Declaration of a variable must be placed

first before you use it. Example: int number;

Page 17: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Classwork/HomeworkWrite a statement(or comment) to accomplish

each of the following:1. State that a program with calculate the

product of three integers.2. Declare the variables x,y,z, and result to be of

type int.3. Prompt the user to enter three integers.4. Read three integers from the keyboard and

store them in the variables x, y, z.5. Compute the product of the three integers

contained in variables x, y,and z, and assign the result to the variable result.

6. Print “The product is” followed by the value of the variable result.

7. Return a value from main indicating that the program terminated successfully.

Page 18: Introduction to C++ September 12, 2007. Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules

Classwork/Homework Continued:

Using the statements you wrote for the above slide, write a complete program that calculates and prints the product of three integers.

Due Date will be Friday! Quiz on Friday! Tomorrow we will start working

with Arithmetic Expressions!