Download presentation
Presentation is loading. Please wait.
Published byAubrey Wade Modified over 9 years ago
1
Introduction to C++ September 12, 2007
2
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!
3
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.
4
Example of an comment! //A first program in C++ // John Smith -or- /* John Smith Computer Science Class This program will perform the ……. */
5
#include ? 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!
6
main( ) ? Is a part of every C++ program. The parentheses after main indicate that main is a program building block called a function.
7
Left/Right braces { and } ? The left brace { must begin the body of the program. The right brace } must end the body of the program.
8
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.
9
Backslash \ ? Is called an escape character. It indicated that a special character it to be output. Escape SequenceDescription \nNewline \tHorizontal tab \aAlert. Sound the system bell \\Used to print a backslash character. \* Used to print a double quote character.
10
Return 0; ? Indicates that program ended successfully. Exits the function main( )
11
Check you work! Write a program that prints a box, circle, and a diamond as follows: **** ** * * * * **** ** *
12
Another Simple Program: Adding Two Integers //This program will add two numbers #include main( ) { int integer1, integer2, sum;//declaration cout<<“Enter first integer\n”;//prompt cin>>integer1; //read an integer
13
cout<<“Enter second integer\n”; // read an integer cin>>integer2; sum = integer1 + integer2; //calculations cout<<“Sum is “<< sum << endl; //print the sum to the screen return 0; }
14
Run your program? What happens?
15
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.
16
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;
17
Classwork/Homework Write 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.
18
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!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.