You are on page 1of 9

LECTURE 10

11 OCTOBER 2011

HOW IS C++ CODE EXECUTED


a) b) c) d)

Create file containing the program with a text editor. Run preprocessor to convert source file directives to source code program statements. Run compiler to convert source program into machine instructions (Object Code) Run linker to connect hardware-specific code to machine instructions, producing an executable file. Steps bd are often performed by a single command or button click. Errors detected at any step will prevent execution of following steps. 1-2

PROCESS STEPS
Source Code Object Code

Preprocessor
Modified Source Code

Linker

Executable Code

Compiler
1-3

ARITHMETIC OPERATORS

Used for performing numeric calculations


C++ has unary, binary operators

unary (1 operand) binary (2 operands)

-5 13 - 4

2-4

BINARY ARITHMETIC OPERATORS


SYMBOL OPERATION EXAMPLE VALUE OF

ans

+
* / %

addition
subtraction multiplication division Modulus / remainder operator

ans = 7 + 3;
ans = 7 - 3; ans = 7 * 3; ans = 7 / 3; ans = 7 % 3; ans = 7 % 5;

10
4 21 2 1 2
2-5

A CLOSER LOOK AT THE % OPERATOR

% (modulus) operator computes the remainder resulting from integer division


cout << 13 % 5; // displays 3

% requires integers for both variables


cout << 13 % 5.0; // error

2-6

CONSTANT VARIABLES
Constant Variables: variable whose content cannot be changed during program execution Used for representing constant values with descriptive names:

const int NUM_STATES = 50; const float price = 10.25;

It is common practice to name these in uppercase letters

3-7

CLASS EXERCISE

Write a program to take two numbers and display their sum, product, division and remainder on the screen.

WORKING SOLUTION
#include<iostream.h> #include<conio.h> void main() { int num1, num2; cout<<"Enter Num1 "; cin>>num1; cout<<"Enter Num2 "; cin>>num2; cout<<"\n\nSum of numbers is "<<num1+num2; cout<<"\n\nProduct of numbers is "<<num1*num2; cout<<"\n\nDivision of numbers is "<<num1/num2; cout<<"\n\nRemainder of the numbers is "<<num1%num2; getch(); }

You might also like