Lecture 4 Structure of a Program(1)
Lecture 4 Structure of a Program(1)
|
Introduction
• No one learnt to ride a bicycle by
just reading about how to ride. At
some time you have to get up there
and take some bumps.
• Just like riding the bike, you are not a
good confident rider until you have
practiced riding over and over.
• Apply the same persistence to
learning how to write C++ programs.
• How?
Monday, M
ay 12, 202
Introduction to Programming | 7
#include <iostream>
• programming is carried out in add-on
style. That is, if something is already
available you can use the same and
develop further.
• There are trillions of programs
already developed by others. Why
should we re-invent the wheel?
Monday, M
ay 12, 202
Introduction to Programming | 8
#include <iostream>
• If we plan to use the readily
available programs we have to ask
the compiler to include them.
• Actual code will be available in
machine language found in in built
libraries.
Monday, M
ay 12, 202
Introduction to Programming | 9
#
• Lines beginning with a hash sign (#) are
directives for the preprocessor.
• This essentially means read me before
you compile and do what I say
essentially...
• This line notifies the preprocessor to
include in the program the contents of the
input/output program that is<iostream>.
Monday, M
ay 12, 202
Introduction to Programming | 10
#
• General format for directives to the
pre processor is
• #include <inbuild program you want
to include>
• the < and > are just enclosures for
the compiler to read between and
import accordingly .
Monday, M
ay 12, 202
Introduction to Programming | 11
#include <iostream>
• iostream must be included for any
program that outputs data to the
Monitor or inputs data from the
keyboard using C++’s stream
input/output.
Monday, M
ay 12, 202
Introduction to Programming | 12
using namespace std;
• A namespace is a declarative region
that provides a scope to the identifiers
(names of the types, function, variables
etc) inside it.
• We are utilizing the standard (std)
namespace which contains the
definition of cin, cout, vector, map..etc
identifiers
Monday, M
ay 12, 202
Introduction to Programming | 13
The using directive:
• This directive tells the compiler that
the subsequent code is making use
of names in the specified
namespace.
• In this case the namespace is std
Monday, M
ay 12, 202
Introduction to Programming | 15
Statements in C++
A programming statement is the
smallest independent unit in a
program, just like a sentence in the
English language. It performs a piece
of programming action.
A programming statement must be
terminated by a semi-colon (;), just
like an English sentence ends with| a16
Monday, M
ay 12, 202
Introduction to Programming
Statements in C++
Why not ends with a period like an
english sentence? This is because
period crashes with decimal point - it
is hard for the computer to
differentiate between period and
decimal point
Monday, M
ay 12, 202
Introduction to Programming | 17
int main ()
• When a program begins running, the
system calls the function main,
which marks the entry point of the
program.
Monday, M
ay 12, 202
Introduction to Programming | 18
int main ()
• This line initiates the declaration of a
Monday, M
ay 12, 202
Introduction to Programming | 19
int main ()
• Functions will be discussed in detail in
Monday, M
ay 12, 202
Introduction to Programming | 20
int main ()
The function named main is a special
function in all C++ programs; it is the
function called when the program is
run. The execution of all C++ programs
begins with the main function
regardless of where the function is
actually located within the code.
Monday, M
ay 12, 202
Introduction to Programming | 21
cout
• The standard output stream
Monday, M
ay 12, 202
Introduction to Programming | 22
<<
• Referred to as the stream insertion
operator
Monday, M
ay 12, 202
Introduction to Programming | 23
Out Put of our hello world program
Monday, M
ay 12, 202
Introduction to Programming | 24
Class /Lab Exercise
• Write a program that will output the
following statement
Monday, M
ay 12, 202
Introduction to Programming | 25
Class /Lab Exercise
• Write a program that will output the
following statement
Monday, M
ay 12, 202
Introduction to Programming | 26
Ole Sangale Road, Madaraka Estate. PO Box 59857-00200, Nairobi, Kenya
Tel: (+254) (0)703 034000/200/300 Fax : +254 (0)20 607498
Email: [email protected] Website: www.strathmore.edu
|