Download presentation
Presentation is loading. Please wait.
Published byDenis Cummings Modified over 8 years ago
1
Lecture 4 Computer Programming -1-
2
2-2
3
// sample C++ program #include using namespace std; int main() { cout << "Hello, there!"; return 0; } 2-3 comment preprocessor directive which namespace to use beginning of function named main beginning of block for main output statement string literal send 0 to operating system end of block for main
4
2-4 CharacterNameMeaning // Double slashBeginning of a comment # Pound signBeginning of preprocessor directive Open/close bracketsEnclose filename in #include ( ) Open/close parentheses Used when naming a function { } Open/close braceEncloses a group of statements " Open/close quotation marks Encloses string of characters ; SemicolonEnd of a programming statement
5
The cout Object
6
Displays output on the computer screen You use the stream insertion operator < < to send output to c out : cout << "Programming is fun!"; 2-6
7
Can be used to send more than one item to cout: cout << "Hello " << "there!"; Or: cout << "Hello "; cout << "there!"; 2-7
8
This produces one line of output: cout << "Programming is "; cout << "fun!"; 2-8
9
You can use the e ndl manipulator to start a new line of output. This will produce two lines of output: cout << "Programming is" << endl; cout << "fun!"; 2-9
10
2-10 Programming is fun! cout << "Programming is" << endl; cout << "fun!";
11
You do NOT put quotation marks around e ndl The last character in e ndl is a lowercase L, not the number 1. 2-11 endl This is a lowercase L endl
12
You can also use the \ n escape sequence to start a new line of output. This will produce two lines of output: cout << "Programming is\n"; cout << "fun!"; 2-12 Notice that the \n is INSIDE the string.
13
2-13 Programming is fun! cout << "Programming is\n"; cout << "fun!";
14
The #include Directive
15
Inserts the contents of another file into the program This is a preprocessor directive, not part of C++ language #include lines not seen by compiler Do not place a semicolon at end of #include line 2-15
16
The General form of the program written in a language C++ (General Syntax) : #include int main () { ……….. return 0 ; }
17
The parts of C++ program // This program will display a message on the screen. #include int main () { cout<< “ welcome to c++ ! \n “ ; Return 0 ; } Output : welcome to C++ !
18
1- comments : // This program will display a message on the screen. // comments are text that is ignored by the compiler but tell notes or descriptions at any statement in the program. Types of comments : 1- //double-slash comment : Tell compiler to ignore everything that follow this comment until the end of the line. 2- /* slash –star comment : Tell the compiler to ignore everything that follows the comment until it find Star-slash */
19
2- Preprocessor Directive : ( #include ) this file must be included for any program that outputs data to the screen or inputs data from the keyboard using (cout) and (cin) statements.
20
3- main function : ( int main () ) Every C++ program has main () Function. main () Function is a special Function because it is called automatically when the program start but the other Functions are called by other Functions. 4- { } begin, end the body of every function.
21
5- Output ( Cout<<“welcome to c++ ! \n“ ; ) cout statement used to print messages and values to the screen. Example : #include int main ( ) { cout << 7 << " is an integer.\n"; cout << 'a' << "is a character.\n"; return 0 ; } Output : 7 is an integer. a is a character
22
- Escape Sequences : \n : tell cout to put new line after the printer line \t : insert a tab character Cout<<"Hello world \t I'm nora"; (program) Hello world I'm nora (output) 6- return 0 ; mean that program ended successfully.
23
Examples Cout<<"Hello world"; Cout<<"I'm a C++ programmer"; Hello world I'm a C++ programmer (output) Cout<<"Hello world\n"; Cout<<"I'm a C++ programmer"; Hello world I'm a C++ programmer (output). Cout<<"Hello world"<<"I'm a C++ programmer"; Hello world I'm a C++ programmer (output)
24
Cout<<"Hello world\n"<<"I'm a C++ programmer"; Hello world I'm a C++ programmer (output) Age=19; Cout<<''I'm "<<Age << "years old'' ; I'm 19 years old (output) Cin>>Age; Cout<<''I'm "<<Age<< "years old" ; 18 I'm 18 years old (output)
25
Example : What prints when each of the following c++ statement is executed let x=2 and y=3 a- cout<< x; b- cout<< x+x ; c- cout<<"x=" ; d- cout<<"x="<<x ; e- //cout<< x+y ; f- cout<< x+y <<"="<<y+x ; g- cout <<“ \n “ ;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.