0% found this document useful (0 votes)
33 views14 pages

Chapter-2 (C++)

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

Programming 

Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

Diving into the Ocean 
How to develop a program? 

Basic Types of Instructions 
There are 4 types of instructions in a High level programming language 

1. Type Declaration Instructions 
2. Arithmetic Instructions 
3. Input /Output Instructions 
4. Control Instructions 

Page 1 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

1. Type Declaration Instructions 
 
• In the Type Declaration generally variables are declared 
Example: 
o int count;
o char c1;
o float value;
• Variables are declared in memory (RAM)

• Data before processing and after processing goes to memory 
• Different types of data require different memory space 
Example: 
Data type  keyword  size    size checking 
o Integer    (int)    2 bytes  sizeof(int) 
o Float    (float)  4 bytes  sizeof(float) 
o Character  (char)  1 byte  sizeof(char) 
 
 
1.1 Variables 
o Variables are those quantities, whose values can be changed 
throughout the program. 
o It is a memory space reserved, where values can be changed. 
o Example: A customer’s name 

Page 2 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

 
 
1.2 Constants 
o Constants are those quantities, whose values cannot be 
changed. 
o It is a memory space reserved, where values cannot be 
changed. 
o Example: Sales tax rate for a particular period 

Attributes of a variable 

In a program a variable has: 

I. Name 

Page 3 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

II. Type 
III. Size 
IV. Value 

Example : const int x=10; 

 
 
1.3 Keywords 
o Keywords are those words whose meanings are already 
defined to the compiler. 
o We cannot assign new meanings to them. 
o They are reserved words for doing a specific task. 
o They cannot be used as a variable. 
o Examples are: int, short, signed, unsigned, default, volatile, 
float, long, double, break, continue, typedef, static, do, for, 

Page 4 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

union, return, while, do, extern, register, enum, case, goto, 
struct, char, auto, const, etc. 

2. Arithmetic Instructions 
o Arithmetic instructions are used for calculation purposes. 
o These instructions are formed by using arithmetic operators 
and assignment operator. 
 
2.1 Arithmetic operator 
This operator is used for numeric calculations.  
These are of 2 types:  
I. Unary arithmetic operator 
II. Binary arithmetic operator 
 
I. Unary arithmetic operator 
o They require only one operand to perform calculation. 
o Examples: ++, ‐ ‐ 
o Syntax:  x++, y ‐ ‐. 

II. Binary arithmetic operator 

Page 5 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

o They require two operands to perform calculation. 
o Examples: + for (addition), ‐ for (subtraction), * for 
(multiplication), / for (division), % for (modulus). 
¾ Integer arithmetic    (int + int) results  int 
¾ Floating arithmetic    (float * float) results float 
¾ Mixed mode arithmetic  (int + float) results float 
¾ Syntax:  x + y, y – z, p * q, s / n, etc. 
 

2.2 Assignment operator 

A value can be stored in a variable with the use of assignment 
operator. 

• The assignment operator (=) can be used in assignment 
statement or assignment expression. 
• Operand on the left hand side should be a variable and the 
operand on the right hand side should be variable or 
constant or any expression. 
• Example:  
o Sum = x + y + z; 
o Sum = 5 + 2 + 3; 

Page 6 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

Precedence 
• Highest:  ( ) 
• Middle:   * , / , % 
• Lowest:   + , ‐ 

Example of Arithmetic Instruction 

If x=2, z=4, n=5, p=5,

Then solve the following arithmetic expression: 

Result = x*2/z+4/z+8–2+n/p
= 2*2/4+4/4+8-2+5/5
= 4/4+4/4+8-2+5/5
= 1+4/4+8-2+5/5
= 1+1+8-2+5/5
= 1+1+8-2+1
= 2+8-2+1
= 10-2+1
= 8+1
= 9

Page 7 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

3. Input /Output Instructions 
• As stated earlier, Data before processing and after processing 
goes to memory (RAM). 
 
3.1 Input instructions:  
o Input instructions are used to enter data from the input 
unit to RAM.  
o So to input the value of any variable, we need to use 
input instructions 
o Example:  
In C++, We use cin >> for input instructions. 
1. cin >> x;
2. cin >> y;
3. cin >> x >> y >> z;
Note: Statement terminator:  “ ; ” 
 
 
 
 

Page 8 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

3.2 Output instructions:  
o Data after processing does not go to the output unit; 
rather it directly goes to the RAM. 
o To display the value of the variable / constant or a string 
on output unit, we need to use output instruction. 
o Example: 

In C++ we have cout << that is used for output 
instruction. 

1. Cout << j;
2. Cout << k;
3. Cout << j << k;
4. Cout << “This is my first program”;
 
 
 
 
 
 

4. Control Instructions 
 
 

Page 9 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

 
 
 
 
 
 
 
 

Structure of C++ Program 
1. Comment line 
2. Preprocessor directive 
3. Global variable declaration 
4. Main function ( ) 

  Statements; 

 
1. Comment line 

/* …………………………………*/

Page 10 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

// …………………………………

• Comment line is used for increasing the readability of the 
program. 
 
2. Preprocessor directive 

#include <iostream.h>

• tells the compiler to include information about the standard 
input / output library 
 
 
3. Global variable declaration 
• This is the section where variables are declared globally. 
• Global variables can be accessed in all the functions used in 
the program 
Example: if x is declared after preprocessor directive, and 
outside main () function, it will be a global variable. 
#include <iostream.h>
int x; // global x

void main()
{

Page 11 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

int y; // local variable y


addition();
}// end of main

void addition()
{
Cout << “I am func.1”;
}
 
4. Main function 
• It is the user defined function. 
• Every program has one main () function from where 
program starts execution and it is enclosed with in a pair of 
curly braces. 
• The main () function can be anywhere in the program, but in 
general practice it is placed in the first position. 
• Syntax: 

main(){
………………;
………………;
………………;
}

Page 12 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

Modify My First program 
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
cout << “Welcome to programming…”;
cout << “I am a student of BSCS”;
getch();
}
 

 
 
 
 
 
 
 
 
 
 

Page 13 of 14 
 
Programming Fundamentals   |   Chapter 2    Prof. Ghulam Mohyuddin 
 

Programming Practice 
1. Write down the program that simply adds two integer values. 
 
2. Write down the program that calculate the sum of 5 numbers and 
display the average of these numbers. 

3. Write down the program that inputs 5 numbers from user and 
display the sum and average of these numbers. 

4. Write down the program to convert the temperature given in 
Fahrenheit into centigrade. 

5. Write a program to interchange the contents of two variables, 
while the values of variables are input through the keyboard. 

 
 

Page 14 of 14 
 

You might also like