Download presentation
Presentation is loading. Please wait.
Published byDella York Modified over 8 years ago
1
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types
2
A Simple C++ Program Programming is great fun! // my first program in C++ #include #include void main ( ) { cout << “Programming is great fun!”; cout << “Programming is great fun!”; }
3
// my first program in C++ This is a comment line. All lines beginning with two slash signs (//) are considered comments do not have any effect on the behavior of the program programmer can use them to include short explanations or observations within the source code itself.
4
#include Lines beginning with a sign (#) are directives for the preprocessor. They are not regular code lines directive #include tells the preprocessor to include the iostream standard file This specific file (iostream.h) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.
5
void main () The main function is the point where all C++ programs start their execution, independently of its location within the source code it is essential that all C++ programs have a main function The word main is followed in the code by a pair of parentheses (). That is because it is a function declaration Optionally, these parentheses may enclose a list of parameters within them Right after these parentheses we can find the body of the main function enclosed in braces{}
6
cout <<“programming is great fun” This line is a C++ statement A statement is a simple or compound expression that can actually produce some effect cout represents the standard output stream in C++ meaning of the entire statement is to insert a sequence of characters into the standard output stream (which usually is the screen). (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs
7
Special Characters CharacterUse // Double slash # Pound sign Open / close brackets Open / close brackets Marks the beginning of a comment Marks the beginning of a preprocessor directive Encloses a filename when used with the #include directive
8
Special Characters CharacterUse { } Open / close braces Encloses a group of statements, such as the contents of a function ( ) Open / close parentheses Used in naming a function as in void main (void)
9
Special Characters CharacterUse “ ” Open / close quotation marks Encloses a string of characters, such as a message that is to printed on the screen. ; Semicolon Marks the end of a complete programming statement.
10
C++ Statement cnt’d For each C++ statement there are grammatical rules (called syntax rules); For each C++ statement there are grammatical rules (called syntax rules); To write any C++ statement correctly we MUST follow the statements syntax rules; To write any C++ statement correctly we MUST follow the statements syntax rules; A violation of the syntax rules of a statement causes the the compiler to report a syntax error. A violation of the syntax rules of a statement causes the the compiler to report a syntax error.
11
A sequence of C++ statements; A sequence of C++ statements; There are grammatical rules (syntax rules) that determine the structure of a correct C++ program; There are grammatical rules (syntax rules) that determine the structure of a correct C++ program; In general any C++ program consists of one or more mini programs called functions; In general any C++ program consists of one or more mini programs called functions; The simplest c++ program contains one function called main, that has the following structure: The simplest c++ program contains one function called main, that has the following structure: void main() {statementstatement…} Terms we Must Know C++ program
12
comments A comment is text that is written by the programmer to explain in English the logic of program statements; A comment is text that is written by the programmer to explain in English the logic of program statements; Comments are ignored by the compiler (not translated), since they are not C++ statements; Comments are ignored by the compiler (not translated), since they are not C++ statements; Comments can be written in two ways in a C++ program: Comments can be written in two ways in a C++ program: 1.2 forward slashes followed by text; these comments cannot span more than one line e.g. // this is a comment // this is a comment 2.Enclose text between /* and */ ; in this case the comment cab span multiple lines, e.g.: /* compute the area /* compute the area of the circle of the circle */ */
13
A Simple C++ Program Programming is _great fun!_ // A simple C++ program #include #include void main() { cout << “Programming is ”; cout << “great fun!”; cout << “great fun!”; }
14
A Simple C++ Program #include #include void main ( ) { cout <<“Following items were top ” <<“sellers” << endl; <<“sellers” << endl; cout <<“during the month of June:” << endl; cout <<“Computer games” << endl; cout <<“Coffee” << endl; cout <<“Aspirin” << endl; }
15
A Simple C++ Program Following items were top sellers _ during the month of June: _ Computer games _ Coffee _ Aspirin _
16
#include void main () { cout << “Following items were top ” << “sellers\n”; << “sellers\n”; cout << “during the month of June: \n”; cout << “during the month of June: \n”; cout << “Computer games\nCoffee”; cout << “Computer games\nCoffee”; cout << “\nAspirin\n”; cout << “\nAspirin\n”;}
17
A Simple C++ Program Following items were top sellers _ during the month of June: _ Computer games Coffee Aspirin _
18
Common Escape Sequences EscapeSequence Description \n Newline \t Horizontal tab \a Alarm Causes the cursor to the next line for subsequent printing Causes the cursor to skip over to the next tab stop Causes the computer to beep
19
Common Escape Sequences \r Return Causes the cursor to go to the beginning of the current line, not the next line. \b Backspace Causes the cursor to backup, or move left one position EscapeSequence Description
20
Common Escape Sequences \\ Backslash \’ Single quote \” Double quote Causes a backslash to be printed Causes a single quotation mark to be printed Causes a double quotation mark to be printed EscapeSequence Description
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.