Unit 2 - PIC
Unit 2 - PIC
An operator is a symbol that tells the computer to perform certain mathematical and logical manipulations.
Operator is used to manipulate data and variable.
Example : main()
{
int x;
read:
scanf(“%d”, &x);
if(x<0)
goto read;
}
18) Define the term looping. What are the different looping statements available in C?
Looping is the process where the sequence of statements are continuously executed until a specific condition is met.
Different looping statements available in C are :
1. while loop
2. do while loop
3. for loop
do while is an exit controlled loop. do while loop the condition is tested after the execution of the loop. Therefore the
body of the loop is executed atleast once.
Break statement is used to get the control of the program out of the loop. When a break statement is encountered inside
a loop, the loop is immediately exited and the program continuous with the statement immediately
Continue statement is used to get the control of the program to the beginning of the loop. Continue statement tells the
compiler to skip the following statements and continue with the next iteration.
1) List and explain arithmetic, logical and relational operators available in C
Operator is a symbol that tells the computer to perform certain mathematical or logical operations. Operators are used
in programs to manipulate data and variables.
Arithmetic operators: Arithmetic operators are mathematical functions that take two operands and perform
calculations on them.
Basic arithmetic operators are
Addition +
Subtraction -
Multiplication*
Division /
Modulo division %
Relational operators : Relational operators are used to compare two quantities and to make decisions depending on
their relation.
There are 6 relational operators:
< is less than
> is greater than
<= is less than or equal to
>= is greater than equal to
== is equal to
!= is not equal to
Logical operators : Logical operators are used when we want to test more than one condition and make decision.
There are 3 logical operators
&& AND
|| OR
! NOT
Let a=10 and b=5 a b a && b a || b
Assignment operators : Assignment operators are used to assign the result of an expression to a variable. The usual
assignment operator is ‘=’. C has a set of shorthand operators of the form
v op = exp;
where v is the variable, exp is an expression and op is an arithmetic operator. The operator op= is known as the
shorthand operator.
The assignment statement
v op = exp;
equivalent to
v = v op(exp)
Example :
Bitwise Operators:
C has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level.
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left
>> shift right
Ex: 1001101001
<<(shift left) x=3
1101001000
Special Operators:
Special operators are comma operator and sizeof operator.
Comma operator : The comma operator can be used to link the related expressions together. A comma linked list of
expressions are evaluated left to right and the value of right most expression is the value of the combined expression.
Ex: value(x=10,y=5,x+y)
First assigns the value 10 to x, then assigns 5 to y, and finally assigns 15(i.e 10+5) to value.
The sizeof operator: The sizeof is a compile time operator and when used with an operand it returns the number of
bytes the operand occupies.
Ex : m=sizeof(sum)
n=size(long int)
The sizeof operator is normally used to determine the lengths of arrays and structures when their sizes are not known to
the programmer. It is also used to allocate memory space dynamically to variables during execution of a program.
variable = expression;
Variable is any valid C variable name. When the statement is encountered, the expression is evaluated first and
the result then replaces the previous value of the variable on the left-hand side. All variables used in the
expression must be assigned values before evaluation is attempted. Examples of evaluation statements are
x = a * b - c;
y = b / c * a;
z=a – b / c + d;
The blank space around an operator is optional and adds only to improve readability. When these statements are
used in a program, the variables a, b, c, and d must be defined before they are used in the expressions.
Example:
Program
main()
{
float a, b, c, x, y, z;
a = 9;
b = 12;
c = 3;
x = a - b / 3 + c * 2 - 1;
y = a - b / (3 + c) * (2 - 1);
z = a - (b / (3 + c) * 2) - 1;
printf("x = %f", x);
printf("y = %f", y);
printf("z = %f", z);
}
OUTPUT:
x = 10.000000
y = 7.000000
z = 4.000000
High priority * / %
Low priority + -
The basic evaluation procedure includes two' left-to-right passes through the expression. During the first pass, the
high priority operators (if any) are applied as they are encountered. During the second pass, the low priority operators
(if any) are applied as they are encountered.
x = a-b/3 + c*2-1
x = 9-12/3+3*2-1
Step2: x=9-4+6-1
Second pass
Step3: x =5+6-1
Step4: x = 11-1
Step5: x = 10
The order of evaluation can be changed by introducing parentheses into an expression. Consider the same expression
with parentheses as shown below:
9-12/(3+3)*(2-1)
Whenever parentheses are used, the expressions within parentheses assume highest priority. If two or more sets of
parentheses appear one after another as shown above, the expression contained in the left-most set is evaluated first
and the right-most in the last.
For example:
9-(12/(3+3) *2)-1=4
Where as
9-((12/3) + 3 * 2)-1 = -2
While parentheses allow us to change the order of priority, we may also use them to improve understandability of the
program.
C automatically converts any intermediate values to the proper type so that the expression can be evaluated without
losing any significance. This automatic conversion is known as implicit type conversion.
Given below is the sequence of rules that are applied while evaluating expressions.
Conversion Hierarchy
Explicit Conversion
C performs type conversion automatically through implicit type conversion. However, there are instances when we
want to force a type conversion in a way that is different from the automatic conversion. Consider, for example, the
calculation of ratio of females to males in a town.
Since female_number and male_number are declared as integers in the program, the decimal part of the result of the
division would be lost and ratio would represent a wrong figure. This problem can be solved by converting locally
one of the variables to the floating point as shown below:
ratio = (float) female_number/male_number
The operator (float) converts the female_number to floating point for the purpose of evaluation of the expression.
Then using the rule of automatic conversion, the division is performed in floating point mode, thus retaining the
fractional part of result.
The process of such a local conversion is known as explicit conversion Or casting a value. The general form of
casting is
(type-name) expression
where type-name is one of the standard C data types. The expression may be a constant, variable or an expression.
Some examples of casts and their actions are shown in below
Casting can be used to round-off a given value. Consider the following statement:
X =(int) (y+0.5);
If y is 27.6, y+0.5 is 28.1 and on casting, the result becomes 28, the value that is assigned to x. Of course, the
expression, being cast is not changed.
Given below is the sequence of rules that are applied while evaluating expressions.
➢All short and char are automatically converted to int
➢If one of the operands is double, the other will be converted to double and the result will be double;
➢If one of the operands is float, the other will be converted to float and the result will be float;
➢If one of the operands is long int, the other will be converted to long int and the result will be long int;
➢If one of the operands is long int and the other is unsigned int, then the result will be long int;
Explicit Conversion
C performs type conversion automatically through implicit type conversion. However, there are instances when we
want to force a type conversion in a way that is different from the automatic conversion. Consider, for example, the
calculation of ratio of females to males in a town.
Since female_number and male_number are declared as integers in the program, the decimal part of the result of the
division would be lost and ratio would represent a wrong figure.
This problem can be solved by converting locally one of the variables to the floating point as shown below:
The operator (float) converts the female_number to floating point for the purpose of evaluation of the expression. Then
using the rule of automatic conversion, the division is performed in floating point mode, thus retaining the fractional
part of result.
a) simple if statement : When there is only one condition then simple if statement is used. If the test condition is
true the statement block will be executed otherwise the statement will be skipped and the control of the
program goes to the next statement in the program.
Syntax :
if(expression)
{
statement;
}
statement x;
Example :
If(a==b)
{
printf(“a and b are equal”);
}
b) if else statement : When there are two conditions then if else statement is used. If the condition or the
expression is true then the first statement is executed i.e the true block statements otherwise the else statement is
executed i.e the false block statement. Either first statement or the second statement will be executed, not both.
Example :
if(age>18)
{
printf(“Person is eligible to vote”);
}
else
{
printf(“Person is not eligible to vote”);
}
c) nested if else statement : When there are series of decisions involved then we have to use more than one if
else statement in nested form.
Syntax (General form) :
if(condition 1)
{
if(condition 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
statement 3;
}
Statement x;
If the condition 1 is false then statement 3 is executed. If condition 1 is true then condition 2 is tested. If both the
conditions are true then statement 1 is executed, otherwise the statement 2 will be executed.
Example :
if(n1>n2)
{
if(n1>n3)
printf(“n1 is the largest”);
else
printf(“n3 is the largest”);
}
else
{
if(n2>n3)
printf(“n2 is the largest”);
else
printf(“n3 is the largest”);
}
d) else if ladder: When there are more than two conditions to be tested then else if is used. Here the conditions
are tested from top to bottom. If the first condition is true statement 1 is executed else second statement is
tested and if its true then the statement 2 will be executed. If the second condition is also false then the third
condition is tested and so on. Any one statement is executed which is associated with the true condition. If
none of the condition is true then the default statement is executed.
{
statement 3;
}
.
.
.
else if(condition n)
{
Statement n;
}
else
{
default statement;
}
Statement x;
Example :
if(avg>90)
{
printf(“Grade is A”);
}
else if(avg>70)
{
printf(“Grade is B”);
}
else if(avg>50)
{
printf(“Grade is C”);
}
else if(avg>35)
{
printf(“Grade is D”);
}
else
{
printf(“FAIL”);
}
9) Explain nested if statement with the help of a program example
When there are series of decisions involved then we have to use more than one if else statement in nested form
that is using the nested if statement.
If the condition 1 is false then statement 3 is executed. If condition 1 is true then condition 2 is tested. If both the
conditions are true then statement 1 is executed, else the statement 2 will be executed.
Example :
#include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter the numbers:");
scanf("%d%d %d",&a,&b,&c);
if(a>=b)
{
if(a>=c)
printf("%d is the largest number",a);
else
printf("%d is the largest number",c);
}
else
{
if(b>=c)
printf("%d is the largest number",b);
else
printf("%d is the largest number",c);
}
getch();
return 0;
}
This program is to find the largest of three numbers using nested if statement. Three integer values are stored inside the
variables a b and c. is tested. If both these conditions are true then the value of a will be printed. then the condition is
tested for a>b if the condition is true again the condition a>c .If the first condition a>b is false then the control will go
to the else statement and condition b > c is tested. If this is true the value of b is printed else the value of c will be
printed.
10) Explain else..if ladder with the help of a program example.
When there are more than two conditions to be tested then else if is used. Here the conditions are tested from top to
bottom. If the first condition is true statement 1 is executed else second statement is tested and if its true then the
statement 2 will be executed. If the second condition is also false then the third condition is tested and so on. Any
one statement is executed which is associated with the true condition. If none of the condition is true then the default
statement is executed.
Example :
#include<stdio.h>
main()
{
int m1,m2,m3,m4,m5,total;
float per;
clrscr();
printf("Enter the marks in five subjects:\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
total=m1+m2+m3+m4+m5;
per=total/5;
if(per>=60 && per<=100)
printf("Grade = A");
else if(per>=50 && per<=60)
printf("Grade = B");
else if(per>=40 && per<=50)
printf("Grade = C");
else
printf("FAIL");
getch();
return 0;
}
This is a program to find the grade of students. Marks of five subjects are entered through the keyboard and stored in
the variables m1,m2,m3,m4 and m5. The total and average is found using the formulas. Then the else if ladder is
used to find the grade of the student. If the first condition is true the first statement is printed, else the second
condition is tested. If it is true the second statement is executed else the third statement is tested and so on. The
process continues until the value matches and the grade associated with it will be executed. If none of the values
match then the default statement ‘FAIL’ will be printed.
………………
………………
default : default block;
break;
}
Here expression is an integer expression or characters. value-1, value-2……are considered as the case labels. Each of these
values should be unique within the switch statement. block-1, block-2…..are the statement lists which contain zero or more
statements. When switch is executed, the expression is compared with the values value-1,value-2….. If a case is found
whose value matches with the value of the expression then the block of statements of that case will be executed. break
statement is used to end each block that causes exit from switch statement. The default block is executed if the value of the
expression does not match with any of the case values.
Example:
ch=getchar();
switch(ch)
{
case ‘1’:
printf(“Addition of two integer is:%d”,a+b);
break;
case ‘2’:
printf(“Subtraction of two integer is :%d”,a-b);
break;
default:
printf(“Invalid choice”);
}
Here the value of the variable ch will be compared with case values 1 and 2. If the value of variable ch is 1 then the
block followed by case 1 will be executed. If the value of ch is 2 the block of case 2 is executed. If the value of ch
does not match with both of these cases then the default block is executed.
Here i is the integer variable which has been assigned with the initial value 1.while statement checks for the condition
i<5 if this condition is true then the statement within the loop will be executed that is the value of i will be printed
which is 1. Then the value of i will be incremented by 1 so the value of i becomes 2. The condition i<5 is once again
tested and if it is true the value of i is again printed. This process continuous until the condition becomes false, once
the condition is false the loop will be terminated. So the final output will be 1 2 3 4.
Example:
int i=1;
do
{
printf(“%d”,i);
i++;
}
while(i<5);
Here i is the integer variable which has been assigned with the initial value 1. The body of the loop is executed first
that is the value of i will be printed which is 1. Then the value of i will be incremented by 1 so the value of i
becomes 2. After that the while statement checks for the condition i<5, if this condition is true then the body of the
loop executed once again. The condition i<5 is once again tested and if it is true the value of i is again printed. This
process continuous until the condition becomes false, once the condition is false the loop will be terminated. So the
final output will be 1 2 3 4. Since the condition is tested at the end of the loop the initial value of i will be printed
even if the condition is false at the first evaluation.
Here the initial value of variable i is 0. The condition here is i<5 which will be tested, if the condition is true for 0<5
then the statement will be printed that is the value of i will be printed which is 0. Then the control will go back to the
increment section of for loop and the value of i will be incremented by i so the value of i becomes 1. Then again the
condition is tested for 1<5, if this is true then again the value of i will be printed. This will continue until the condition
becomes false. Once the condition is false loop will be terminated and the control will go to the next statement in the
program.
Nested loops
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside
another loop.
Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops.
The nesting level can be defined at n times. We can define any type of loop inside another loop, for example, we can
define 'while' loop inside a 'for' loop.
Example:
int main()
{
int n;
printf("Enter the value of n :");
for(int i=1;i<=n;i++) // outer loop
{
for(int j=1;j<=10;j++) // inner loop
{
printf("%d\t",(i*j)); // printing the value.
}
}
}
First, the 'i' variable is initialized to 1 and then program control passes to the i<=n.
The program control checks whether the condition 'i<=n' is true or not.If the condition is true, then the program control
passes to the inner loop. The inner loop will get executed until the condition is true. After the execution of the inner
loop, the control moves back to the update of the outer loop, i.e., i++.
After incrementing the value of the loop counter, the condition is checked again, i.e., i<=n. If the condition is true,
then the inner loop will be executed again. This process will continue until the condition of the outer loop is true.
Infinite loops
An infinite loop is a looping construct that does not terminate the loop and executes the loop forever. It is also called
an indefinite loop or an endless loop. It either produces a continuous output or no output.
An infinite loop is useful for those applications that accept the user input and generate the output continuously until
the user exits from the application manually. In the following situations, this type of loop can be used:
➢ All the operating systems run in an infinite loop as it does not exist after performing some task. It
comes out of an infinite loop only when the user manually shuts down the system.
➢ All the servers run in an infinite loop as the server responds to all the client requests. It comes out
of an indefinite loop only when the administrator shuts down the server manually.
➢ The following are the loop structures through which we will define the infinite loop:
• for loop
• while loop
• do-while loop
• go to statement
• C macros
Example:
#include <stdio.h>
main()
{
int i=0;
while(1)
{
i++;
printf("i is :%d",i);
}
}