introduction to c++ september 12, 2007. today’s agenda quick review check your programs from...
TRANSCRIPT
Introduction to C++
September 12, 2007
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!
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.
Example of an comment!
//A first program in C++// John Smith
-or-
/* John Smith Computer Science Class This program will perform the …….
*/
#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!
main( ) ?
Is a part of every C++ program.
The parentheses after main indicate that main is a program building block called a function.
Left/Right braces { and } ?
The left brace { must begin the body of the program.
The right brace } must end the body of the program.
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.
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.
Return 0; ?
Indicates that program ended successfully.
Exits the function main( )
Check you work!
Write a program that prints a box, circle, and a diamond as follows:
**** ** ** * * * * ** * * * * ** * * * * * **** ** *
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
cout<<“Enter second integer\n”;// read an integercin>>integer2;sum = integer1 + integer2;
//calculationscout<<“Sum is “<< sum << endl;//print the sum to the screenreturn 0;}
Run your program?
What happens?
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.
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;
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.
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!