Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types
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!”; }
// 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.
#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.
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{}
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
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
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)
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.
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.
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
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 */ */
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!”; }
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; }
A Simple C++ Program Following items were top sellers _ during the month of June: _ Computer games _ Coffee _ Aspirin _
#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”;}
A Simple C++ Program Following items were top sellers _ during the month of June: _ Computer games Coffee Aspirin _
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
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
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