0% found this document useful (0 votes)
4 views

LECTURE 2

Uploaded by

oswardkipeleka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

LECTURE 2

Uploaded by

oswardkipeleka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

INTRODUCTION TO

PROGRAMMING PRINCIPLES
CODE: ITU07112
LECTURE 2
Key features of C++
C++ is:
• Free-format, i.e., there are few restrictions on the
appearance of the program.
• Case-sensitive.
• Compose of one or more functions and thus supports
structured programming.
• Supports Object-oriented programming methodology.
• Portable as it is available in all platforms, i.e., for all
operating systems.
STRUCTURE OF A C++ PROGRAM
The following example illustrates the structure of a C++ program.

//Welcome.cpp (program name)


//This program displays the message "Welcome to C++"
//message ‘Welcome to C++’ is displayed

#include<iostream>
using namespace std;
int main()
{
cout<<"Welcome to C++"<<endl;
return 0;
}
Key features of C++ cont…
i. Comments:
Comments are used for better understanding of the
program statements. The comment entries start with a //
symbol and terminated at the end of the line. In Program
above the lines are comment lines.

• //Welcome.c (program name)


• //This program displays the message "Welcome to C+
+"
• //message ‘Welcome to C++’ is displayed
Key features of C++ cont…
ii. # include Directive
The #include directive instructs the compiler to
include the contents of the file enclosed within
angular brackets (<>) into the source file. In
Program 2.1, the file iostream is included in the
source file welcome.cpp.
Key features of C++ cont…
iii. Function
All C++ programs comprise of one or more
functions. A function is identified by a function
name and a function body. A function body is
surrounded by curly braces ({}).
Key features of C++ cont…
iv. Output using cout
The statement
cout<<"Welcome to C++"<<endl;

causes the enclosed text to be displayed on the


screen and then move a cursor to next line
(endl).
Key features of C++ cont…
Note:
• No particular layout of the program is
enforced, but you MUST make the program
easy to read.
• The program file must have extension of .cpp
or .c.
FUNDAMENTAL DATA TYPES
The fundamental data types are data at the
lowest level, that is, those, which are used for
actual data representation in memory.
The fundamental data types and their function
are listed following table.
FUNDAMENTAL DATA TYPES

Data Type Used for representing

int Integers

float Real numbers

char Character.
#include<iostream>
Using namespace std;
Int main()
{
int number1;
Int number2;
Int sum;
cout<<“Please enter first number”<<endl;
cin>>number1;
cout<<“Please enter second number”<<endl;
cin>>number2;
Sum=number1+number2;

Cout<<“The sum is ”<<sum<<endl;


return 0;
}
VARIABLES
A variable is a symbol that represents a storage
location in the computer memory.

A variable must be defined/ declared before


using it and for this reason; definitions/
declaration usually appear before the main code.
VARIABLES cont…
Syntax:
 type variable_name
 type variable_name=expression.

First the expression is evaluated and then the


resulting value is assigned to the variable.
 int number1
 float number2
 char name
VARIABLES cont…
Example: Consider the following program.
//shows the use of variables
#include<iostream>
int main()
{
int var1, var2;
int var3=50;

var1 = 10;
var2 = var3 - var1;

cout << "Var1 is "<<var1<<endl;


cout << "Var3 is "<<var3<<endl;
cout << "Var3 - Var1 is "<<var2<<endl;
return 0;
}
VARIABLES cont…
OUTPUT
Var1 is 10
Var3 is 50
Var3 - Var1 is 40

Notice the difference methods of defining variables:


 int var1, var2
 int var3 =50
The first line defines two integer variable “var1” and “var2”.
The second line defines “var3” and assigns it a value to 50.
OPERATORS
In C++ we use the following arithmetic operators.
• + Addition
• Subtraction
• Multiplication
• / Division
• % modulus
Is the remainder when dividing two integers.
Example, 20 % 3 = 2. The modulus operator is
only for integers.

You might also like