Computer Programming -1- Lecture 4

advertisement
Computer Programming -1Lecture 4
2-2
comment
// sample C++ program
preprocessor
directive
#include <iostream>
which namespace
using namespace std;
to use
int main()
beginning of
function named main
{
beginning of
block for main
cout << "Hello, there!";
return 0;
}
end of block
for main
2-3
send 0 to
operating system
output
statement
string
literal
Character
Name
Meaning
//
Double slash
Beginning of a comment
#
Pound sign
< >
( )
{ }
" "
;
2-4
Beginning of preprocessor
directive
Open/close brackets Enclose filename in #include
Open/close
parentheses
Open/close brace
Open/close
quotation marks
Semicolon
Used when naming a
function
Encloses a group of
statements
Encloses string of
characters
End of a programming
statement
The cout Object
 Displays output on the computer screen
 You use the stream insertion operator << to
send output to cout:
cout << "Programming is fun!";
2-6
 Can be used to send more than one item to
cout:
cout << "Hello " << "there!";
Or:
cout << "Hello ";
cout << "there!";
2-7
 This produces one line of output:
cout << "Programming is ";
cout << "fun!";
2-8
 You can use the endl manipulator to start a
new line of output. This will produce two
lines of output:
cout << "Programming is" <<
endl;
cout << "fun!";
2-9
cout << "Programming is" << endl;
cout << "fun!";
Programming is
fun!
2-10
 You do NOT put quotation marks around endl
 The last character in endl is a lowercase L, not
the number 1.
endl
endl
This is a lowercase L
2-11
 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!";
Notice that the \n is INSIDE the
string.
2-12
cout << "Programming is\n";
cout << "fun!";
Programming is
fun!
2-13
The #include Directive
 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
The General form of the program written in a language C++
(General Syntax) :
#include<iostream.h>
int main ()
{
………..
………..
return 0 ;
}
The parts of C++ program
// This program will display a message on the screen.
#include<iostream.h>
int main ()
{
cout<< “ welcome to c++ ! \n “ ;
Return 0 ;
}
Output :
welcome to C++ !
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 */
2- Preprocessor Directive :
( #include<iostream.h> )
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.
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.
5- Output
( Cout<<“welcome to c++ ! \n“ ; )
cout statement used to print messages and values to the
screen.
Example :
#include <iostream.h>
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
- 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
(output)
I'm nora
6- return 0 ;
mean that program ended successfully.
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)
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)
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 “ ;
Download