7/28/2019 TA ZC142-L4
1/41
TA ZC142 Computer Programming
Lecture 3
Date: 16/01/2013
7/28/2019 TA ZC142-L4
2/41
Last Lecture
C Programming Basics
Arithmetic, Logical and Relational
Operators Precedence and Associativity
Flow Charts and Algorithms
7/28/2019 TA ZC142-L4
3/41
Todays Lecture
Flow Charts and Algorithms
Programming Examples
7/28/2019 TA ZC142-L4
4/41
Type Conversion
Implicit Type Conversion
Explicit Type Conversion
7/28/2019 TA ZC142-L4
5/41
Implicit Type Conversion
C automatically converts any
intermediate values to the proper data
type Lower type is automatically converted to
the higher type before operation
proceeds
7/28/2019 TA ZC142-L4
6/41
7/28/2019 TA ZC142-L4
7/41
Relational OperatorsOperator Meaning
< Is less than
Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
7/28/2019 TA ZC142-L4
8/41
Examples:
5 = 0 FALSE
7/28/2019 TA ZC142-L4
9/41
Logical Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Q. Are !(a>b) and a
7/28/2019 TA ZC142-L4
10/41
Increment and Decrement
Operators++ Adds one to the operand-- Subtracts one from the operand
Both are unary operators
++i is equivalent to: i = i+1
--i is equivalent to: i = i-1
Is ++i and i++ are same??
7/28/2019 TA ZC142-L4
11/41
Postfix and Prefix Examples
j =10;
i =j++;
What is the values of i and j?
j =10; i=--j;
What is the values of i and j?
Output
i = 10 j = 11
Output
i = 9 j = 9
7/28/2019 TA ZC142-L4
12/41
Conditional Operator
Also called Ternary operator
Syntax
Variable = expression1 ? expression2
: expression3
Example:
a=5;b=10;
x =(a>b) ? a : b;
What will be the value of x??
Output
X = 10
7/28/2019 TA ZC142-L4
13/41
Bitwise Operators
Manipulation of data at bit level
Not be applied to float or double values
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
> Shift right
7/28/2019 TA ZC142-L4
14/41
Operators Associativity Rank
(),[] L to R 1
+, -, ++, --, !, ~, *, &, sizeof,
(type)
R to L 2
*, /, % L to R 3
Binary plus(+) and Binary minus (-) L to R 4
L to R 5
= L to R 6
==, != L to R 7
& L to R 8
^ L to R 9
| L to R 10
&& L to R 11
|| L to R 12
?: R to L 13
=, *=, /=, %=, +=, -=, &=, ^= R to L 14
, L to R 15
7/28/2019 TA ZC142-L4
15/41
Algorithms An Algorithm is just a detailed sequence of simple steps
that are needed to solve a problem
Flow Charts Flow Charts are maps or graphical representations of a
process.
Steps in a process are shown with symbolic shapes, and
the flow of the process is indicated with arrowsconnecting the symbols
ALGORITHMS AND FLOW CHARTS
7/28/2019 TA ZC142-L4
16/41
Symbols in Flowchart
START , END
INPUT
PROCESS INPUT DATA
DECISION
CONNECTOR
CONNECTOR
7/28/2019 TA ZC142-L4
17/41
Example Flow Chart
Marks = 50
Avg = 45
Marks > avg
END
START
YES
NO
Flow chart to find
whether the the marks ofa student is above avg or
below avg
Print below Avg
Print above Avg
7/28/2019 TA ZC142-L4
18/41
Draw a flow chart for calculating the gross
salary of a employee if his basic salary is
input through the keyboard. if basic salary is
less than Rs.1500 then HRA = Rs. 500 and
DA = 90% of basic. If salary is equal or
greater than 1500 then HRA = 10% and DA
= 95% of basic salary.
Example on flowchart
7/28/2019 TA ZC142-L4
19/41
START
INPUT bs
is
bs < 1500
Hra = 500
Da = bs * 90/100
Hra = bs * 10/100
Da = bs * 95/100
Gs = bs + hra + da
GSSTOP
NOYES
7/28/2019 TA ZC142-L4
20/41
7/28/2019 TA ZC142-L4
21/41
Control Structure
Sequential Control Structure
Its a sequential flow of execution of instructions.
Selection Control Structure
This control structure chooses among alternative
program statements.Repetition Control Structure
This control structure repeats a group of program
statements till it satisfies some condition.
7/28/2019 TA ZC142-L4
22/41
if Statement
Selection Control Structure
7/28/2019 TA ZC142-L4
23/41
If Statement
if (condition)
{
statement-block; }
next-statement;
condition
Statement -block
T
F
Condition is a C expression, which evaluates to
TRUE (non-zero) or FALSE (zero).
Statement-Blockcan be a single statement
7/28/2019 TA ZC142-L4
24/41
Example If Statements
if (x
7/28/2019 TA ZC142-L4
25/41
More If Examples
if (age >= 0 && age
7/28/2019 TA ZC142-L4
26/41
If - else Statement
if (condition)
true block statement(s);
else
false block statement(s);
condition
True Block
statement
False Block
statement
T F
elseallows choice between two mutuallyexclusive actions without re-testing condition.
7/28/2019 TA ZC142-L4
27/41
If else Statement example
if (i>j) k = i++ + --j;
else
k = ++i + j--;if (i>j && j>k)
{ i++;
j++;
}
else{ k++;
j--;
}
7/28/2019 TA ZC142-L4
28/41
Chaining ifs and elses
if(condition 1)
statement-1;else if(condition 2)
statement-2;
else if(condition 3)statement-3;
else if(condition 4)
statement-4;else
default-statement;
next_statement;
Example
7/28/2019 TA ZC142-L4
29/41
Example
if(month == 4 || month == 6 || month == 9 || month == 11)
printf(Month has 30 days.\n);
else if(month == 1 || month == 3 ||month == 5 || month == 7 ||
month == 8 || month == 10 ||
month == 12)
printf(Month has 31 days.\n);
else if(month == 2)
printf(Month has 28 or 29 days.\n);
else
printf(Dont know that month.\n);
Program using if else ladder
7/28/2019 TA ZC142-L4
30/41
Program using if-else ladder
/* Checking a character whether it is capital letter or smallletter or special symbol*/
#include
int main(){
char ch;
printf("Enter a char:");
scanf("%c",&ch);if(ch>='A' && ch='a' && ch='0' && ch
7/28/2019 TA ZC142-L4
31/41
Nesting of
ifelse statements
if (condition 1)
{
if (condition 2)
{statement(s) 1;
}
else
{statement(s) 2;
}
}
else
{
statement(s) 3;
}
next_statement;
7/28/2019 TA ZC142-L4
32/41
Nested if examples
if (x == 3)if (y != 6) {z = z + 1;w = w + 2;
}
if ((x == 3) && (y != 6)) {
z = z + 1;
w = w + 2;
}
is the same as...
7/28/2019 TA ZC142-L4
33/41
Matching else with If
else is always associated with closestunassociated if.
if (x != 10)
if (y > 3)
z = z / 2;
else
z = z * 2;
if (x != 10) {
if (y > 3)z = z / 2;
else
z = z * 2;
}
is the same as...
if (x != 10) {
if (y > 3)z = z / 2;
}
else
z = z * 2;
is NOT the same as...
int main() {
7/28/2019 TA ZC142-L4
34/41
Example: nesting of if-elseprogram to find largest of
the three numbers
int main() {float a, b, c;
printf(Enter three values\n);
scanf(%f %f %f,&a, &b, &c);
printf(Largest value is);if(a>b)
{ if(a>c)
printf(%f,a);
else
printf(%f,c);
}
else
{ if(c>b)
printf(%f,c);else
printf(%f,b);
}
return 0;
7/28/2019 TA ZC142-L4
35/41
Exercise: on if-else
1. Write a program to determine whether a number isodd or even and print the appropriate message.
2. Write a program to find the smallest of the threenumbers.
3. Write a program to determine whether the inputtedyear is a leap year or not.
7/28/2019 TA ZC142-L4
36/41
Exercise: on if-else
4. Write a program that takes the x-ycoordinates of a point in the Cartesian
plane and prints e message telling eitheran axis on which the point lies or the
quadrant in which it is found.
7/28/2019 TA ZC142-L4
37/41
Exercise: on if-else
1. Write a program to determine whether a number is
odd or even and print the appropriate message.
2. Write a program to find the smallest of the threenumbers.
3. Write a program to determine whether the inputtedyear is a leap year or not.
7/28/2019 TA ZC142-L4
38/41
Write a program to reverse a three digit decimalnumber and determine whether the two numbers
are equal or not.
Write a program to add the digits of a number. Let
the number 4 digit only.
Write a program to convert a decimal number
ranging from 1 to 7 into binary.
7/28/2019 TA ZC142-L4
39/41
Exercise: on if-else
Write a program to calculate area of acircular ring.
Write a program that takes the x-ycoordinates of a point in the Cartesianplane and prints a message telling eitheran axis on which the point lies or thequadrant in which it is found.
7/28/2019 TA ZC142-L4
40/41
Write a program to implement the followingdecision table to characterize an earth quakebased on its Richter scale number.
Richter Scale Number(n)
Characterization
n < 5.0 Little or damage
5.0
7/28/2019 TA ZC142-L4
41/41
Any Doubts or Questions?