Professional Documents
Culture Documents
Mbeya University of Science and Technology: EEB 3107: Computer Programming Academic Year
Mbeya University of Science and Technology: EEB 3107: Computer Programming Academic Year
Technology
EEB 3107: Computer Programming
Academic year :2020/2021
EEB 3107_MUST 2
Introduction
EEB 3107_MUST 3
Introduction
Primary
Memory
CPU
CPU takes each
instruction and
executes it, possibly
storing new data
..
.. values as the program
5 ..
executes.
Basics of a Typical C++ Environment
6
A Simple Program: Printing a Line of Text
• Document programs
• Improve program readability
• Ignored by compiler
• Single-line comment
– Use C’s comment /* .. */ OR Begin with // or
Preprocessor directives
• Processed by preprocessor before compiling
• Begin with #
7
Moving from C to C++
EEB 3107_MUST 8
Example
EEB 3107_MUST 9
Running the program it outputs the statement:
Hello World
EEB 3107_MUST 10
Example
#include <iostream.h>
main()
EEB 3107_MUST 11
Running the program it outputs the statement:
Hello World
EEB 3107_MUST 12
1. // hello.cpp: printing Hello World message comment
2. #include <iostrem.h> preprocessor directive
3. main() function declarator.
4. { beginning of the program
5. cout << “Hello World”; body of the function main
6. } end of the program
EEB 3107_MUST 13
First Line: Comment Line
The statement which starts with symbols “//” (i.e, two slash
characters one after another without a space) is treated as a
comment. Hence, the compiler ignores the complete line
starting from the // character pair.
EEB 3107_MUST 14
Second Line: Preprocessor Directive
EEB 3107_MUST 15
Third Line: Function Declarator
EEB 3107_MUST 17
Fifth Line: Body of a Function
printf(“Hello World”);
EEB 3107_MUST 18
Sixth Line: Function End
In this program, the last line actually marks the end of program
and control is transferred to the operating system on termination
of the program.
EEB 3107_MUST 19
Compilation Process
The C++ program hello.cpp can be entered into the system using
any available text editor. The program coded by the programmer
is called the source code. This source code is supplied to the
compiler for converting it into the machine code.
Output Streams
Input Streams.
EEB 3107_MUST 21
Output Streams
the word cout is followed by the symbol <<, called the insertion or
put-to operator, and then with the items
(variables/constants/expressions) that are to be output.
Variables can be of any basic data type.
The following are examples of stream output operations:
EEB 3107_MUST 22
Output Streams
EEB 3107_MUST 23
Output Streams
The cout object will display all the items from left to right.
Hence, in the above case, it prints the massage string “Age =”
first, and then prints the value of the variable age. C++ does not
enforce any restrictions on the maximum number of items to
output. The complete syntax of the standard output streams
operation is as follows:
EEB 3107_MUST 24
Example
// read data
cout << “Enter Name: ”;
cin >> name;
cout << “Enter age: “;
cin >> age;
cout << “Enter Address: “;
cinEEB 3107_MUST
>> address; 25
Example
//Output data
EEB 3107_MUST 26
Running this program the output will be:
Enter Ange: 24
Name is Godphrey
Age is 24
EEB 3107_MUST 27
Thank you for Listening! Questions
EEB 3107_MUST 28