Pandit Deendayal Petroleum University Gandhinagar
Pandit Deendayal Petroleum University Gandhinagar
CONSTANTS
VARIABLES
KEYWORDS
SPECIAL
ALPHABETS DIGITS SYMBOLS
C/C++ CHARACTER SET
• ALPHABETS : A … Z , a … z
• DIGITS : 0 … 9
• SPECIAL
SYMBOLS : ~ ` ! @ #
% ^ & * () _ - + =
| \ {} [] :
; “ ‘ < > , . ? /
C/C++ CHARACTER SET
• ALPHABETS : A … Z , a … z
• DIGITS : 0 … 9
• SPECIAL
SYMBOLS : ~ ` ! @ #
% ^ & * () _ - + =
| \ {} [] :
; “ ‘ < > , . ? /
VARIABLES/CONSTANTS
• VARIABLE:
Whose value can be changed.
• CONSTANT:
That never changes.
A Variable B
100 Constant ‘i’
TYPES OF CONSTANTS
CONSTANTS
PRIMARY SECONDARY
CONSTANTS CONSTANTS
• Range:
16-bit Compiler: -32768 to +32767
32-bit Compiler:
-2147483648 to +2147483647
• Examples:
1802 +1972 -4000 -32500
RULES: REAL CONSTANTS
• It must have at least one digit.
• It must have a decimal point.
• It can be + ve or – ve.
• If no sign precedes, it is assumed
to be positive.
• No commas or blanks are allowed
within it.
• EXAMPLES: +9.02 -19.72
RULES: CHARACTER
CONSTANTS
• Any alphabet, any digit or a special
symbol enclosed within single
inverted commas is known as
character constant.
e.g. 'a' '7' '+'
}
TYPES OF FUNCTION
• Built-in Functions
• User-defined Functions
FUNCTION
function name
void main ( )
{
}
FUNCTION
Return type
void main ( )
{
}
FUNCTION
}
FUNCTION
void main ( )
{
statement(s);
}
FUNCTION
10 20
function add() x y z
10 20 30
Functions
No Arguments/With Return Value
#include <iostream.h> float add(void)
#include <conio.h> {
void main() float x, y, z;
{ cout << “\nEnter 2 Nos.”;
float c; cin >> x >> y;
float add(void); z = x + y;
clrscr(); return z;
c = add(); }
cout << “Answer = “ << c;
getch();
}
How memory looks like?
function main() c
30
function add() x y z
10 20 30
Functions
With Arguments & Return Value
10 20 30
function add() x y
10 20
WHEN TO USE WHICH
FUNCTION
INPUT PROCESS OUTPUT
VOID FN FN FN
FN(VOID)
VOID MAIN FN FN
FN(INT)
INT FN FN MAIN
FN(VOID)
INT MAIN FN MAIN
FN(INT)
CONTROL STRUCTURES
• SEQUENCE STRUCTURE
• SELECTION STRUCTURE
• LOOP STRUCTURE
SEQUENCE STRUCTURE
entry
Action 1
Action 2
Action 3
exit
SELECTION STRUCTURE
entry
true false
condition
Action 1 Action 2
exit
Action 3
SELECTION STRUCTURE
• if-else
• switch
if-else (various forms)
1) if (condition) e.g. if ( a > 5)
statement; cout << a;
• ||
– if the first expression evaluates to true, the following
expression is not evaluated.
• &&
– if the first expression evaluates to false, the following
expression is not evaluated.
Examples of Short Circuit
int a, b, c;
a = 4; b = 10; c = 30;
if ( ( a < b ) || ( a < c ) )
/* (4<10)||(4<30) */
...
• y = ( x > 5) ? 3 : 4;
means if x > 5, y = 3 otherwise y = 4
• int big, a, b, c;
• big = ((a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c) );
Increment/Decrement Operators
• Unary
• ++ increment
• -- decrement
• Each has two flavours, pre and post
• Applies to integer data-types only
Pre Increment
• Value of variable is incremented and then
used in expression
• Syntax
++variableName
Example
int a, c;
a = 10;
/* a is 11 */
/* c is 11 */
Post Increment
• Value of variable is used in expression
and later incremented
• Syntax
variableName++
Example
int a, c;
a = 10;
/* a is 11 */
/* c is 10 */
Decrement operator
• Pre Decrement
– Similar to Pre Increment
– Syntax :
--variableName
• Post Decrement
– Similar to Post Increment
– Syntax :
variableName--
Operator Precedence
• Multiple operators in an expression
• Precedence decides the order of application of
operators
• Specified by a precedence table
• Operators at the top have higher precedence
• Operators in the same row have same
precedence
• () can be used to override precedence rules
Precedence of operators
() [] -> (Highest precedence)
! ~ ++ -- (type)* & sizeof
* / %
+ -
<< >>
< <= > >=
== !=
&
^
|
&&
||
?:
= += -= *= /= (Lowest precedence)
Exercise-Write C++ Program for
the following.
1. Find out largest and smallest of two values.
2. Find out largest and smallest of three values.
3. Find out net salary where net salary = gross
salary + allowances – deductions.
If gross salary > 10000, allowances are 10%,
deductions are 3% of gross salary.
If gross salary > 5000, allowances are 7%,
deduction are 2% of gross salary.
Exercise-Write C++ Program for
the following.
4. Calculate total, average of marks of three
subjects. Give following grades to the
student.
If average >= 70, distinction, if average >=
60, first class,
if average >= 50, second, if average >= 35,
third class,
Otherwise fail. If student secures < 35 marks
in any subject then declare student fail.
Exercise-Write C++ Program for
the following.
5. Find out whether a given no. is divisible by 7
or not.
6. Find out net sales where net sales = gross
sales – discount.
If gross sales > 20000, discount is 15%
If gross sales > 10000, discount is 10%
otherwise 5%.
SWITCH-CASE-DEFAULT
• Control statement
• Allows us to make a decision from the
number of choices.
• switch( integer expression)
{
case constant1 : { statements1 }
break;
case constant2 : { statements2 }
default : { statement3 }
}
SWITCH-CASE-DEFAULT
expression
constant 2 statements 2
missing break
control falls through
statements 3
default
SWITCH-CASE-DEFAULT
int i = 25;
switch (i)
{
case 121: cout << 121 << endl;
break;
case 7: cout << 7 << endl; break;
default: cout << “ I am in default “;
}
SWITCH-CASE-DEFAULT
if i ==
7 cout << 7;
initialization
Update/
statement
loop back
yes no
condition
DO…WHILE LOOP STRUCTURE
A
A
E B
E
F
do
D C {
G D
} while ( C );
G
Examples of do…while loop
Print 1st 10 natural numbers. Print 1st N natural numbers.
INPUT N
I=0 1 I=0 1
PRINT I PRINT I
+1 10 +1 N
initialization
no
condition
loop back
yes
Update/
statement
WHILE LOOP STRUCTURE
A
A
E B
E
F
while ( C )
D C
G
{
D
F
};
G
Examples of while loop
Print 1st 10 natural numbers. Print 1st N natural numbers.
INPUT N
I=0 1 I=0 1
PRINT I PRINT I
+1 10 +1 N
initialization
loop back no
condition
yes
update
statement
FOR LOOP STRUCTURE
Steps for writing Algorithm
A
A: Statements to be
E B executed before loop begins
B: Starting Value
F C: Ending Value
D: Difference
D C E: Control Variable (C.V.)
G = Initial Value (I.V.)
F: Loop Statements
G: Statements to be
executed after loop ends.
for ( C.V.= B ;C.V. <= C ; C.V. + D )
{
F
};
Examples of for loop
Print 1st 10 natural numbers. Print 1st N natural numbers.
INPUT N
I=0 1 I=0 1
PRINT I PRINT I
+1 10 +1 N