CP Unit-II - RIT CSE (1)
CP Unit-II - RIT CSE (1)
CP Unit-II - RIT CSE (1)
UNIT-II
OPERATORS & CONTROL STATEMENTS
Operators, Operator Precedence and Associativity, Evaluating Expressions, Selection &
Making Decisions: Two Way Selection, Multiway Selection, Repetition: Concept of Loop,
Pre-test and Post-test Loops, Initialization and Updating, Event and Counter Controlled Loops,
Loops in C, Other Statements Related to Looping, Looping Applications- Summation, Powers,
Smallest and Largest, Programming Examples.
Operators:
An operator is a symbol that tells the computer to perform certain mathematical or logical
operations. They are used to manipulate data.
Types:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
Arithmetic operators:
Arithmetic operators are used to perform arithmetic operations like addition, subtraction,
multiplication , division and to find the remainder in division. The corresponding symbols are
+(addition or unary plus), - (subtraction or unary minus), *, /,%(modulo division). C does not
have an operator for exponentiation.
Relational operators:
It is used to relate any two or more quantities.
Operator Meaning
< is less than
> is greater than
>= is greater than or equal to
<= is lesser than or equal to
== is equal to
!= is not equal to
Logical operators:
It is used to combine two or more relational expressions.
Operator Meaning
&& logical and
|| logical or
! logical not
Assignment operators:
The conditional expression is evaluated first. If it is non zero then expression1 is evaluated else
expression 2 is evaluated. It is an alternative for If-else statement.
Example:
//program to find the greater of 2 numbers
Bitwise operators:
Manipulates the data at bit level.
Operator Meaning
& bitwise AND
| bitwise OR
^ bitwise EXCLUSIVE OR
<< shift left
>> shift right
~ one‟s complement
Special operators:
Sizeof() - to determine the size of the variable
,(comma) - to link the related expressions together
Example : value=(x=10,y=17,x+y)
. and -> - member selection operator
& and * - pointer operator
Expressions:
Combination of both operand and operator.
Types:
1. Arithmetic
2. Relational
3. Logical
Arithmetic expression:
Operand combined with arithmetic operators are called as arithmetic expressions.
Examples:
a+b
c-d
Types:
1. Integer arithmetic
2. Real arithmetic
3. Mixed arithmetic
Integer arithmetic:
Arithmetic operator combined with integer operands.
CSE, RIT Page 3
Example:
a + b; where a and b are of type integer.
Real arithmetic:
Arithmetic operator combined with real operands.
Example:
a + b where a and b are of type real.
Mixed mode arithmetic:
Expression consists of both integer and real operands.
Example:
a-b where a is of type integer and b is of type real.
An arithmetic expression is a combination of variables, constants and operators. C has no
operator for exponentiation.
Syntax :
variable = expression;
The expression is evaluated first and the results are placed on the variable on the left.
All variables must be assigned values before they are used in an expression.
Spaces before and after an operator is optional. It is used to enhance the readability of the
statement.
Precedence of arithmetic operators:
An arithmetic expression without parenthesis is always evaluated from left to right. An
arithmetic expression has two priorities.
The highest priority - /, *, %
The lowest priority - +, -
During the first pass, the high priority operators are evaluated as they are encountered. During
the second pass, the low priority operators are evaluated.
Example:
It allows the system to evaluate the expression first and then, depending on whether the value is
true or false it transfers the control to the particular statement.
In the above statement if test expression is true then statement block will be executed else
statement block will be skipped.
Example:
// Program to find if a number is divisible by 7 or not
#include<stdio.h>
main()
{
int n;
printf(“Enter the number”);
scanf(“%d”,n);
if ((n%7)== 0)
printf(“The number is divisible by 7\n”);
printf(“Thank you”);
}
If …else statement:
Syntax:
In the above statement if the test expression is true then true block statement, immediately
following the If statement are executed; otherwise the false block statement are executed. In
either case either true block will be executed or false block will be executed but not both.
In the above statement if test condition 1 is true then if test condition 2 will be tested and if it is
also true then statement 1 will be executed. If the second condition is false then statement 2 will
be executed. If the first condition is false then statement 3 will be executed.
Example:
//Program to find the greatest of 3 numbers
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the 3 numbers”);
scanf(“%d %d %d”,&a,&b,&c);
if( a>b)
{
if(a>c)
printf(“a is greater”);
else
printf(“c is greater”);
}
else
{
if(b>c)
printf(“b is greater”);
else
printf(“c is greater”);
}
}
CSE, RIT Page 9
Sample Output;
Enter the 3 numbers 2 3 5
c is greater
Enter the 3 numbers 5 3 2
a is greater
Enter the 3 numbers 2 5 3
b is greater
Else … if ladder:
Syntax:
Else If ladder is used for multi path decisions (ie) a chain of ifs in which the statement associated
with each else is another if. The condition are evaluated from the top to bottom as soon as a true
condition is found, the statement associated with it is executed and the control is transferred to
the statement x. When all the „n‟ conditions become false then the final else containing the
default-statement will be executed.
Example:
//Program to find the grade
CSE, RIT Page 10
#include<stdio.h>
void main()
{
int marks;
char grade;
printf(“Enter marks);
scanf(“%d”,marks);
if(marks>89)
grade=‟O‟;
else if(marks>79)
grade=‟A‟;
else if(marks>69)
grade=‟B‟;
else If(marks>40)
grade=‟C‟;
else
grade=‟F‟;
printf(“Marks is %d and the grade is %c”,marks, grade);
}
Sample Output:
Enter marks 89
Marks is 95 and grade is O
Switch:
Switch statement is used as an alternative for else-if ladder. The major disadvantage with else –if
ladder is that when there are more number of alternatives, the else-if ladder grows. This reduces
the readability of the program. Thus switch can be used as an alternative for else-if ladder for
multi path decisions. The value of any variable or expression is used to select one branch among
several alternatives. The switch tests the value of given variable (or) expression against a list of
case values and when a match is found, a block of statements associated with that case is
executed.
Syntax:
The conditional expression is evaluated first. If it is non zero then expression1 is evaluated else
expression 2 is evaluated. It is an alternative for If-else statement.
Example:
//program to find the greater of 2 numbers
#include<stdio.h>
void main()
{
int a, b, great;
printf(“Enter 2 numbers……”);
scanf(“%d %d”,&a,&b);
great = (a>b) ? a : b;
printf(“the greater value is %d”,great);
}
Sample output:
Enter 2 numbers……10 23
The greater value is 23
Goto:
Example:
/*program to find the sum of 5 positive numbers the program branches unconditionally to x
when it encounters a negative number*/
#include<stdio.h>
void main()
{
int a, sum =0, count=1;
printf(“Enter 5 positive numbers\n”);
x: scanf(“%d”,&a);
if(a<0)
{
printf(“Enter a positive number”);
goto x:
}
else
{
sum = sum + a;
count++;
}
if (count<=5)
goto x;
else
printf(“sum = %d”, sum);
}
Sample output:
Enter 5 positive numbers
1
-1
enter a positive number
1
1
1
1
sum = 5
If the label is placed after the statement „goto‟ then it is called a forward jump. If the label is
placed before the statement „goto‟ then it is called a backward jump.
Example:
#include <stdio.h>
void main()
{
Example:
//program using the do statement
#include<stdio.h>
void main()
{
int i=1;
do
{
printf(“the number is one\n”);
}while(i>1);
printf(“thank you”);
}
Sample output:
The number is one
Thank you
For Loop:
If we know the exact number of times for repetition of block of statements, then for statement
can be used. It is an entry controlled loop.
Syntax:
Examples:
for (i=1; i<=10; i++) (positive increment)
for (i=10; i>0; i--) (negative increment)
for (i=0,j=8; i<=j; i++,j--) (multiple initializations, increment and decrements)
Example :
----------------
----------------
for (i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
------------------
------------------
}
}
-------------------
9. Break statement is used to exit from the loop. It can be used as a part of If statement.
10. To skip a portion of statements in the for loop continue statement can be used in conjunction
with if statement .
Thus an important aspect of the for loop is that all the three sections namely, the initialization,
testing and incrementing are placed in the for statement itself, making it visible for the
programmers from one place itself.
This loop is executed 1000 times without producing any output and thus causes a time delay.
This kind of statement is known as the null statement. (it contains semicolon at the end)
Example:
//program to find the factorial of a number //factorial of a number (n) = 1*2*3*…..*n
#include<stdio.h>
void main()
{
int n, i,fact =1;
printf(“Enter the number…”);
scanf(“%d”,&n);
for(i = 1; i<=n; i++)
fact = fact * i;
printf(“\n The factorial of the given number %d is %d”,n, fact);
}
Sample Output:
Enter the number…3
The factorial of the given number 3 is 6.
Nested loop means a loop statement inside another loop statement. That is why nested loops are
also called as “loop inside loop“.
Example:
#include <stdio.h>
int main()
{
int n;// variable declaration
printf("Enter the value of n :");
// Displaying the n tables.
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.
}
printf("\n");
}
while(condition) {
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Multiplication table\n\n");
int i=1,j;
while(i<=10)
{
j=1;
while(j<=10)
{
printf("%d\t",j*i);
j++;
}
printf("\n");
i++;
}
getch();
return 0;
}
do{
Example
#include <stdio.h>
int main()
{
do
{
printf("I'm from outer do-while loop ");
Output
I'm from outer do-while loop
I'm from inner do-while looput:
There is no rule that a loop must be nested inside its own type. In fact, there can
be any type of loop nested inside any type and to any level.
Syntax:
do{
while(condition) {
for ( initialization; condition; increment ) {
// statement of inside for loop
}
// statement of inside while loop
}
// statement of outer do-while loop
}while(condition);
#include <stdio.h>
CSE, RIT Page 25
int main () {
for( ; ; ) {
printf("This loop will run forever.\n");
}
return 0;
}
while do-while
Variable in condition is initialized before the variable may be initialized before or within
execution of loop. the loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition) do { statement(s); }
{ statement(s); } while(condition);
#include <stdio.h>
int main() {
int n, i, sum = 0;
The above program takes input from the user and stores it in the variable n. Then, for loop is
used to calculate the sum up to n.
#include <stdio.h>
int main() {
int base, exp;
long long result = 1;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exp);
while (exp != 0) {
result *= base;
--exp;
}
printf("Answer = %lld", result);
return 0;
Output
Output