Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Sahyog College of Management Studies, Thane

UNIT-II

Operators:
● An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions.
● C language is rich in built-in operators and provides the following types of
operators.
1. Arithmetic operators
2. Assignment operators
3. Increment / Decrement operators
4. Relational operators
5. Logical operators
6. Conditional operator
7. Bitwise operators
8. Special operators

1. Arithmetic Operator:
The operators that are used to perform arithmetic operations such as Addition,
subtraction, multiplication etc., are called arithmetic operators.

The meaning of all the operators along with examples is shown below:
Arithmetic Description Example Result Priority Associativity
operator

+ Used for addition 10 + 20 30 2 Left to Right

- Used for subtraction 50-10 40 2 Left to Right

* Used for multiplication 10 * 20 200 1 Left to Right

/ Used for division and 10/5 2 1 Left to Right


gives quotient

% Used to get remainder. 4%2 0 1 Left to Right


It is read as modulus
operator or mod

Sahyog College of Management Studies, Thane


Note: Modulus operator denoted by % is used only for integer values and not for
Floating point numbers. This operator returns the remainder after division.

Program to perform arithmetic operations.

2. Assignment Operator:
● An operator which is used to copy the data or result of an expression into a
memory location (which is identified by a variable name), is called an assignment
operator.
● Copying or storing into a memory location is called assigning and hence the name.
The assignment operator is denoted by ‘=’ sign.
● Assignment always happens from right to left.

 Syntax (General rule) for assignment:


Variable = expression/value;

 For Example:
● a = b; /* Store the value of b into a */
● area = L * B; /* Compute the product and store in variable area */
● pi = 3.1416; /* Store the number 3.1416 using the variable pi */

Sahyog College of Management Studies, Thane


Normal mistakes during typing the program:
● a == b; /* Error : “==” is relational operator. This can be used only to compare. Not
for copying */
● a = b * 10 /* Error: No semicolon at the end */
● 10 + b = c; /*Error: Expression not allowed on LHS of assignment operator

3. Shorthand assignment operators :


 Shorthand assignment operators such as +=, - =, *=, /= etc., can be used to assign
values.
 The table below shows shorthand operators, shorthand statements and the
meaning associated with them :
Shorthand Shorthand Meanin Explanation Associativity
operator statement g

+= A+=2 A=A+2 Perform A + 2 and store the result in a Right to Left

-= B-=C B=B-C Perform B - C and store the result in B Right to Left

*= A*=3 A=A*3 Perform A *3 and store the result in A Right to Left

/= A/=B A=A/B Perform A /B and store the result in A Right to Left

%= A%=B A=A%B Perform A %B and store the result in A Right to Left

4. Increment and Decrement operators:


4.1 Increment operator:
● ‘++’ is an increment operator. This is an unary operator. It increments the value of the
operand by one.
● The increment operator is classified into two categories as shown below:

Sahyog College of Management Studies, Thane


a. Pre-increment Operator:
● If the increment operator ++ is placed before (pre) the operand, then the Operator
is called pre-increment.
● As the name indicates, pre-increment means increment before (pre) the operand
value is used. So, the operand value is incremented by 1 first, and then this
incremented value is used.
● Eg: ++a, ++b etc.

Program to show the use of pre-increment operator.

#include<stdio.h>
int main()
{
int a = 20,b;
b=++a;
printf("a=%d\n",a);
printf("b=%d\n",b);
return 0;
}

Output:
a=21
b=21

b. Post-increment Operator:
● If the increment operator ++ is placed after (post) the operand, then the operator
is called post-increment. As the name indicates, post-increment means increment
● after (post) the operand value is used. So, operand value is used first and then the
operand value is incremented by 1.
● Eg: a++, b++ etc.

Sahyog College of Management Studies, Thane


Program to show the use of post-increment operator.
#include<stdio.h>
int main()
{
int a = 20;
int b;
b=a++;
printf("a=%d\n",a);
printf("b=%d\n",b);
return 0;
}

Output:
a=21
b=20

4.2 Decrement operator:


● ‘--’ is a decrement operator. This is an unary operator. It decrements the value of
the operand by one.
● The decrement operator is classified into two categories as shown below:

a. Pre-decrement Operator:
● If the decrement operator - - is placed before (pre) the operand, then the Operator
is called pre-decrement.
● As the name indicates, pre-decrement means decrement before (pre) the operand
value is used. So, the operand value is decremented by 1 first, and then this
decremented value is used.
● Eg: --a, --b etc.
Sahyog College of Management Studies, Thane
Program to show the use of pre-decrement operator.
#include<stdio.h>
int main()
{
int a = 20;
int b;
b=--a
printf("a=%d\n",a); Output:
printf("b=%d\n",b); a=19
return 0; b=19
}

b. Post-increment Operator:
● If the decrement operator -- is placed after (post) the operand, then the operator is
called post-decrement.
● As the name indicates, post-decrement means decrement after (post) the operand
value is used. So, operand value is used first and then the operand value is
decremented by 1.
● Eg: a--, b-- etc.

Program to show the use of post-increment operator.


#include<stdio.h>
int main()
{
int a = 20;
int b;
b=a--;
printf("a=%d\n",a); Output:
printf("b=%d\n",b); a=19
return 0; b=20
}

Sahyog College of Management Studies, Thane


5. Relational operators:
● The relational operators, also called comparison operators, are used to compare
two operands. They are used to find the relationship between two operands and
hence are called relational operators.
● The two operands may be constants, variables or expressions. The relationship
between these two operands results in true (1) or false(0).
The relational operators and the meaning associated with them are shown in the following
table:

Operator Description Example Priority Associativity

< Less than 10 < 20 1 Left to right

<= Less than or equal 12 <= 18 1 Left to right

> Greater than 15>10 1 Left to right

>= Greater than or equal 15>=3 1 Left to right

== Equal to 10 ==9 2 Left to right

!= Not equal to 5 !=2 2 Left to right

Program to show the use of relational operator.

Sahyog College of Management Studies, Thane


6. Logical Operator:
● As we have logic gates such as AND, OR and NOT whose output is 1 or 0, we also have logical
operators.
● Logical Operators are used to combine 2 more relational expressions.
● After evaluation, expression consisting of logical operators results in either true (1) or false (0)
and hence such expressions are called logical expressions.

Logical operators and the meaning associated with them are shown in the following table:

Oper Description Example Priority Associativity


ator

&& Logical And. If all the (10<20) && (23>5)=1 2 Left to right
conditions are true then
(21<43) &&(5==5)=0
result will be true

|| Logical OR. If any one of the (18>=17) || (15<10)=1 3 Left to right


condition is true then result
(10!=10) &&(5>6)=0
will be true

! Logical NOT. It is used to 1 Left to right


reverse the logical state of its
!(10>23)=1
operand. If a condition is
true, then Logical NOT !( (21<43)&&(5==5))=0
operator will make it false
and if a condition is false
then Logical NOT operator
will make it false true.

a. Logical AND: The result of logical ‘AND’ operator denoted by && is true if and only
if both the operands are evaluated to true. If one of the operands is evaluated to
false.

Operand 1 Operand 2 Result

True(1) True(1) True(1)

True(1) False(0) False(0)

False(0) True(1) False(0)

False(0) False(0) False(0)

Sahyog College of Management Studies, Thane


b. Logical OR: The result of logical ‘OR’ operator denoted by || is true if and only if at
least one of the operands is evaluated to true. If both the operands are evaluated to
false, the result is false.

Operand 1 Operand 2 Result

True(1) True(1) True(1)

True(1) False(0) True(1)

False(0) True(1) True(1)

False(0) False(0) False(0)

c. Logical Not: The logical ‘NOT’ denoted by ! can be true or false. The result is true if
the operand is false and the result is false if the operand is true.

Operand 1 Result

True(1) False(0)

False(0) True(1)

1. Program to show the use of logical operator

Sahyog College of Management Studies, Thane


7. Conditional Operator:
● The conditional operator is also known as a ternary operator.
● As conditional operator works on three operands, so it is also known as the ternary operator.
● It is represented by two symbols, i.e. '?' and ':'.
● The behavior of the conditional operator is similar to the 'if-else' statement.
 Syntax1:
Expression1? expression2: expression3;
 Syntax2:
Variable=Expression1? expression2: expression3;

Meaning of the above syntax.


● In the above syntax, the expression1 is a Boolean condition that can be either true or
false(Yes/No) value.
● If the expression1 results into a true value, then the expression2 will execute.
● If the expression1 returns false value then the expression3 will execute.

Example1:
Suppose, A=10, B=20
(A<B) ? printf(“A is the smallest number”) : printf(“B is the smallest number”);
Output:
A is the smallest number

Example2:
Suppose, A=30, B=12
C= (A<B) ? A : B;
printf(“Smallest no is %d”,C);

Output:
Smallest no is 12

Sahyog College of Management Studies, Thane


Program to display smallest number between 2 numbers using conditional operator:

Program to display smallest number between 2 numbers using conditional operator:

Program to check eligibility for voting using conditional operator:

Sahyog College of Management Studies, Thane


8. Bit-wise Operator:
● In arithmetic-logic unit (which is within the CPU), mathematical operations like: addition,
subtraction, multiplication and division are done in bit-level.
● To perform bit-level operations in C programming, bitwise operators are used.

The bit-wise operators and the meaning associated with them are shown in the following table:

Operator Description Example Priority Associativity

& Bitwise AND 12 & 15 3 Left to right

| Bitwise OR 5|2 5 Left to right

^ Bitwise XOR 11 ^ 10 4 Left to right

~ Bitwise NOT ~10 1 Left to right

<< Bitwise Left Shift 10<<1 2 Left to right

>> Bitwise Right Shift 12>>1 2 Left to right

Bitwise AND (&):


● Bitwise AND operator is denoted by the single ampersand sign (&). Two integer operands are
written on both sides of the (&) operator.
● The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an
operand is 0, the result of corresponding bit is evaluated to 0.
Perform bitwise AND operation on two integers 12 and 25.

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

Bit Operation of 12 and 25


00001100
& 00011001
___________
00001000 = 8 (In decimal)

Program for bitwise and (&) operator:

#include <stdio.h>
int main()
{
int a=12,b=25,c;

Sahyog College of Management Studies, Thane


c=a&b;
printf("12 & 25=%d",c);
return 0;
}
Output:
12 & 25=8

Bitwise OR ( | ):
● The bitwise OR operator is represented by a single vertical sign (|). Two integer operands are
written on both sides of the (|) symbol.
● The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1.

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25

00001100
| 00011001
__________
00011101 = 29 (In decimal)

Program for bitwise or (|) operator:


#include <stdio.h>
int main()
{
int a=12,b=25,c;
c=a | b;
printf("12 | 25=%d",c);
return 0;
}
Output:
12 | 25=29

Bitwise XOR (^):


● Bitwise exclusive OR operator is denoted by (^) symbol. Two operands are written on both sides
of the exclusive OR operator.
● The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite
(Different).

Sahyog College of Management Studies, Thane


12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25


00001100
^ 00011001
__________
00010101 = 21 (In decimal)

Program for bitwise xor (^) operator:

#include <stdio.h>
int main()
{
int a=12,b=25,c;
c=a^b;
printf("12^25=%d",c);
return 0;
}
Output:
12 ^ 25=21

X Y X&Y X|Y X ^Y ~X

0 0 0 0 0 1

0 1 0 1 1 1

1 0 0 1 1 0

1 1 1 1 0 0

Bitwise Left Shift (<< ):


● Left shift operator shifts all bits towards left by a certain number of specified bits. The bit
positions that have been shifted by the left shift operator are filled with 0.
● The symbol of the left shift operator is <<.
Syntax:
Operand << n;

Sahyog College of Management Studies, Thane


Where,
● Operand is an integer expression on which we apply the left-shift operation.
● n is the number of bits to be shifted.(value of n can be any number)
13 = 0 0 0 0 1 1 0 1
Left Shift 13 by 1 bit

Shifted bit filled with zero


Discarded

0 0 0 1 1 0 1 0 = 26
So,
13<<1=26

Program for Left Shift Operator:

#include <stdio.h>
int main()
{
int a=13,b;
b=a<<1;
printf("13<<1=%d",b);
return 0;
}
Output:
13<<1=26

Bitwise Right Shift (>>):


● Right shift operator shifts all bits towards right by a certain number of specified bits. The bit
positions that have been shifted by the right shift operator are filled with 0.
● The symbol of the right shift operator is >>.
Syntax:
Operand >> n;

Where,
● Operand is an integer expression on which we apply the right-shift operation.
● n is the number of bits to be shifted.(value of n can be any number)

Sahyog College of Management Studies, Thane


20 = 0 0 0 0 1 0 1 0 0
Right Shift 20 by 1 bit

Discarded

Shifted bit filled with zero


0 0 0 0 1 0 1 0 = 10
So,
20>>1=10

Program for right Shift Operator:

#include <stdio.h>
int main()
{
int a=20,b;
b=a>>1;
printf("20>>1=%d",b);
return 0;
}
Output:
20>>1=10

9. Special Operators:

Operator Description Example

Sizeof Sizeof operator Sizeof(int)

& Ampersand (address of) &operand


Operator

Sizeof operator:
● This operator is used to find the number of bytes occupied by a variable or a datatype in the
computer memory.
The syntax is shown below:
sizeof(variable_name / Data_type);
Example:
char ch;
sizeof(int); //4 byte
sizeof(ch); // 1 byte

Sahyog College of Management Studies, Thane


Address of Operator (&):
● The "Address Of" Operator denoted by the ampersand character (&), & is a unary operator,
which returns the address of a variable.

Example:
int a;
&a ; //returns address of a

Program to display address of a variable

#include <stdio.h>
int main()
{
int a=20;
printf("Address of a in decimal :%d",&a);
printf("\nAddress of a in Hexadecimal :%X",&a);
return 0;
}

Output:
Address of a in decimal :6487580
Address of a in Hexadecimal :62FE1C

Note:

1. Operators with 1 operand is known as unary operator.


&,++,--,~ are the unary operators.
2. Operators with 2 operands is known as binary operator.
+,-,*,/ are the binary operators.
3. Operators with 3 operands is known as ternary operator.
? : is ternary operator.

Sahyog College of Management Studies, Thane


Hierarchy of operators:

Precedence operators:
● In C language, each operator is associated with a priority value. Based on the priority, the
expressions are evaluated. A part of the expression with priority value 1 is evaluated first; part of
the expression with priority value 2 is evaluated next and so on.
● These priority values that are associated with various operators are called
Precedence of operators.

Associativity Operators:
● If all the operators in an expression have equal priority, then the direction order chosen (left to
right evaluation or right to left evaluation) to evaluate an expression is called associativity of
operators.
Associativity Operators are classified into 2 categories:

1. Left Associative:
● In an expression, if there are two or more operators having the same priority and are evaluated
from left-to-right, then the operators are called Left to Right associative operators.
For example,
8 + 4 + 3 = 15
Sahyog College of Management Studies, Thane
8-4 -3=1
In the above example + and – both are having equal priority so in this case according to associativity
this expression will be evaluated and associativity of + and – and left to right. So above expression
will execute from left to right.

2. Right Associative:
● In an expression, if there are two or more operators having the same priority and are evaluated
from right-to-left, then the operators are called Right to Left associative operators.
For Example,
i = j = k = 10;
Above expression will execute from right to left because 3 times = operator is used and associativity
of = is right to left.

Control Flow/Control Statement:


 Control statements enable us to specify the flow of program control — that is, the order in
which the instructions in a program must be executed.
 They make it possible to make decisions, to perform tasks repeatedly or to jump from one
section of code to another.

Types of Control Statements


1. Conditional / Decision making statements
2. looping statements
3. Jump statements

1. Decision making statements:


 The conditional statements (also known as decision control structures) such as if,
if else, switch, etc. are used for decision-making purposes in C/C++ programs.
 They are also known as Decision-Making Statements and are used to evaluate one
or more conditions and make the decision whether to execute a set of statements
or not. These decision-making statements in programming languages decide the
direction of the flow of program execution.

Sahyog College of Management Studies, Thane


1. IF Statement
The if statement checks the given condition. If the condition evaluates to be true then the
block of code/statements will execute otherwise not.

Syntax:
if(condition)
{
//code to be executed
}

Program:
// C program to illustrate If statement
#include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}

Sahyog College of Management Studies, Thane


Output
Enter a number:4
4 is even number
enter a number:5

2. IF – else Statement
The if statement evaluates the code if the condition is true but what if the condition is
not true, here comes the else statement. It tells the code what to do when the if
condition is false.

Syntax:
if(condition)
{
// code if condition is true
}
else
{
// code if condition is false
}

Program:
check whether a number is even or odd using if-else statement in C language

#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}
output
enter a number:4
4 is even number
enter a number:5
5 is odd number
Sahyog College of Management Studies, Thane
3. If – else – if ladder Statement
The if-else-if ladder statement executes one condition from multiple statements. The execution
starts from top and checked for each if condition. The statement of if block will be executed
which evaluates to be true. If none of the if condition evaluates to be true then the last else
block is evaluated.

Syntax:
if(condition1)
{
// code to be executed if condition1 is true
}
else if(condition2)
{
// code to be executed if condition2 is true
}

else if(condition3)
{
// code to be executed if condition3 is true
}
...
else
{
// code to be executed if all the conditions are false
}

Program to calculate the grade of the student according to the


specified marks.
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}

Sahyog College of Management Studies, Thane


else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are fail ...");
}
}

Output

Enter your marks?10


Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...

4. Nested – If Statement
if statement inside an if statement is known as nested if. if statement in this case is the target of
another if or else statement. When more than one condition needs to be true and one of the
condition is the sub-condition of parent condition, nested if can be used.

Syntax:

if (condition1)
{
// code to be executed
// if condition2 is true
if (condition2)
{
// code to be executed
// if condition2 is true
}
}

5. Switch Statement
Switch statement is an alternative to long if-else-if ladders. The expression is checked for
different cases and the one match is executed. break statement is used to move out of the
switch. If the break is not used, the control will flow to all cases below it until break is found or
switch comes to an end. There is default case (optional) at the end of switch, if none of the case
matches then default case is executed.

Sahyog College of Management Studies, Thane


Rules for switch statement in C language
1) The switch expression must be of an integer or character type.
2) The case value must be an integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional.
5) If there is no break statement found in the case, all the cases will be executed present after the
matched case. It is known as fall through the state of C switch statement.

Syntax:

switch (expression)
{
case value1: // statement; break;
case value2: // statement ; break;
.
.
.
case valueN: // statement;break;
default: // default statement sequence
}

Program for switch


#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:

Sahyog College of Management Studies, Thane


printf("number is not equal to 10, 50 or 100");
}
return 0;
}

Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50

Looping Statement:
Loops in programming are used to repeat a block of code until the specified condition is met. A loop
statement allows programmers to execute a statement or group of statements multiple times without
repetition of code.

Types of C Loops

There are three types of loops in C language that is given below:

1. for
2. while
3. do-while

Sahyog College of Management Studies, Thane


1. for loop in C
 The for loop is used in the case where we need to execute some part of the code until the given
condition is satisfied.
 The for loop is also called as a per-tested or entry controlled loop.
 It is better to use for loop if the number of iteration is known in advance.

The syntax of for loop in c language is given below:


for(initialization;condition;incr/decr)
{
//code to be executed
}

for loop Examples


Let's see the simple program of for loop that prints 1to 10 .
#include<stdio.h>
int main()
{
int i=0;
for(i=1;i<=10;i++)
{
printf("%d ",i);
}
return 0;
}

Output: 1 2 3 4 5 6 7 8 9 10

C Program: Print table for the given number using C for loop

#include<stdio.h>
int main(){
int i=1,number,m;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++)
{
m=number * i;
printf("%d * %d = %d \n", number , i m);
}
return 0;
}

Sahyog College of Management Studies, Thane


2. while loop in C
 The while loop in c is to be used in the scenario where we don't know the number of
iterations in advance.
 The block of statements is executed in the while loop until the condition specified in the
while loop is satisfied. It is also called a pre-tested or entry controlled loop.

The syntax of while loop in c language is given below:


Initialization;
while(condition)
{
//code to be executed
Incr/decr;
}

Example of the while loop in C language


Let's see the simple program of while loop that prints table of 1.

#include<stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d ",i);
i++;
}
return 0;
}

Output:
1 2 3 4 5 6 7 8 9 10

Infinitive while loop in C


If the expression passed in while loop results in any non-zero value then the loop will run the
infinite number of times.

Example:
while(1)
{
Printf(“hello”);
}

Above loop will iterate infinite times.

Sahyog College of Management Studies, Thane


3. do-while
 The do-while loop continues until a given condition satisfies. It is also called post tested or
exit controlled loop.
 It is used when it is necessary to execute the loop at least once (mostly menu driven
programs).

The syntax of do-while loop in c language is given below:

do
{
//code to be executed
}
while(condition);

Example:

#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;

/* do loop execution */
do
{
printf("value of a: %d\n", a);
a = a + 1;
}
while( a < 20 );
return 0;
}

Output:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Sahyog College of Management Studies, Thane


Jump statement
Jump Statement makes the control jump to another section of the program unconditionally when
encountered. Jump statements are used to interrupt the normal flow of program.

C programming language provides the following jumping statements.

a) break
b) continue
c) goto

a) break:
 A break statement is used to terminate the execution of the rest of the block where it is present
and takes the control out of the block.
 It is mostly used in loops and switch-case to bypass the rest of the statement and take the
control to the end of the loop.
 When a break statement is encountered inside the switch case statement, the execution control
moves out of the switch statement directly.
 When the break statement is encountered inside the looping statement, the execution control
moves out of the looping statements.
 Another point to be taken into consideration is that break statement when used in nested loops
only terminates the inner loop where it is used and not any of the outer loops.

Program for break statement:


#include <stdio.h>
int main ()
{
int a = 10;
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
if( a > 15)
{
break;
}}
return 0; }

Output:
value of a: 10
value of a: 11
value of a: 12

Sahyog College of Management Studies, Thane


value of a: 13
value of a: 14
value of a: 15

b) continue:
 The continue statement skips the current iteration of the loop and continues with the next
iteration.
 continue statements are only used in loops.

Example: continue statement inside for loop


#include <stdio.h>
int main()
{
int j;
for ( j=0; j<=8; j++)
{
if (j==4)
{
continue;
}
printf("%d ", j);
}
return 0; }

Output:
01235678

Difference between break and continue


break continue
When break is executed the statements When continue statement is executed the
following break are skipped and causes statement following continue are skipped and
the loop to be terminated. cause the loop to be continued with the next
iteration
It can be used in switch and loops It is only used in loops

Sahyog College of Management Studies, Thane


E.g. E.g.

for(i=1;i<=5;i++) for(i=1;i<=5;i++)
{ {
if(i==2) if(i==3)
break; continue;
printf(“%d”,i); printf(“%d”,i);
return 0; return 0;
} }

Output: Output:
1 2 1 2 4 5

c) goto:
 The goto statement allows us to transfer control of the program to the specified label.
 The label is an identifier. When the goto statement is encountered, the control of the program
jumps to label: and starts executing the code.

Program to display 5 times hello using goto statement.

#include <stdio.h>
int main ()
{
int n=1;
start:
printf("Hello ");
n++;
if(n<=5)
{
goto start;
}
return 0;
}

Output:
Hello Hello Hello Hello Hello

Sahyog College of Management Studies, Thane


Program to display 1 to 10 using goto statement.
#include <stdio.h>
int main ()
{
int n=1;
start:
printf("%d ",n);
n++;
if(n<=10)
{
goto start;
}
return 0;
}

Output:
1 2 3 4 5 6 7 8 9 10

NESTED LOOPS:
 Loop inside another loop is called nesting of loop.
 Nested loops are basically used when we want to generate output in the form row and column i.e.
in tabular form.
1. nested for loop
2. nested while loop
3. nested do while loop

1. nested for loop:


Syntax:
The syntax for a nested for loop statement in C is as follows −
for(init; condition; increment) outer loop
{
for(init; condition; increment ) inner loop
{
statements of inner loop;
}
statements of outer loop;
}

Sahyog College of Management Studies, Thane


Program:
#include<stdio.h>
int main()
{
int i,j;
int n=1;
for(i=1;i<=5;i++)
{

for(j=1;j<=4;j++)
{
printf("%d ",i);
}
printf("\n");
}
return 0;
}

Output:
1111
2222
3333
4444
5555

Note: outer loop is always used for rows and inner loop is used for columns.

2. nested while loop:


while(condition) outer loop
{
while(condition) inner loop
{
statements of inner loop;
} statements of outer loop;

Program:
#include<stdio.h>
int main()
{
int i=1,j;

Sahyog College of Management Studies, Thane


while(i<=4)
{
j=1;
while(j<=3)
{
printf("%d ",j);
j++;
}
i++;
printf("\n");
}
return 0;
}

Output:
123
123
123
12 3

3. nested do while loop:


Syntax:
do
{
statement;
do
{
Statements of inner loop;
}while( condition );

statements of outer loop;

} while(condition);

Program:
#include<stdio.h>
int main()
{
int i=1,j;
do
{
Sahyog College of Management Studies, Thane
j=1;
do
{
printf("%2d ",i*j);
j++;
}while(j<=10);

i++;
printf("\n");
}while(i<=4);
return 0;
}

Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40

Note: 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. For e.g. in for loop we can write
while loop.

************

Sahyog College of Management Studies, Thane

You might also like