Documente Academic
Documente Profesional
Documente Cultură
ADMN
2014-17
*
/
%
Meaning
Addition or Unary Plus
Subtraction or Unary Minus
Multiplication
Division
Modulus Operator
(Table: 2.1)
Examples of arithmetic operators are
x+y
x-y
-x + y
a*b+c
-a * b
etc.,
Here a, b, c, x, y are known as operands. The modulus operator is a special operator in C
language which evaluates the remainder of the operands after division. It acts only on integer
operands.
Dept.of Computer Science And Applications, SJCET, Palai
Page 34
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
(Table: 2.2)
It is required to compare the marks of 2 students, salary of 2 persons; we can compare them
using relational operators. A simple relational expression contains only one relational operator and
takes the following form.
exp1 relational operator exp2
Where exp1 and exp2 are expressions, which may be simple constants, variables or
combination of them. Given below is a list of examples of relational expressions and evaluated
values.
6.5<=25
TRUE
-65>0
FALSE
10 < 7 + 5
TRUE
The value of a relational expression will be 0 if the expression is false and it will be 1 if the
expression is true. Relational expressions are used in decision making statements of C language such
as if, while and for statements to decide the course of action of a running program.
Meaning
Logical AND
Logical OR
Logical NOT
(Table: 2.3)
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
MODULE 2
MCA- 105 Structured Programming in C
a=a+1
a=a1
a = a * (n+1)
a = a / (n+1)
a=a%b
ADMN
2014-17
a += 1
a -= 1
a *= (n+1)
a /= (n+1)
a %= b
(Table: 2.4)
EXAMPLE
#define N 100
#define A 2
main()
{
int a;
a = A;
while (a < N)
{
printf(%d \n,a);
a *= a; //shorthand form of a = a * a
}
}
OUTPUT
2
4
16
If 2 operands in an assignment expression are of different types, the value of expression on
right hand side is automatically converted to the type of identifier on left. It is also possible to have
multiple assignments in a single line.
i=j=2;
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
The increment operator ++ adds the value 1 to the current value of operand and the decrement
operator subtracts the value 1 from the current value of operand. ++variable name and variable
name++ mean the same thing when they form statements independently, they behave differently
when they are used in expression on the right hand side of an assignment statement.
Consider the following
m = 5;
y = ++m; (prefix)
In this case the value of y and m would be 6
Suppose if we rewrite the above statement as
m = 5;
y = m++; (post fix)
Then the value of y will be 5 and that of m will be 6. A prefix operator first adds 1 to the
operand and then the result is assigned to the variable on the left. On the other hand, a postfix
operator first assigns the value to the variable on the left and then increments the operand.
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
01001000 &
10111000 =
---------------00001000
01001000 |
10111000 =
---------------11111000
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
The bitwise-exclusive-OR operator compares each bit of its first operand to the corresponding
bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1.
Otherwise, the corresponding result bit is set to 0.
EXAMPLE:
01110010 ^
10101010
--------------11011000
x=
1001 0110 1100 1011
x<<4= 0110 1100 1011 0000
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
If one operand is float, the other will be converted to float and result will be float.
4.
If one of the operand is unsigned long int, the other will be converted into unsigned long int
and result will be unsigned long int.
5.
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
6.
a). If unsigned int can be converted to long int, then unsigned int operand will
be
converted as such and the result will be long int.
b). Else both operands will be converted to unsigned long int and the result will be
unsigned long int.
If one of the operand is long int, the other will be converted to long int and the result will be
long int.
7.
If one operand is unsigned int the other will be converted to unsigned int and the
result will be unsigned int.
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
complex expressions. The parentheses must be present, even if there are no arguments, A function
that returns a data item can appear anywhere within an expression in place of a constant or an
identifier. A function that carries out operations on data items but does not return anything can be
accessed simply by writing the function name, since this type of function reference constitutes an
expression statement.
In order to use a library function it may be necessary to include certain specific information
within the main portion of the program. This information is generally stored in special files supplied
with the compiler. Thus, the required information can be obtained simply by accessing these special
files. This is accomplished with the preprocessor statement.
# include <filename>
The list of some commonly used library functions are:
abs(i) to determine absolute value of i.
exp(i) raise e to the power i.
log(d) determine natural logarithm of d.
pow(d1,d2) returns d1 raised to the d2 power
putchar(c) send a character to the standard output device
sqrt(d) return the square root of d.
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
The file name stdio.h is an abbreviation for standard input -output header file. The instruction
#include <stdio.h> tells the compiler to search for a file named stdio.h and place its contents at this
point in the program. The contents of the header file become part of the source code when it is
compiled.
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
putchar (in);
}
C supports testing of the character keyed in by the user by including the file ctype.h & the
functions which can be used after including this file are:
isalnum(c) Is c an alphanumeric character?
isalpha(c) Is c an alphabetic character?
isdigit(c) Is c a digit?
islower(c)Is c a lower case character?
isprint(c)Is c a printable character?
ispunct(c)Is c a punctuation mark?
isspace(c)Is c a white space character?
isupper(c) Is c a upper case character?
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
Formatted input refers to an input data that has been arranged in a particular format. For
example, consider the following data:
15.73 123 John
This line contains three pieces of data, arranged in a particular form. Such data has to be read
conforming to the format of its appearance. For example, the first part of the data should be read into
a variable float, the second into int, and the third part into char. This is possible in C using the scanf
function.
The general form of scanf is
scanf("control string", arg1, arg2,......argn);
The control string specifies the field format in which the data is to be entered and the
arguments arg1, arg2,.....argn specify the address of locations where the data is stored . Control
string and arguments are separated by commas.
Control string consist of:
scanf(%2d %5d,&num1,&num2);
50
31246
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
input data items should be separated by spaces tabs or newlines. When scanf searches input
data line for a value to be read, it will always bypass white space characters.
EXAMPLE:
scanf(%d%*d%d,&a,&b);
let the input data be 123 456 789
Here 123 is assigned to a
456 is skipped(because of *)
789 to b
Inputting Real Numbers
Unlike integers numbers , the field width of real numbers is not to be specified and therefore
scanf reads real numbers using the simple specification %f for both the notations, namely, decimal
point notation and exponential notation. For example, the statement
scanf("%f %f %f , &x, &y , &z);
with the input data
472.34 43.21E-1 678
will assign the value 472.34 to x, 4.321 to y, and 678.0 to z. The input field specifications may
be separated by any arbitrary blank spaces.
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
The specification %[^characters] does exactly the reverse. That is, the characters specified
after the (^) are not permitted in the input string. The reading of the string will be terminated at the
encounter of one of these characters.
Reading Mixed Data Types
It is possible to use one scanf statement to input a data line containing mixed mode data. In
such cases care should be given to ensure that input data items match the control specifications in
order and type. When an attempt is made to read an item that doesnt match the type expected the
scanf function doesnt read any further and immediately returns the value read.
EXAMPLE;
scanf(%d%c%f%s,&count,&code,&ratio,name);
will read data 15 p 1.24 xyz
correctly and assign the values to variables in the order in which they appear.
scanf Format Codes:
%c reads a single character
%d read a decimal integer
%e read a floating point value
%f read a floating point value
%g read a floating point value
%h read a short integer
%i read a decimal, hexadecimal, or octal integer
%o read an octal integer
%s read a string
%u read an unsigned decimal integer
%x read a hexadecimal integer
%[..] read a string of word(s)
The following letters may be used as prefixes for certain conversion character
h for short integers
l for long integers or doubles
L for long double
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
printf(%6d,9876);
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
We can also display a real number in exponential notation by using the specification:
%w.pe
The field width w should satisfy the condition w>=p+7
The display takes the form: [-]m.nnnne[+-]xx
EXAMPLE: let y=-98.7654
printf(%11.4e,y) will display
9 .
8 7 6 5 e + 0 1
Specifier Meaning
%c Print a character
%d Print a Integer
%i Print a Integer
%e Print float value in exponential form.
%f Print float value
%g Print using %e or %f whichever is smaller
%o Print actual value
%s Print a string
%x Print a hexadecimal integer (Unsigned) using lower case a F
%X Print a hexadecimal integer (Unsigned) using upper case A
F
%a Print a unsigned integer.
%p Print a pointer value
%hx hex short
%lo octal long
%ld long
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
4.goto statement.
True
False
Page 53
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
Test exp
statement-block
False
statement-x
next statement
fig 2.2
EXAMPLE:
-----------------------if (category==sports)
{
marks=marks+bonus_marks;
}
printf(%f,marks);
.
2.5.1.2 IF ELSE STATEMENT
if (test expression)
{
True-block statement ;
}
else
{
False-block statement;
}
statement-x;
If the test expression is true then the true-block statements immediately following the if
statements are executed. Otherwise the false-block statements are executed. In either case,either true
-block or false-block will be executed, not both. In both cases, the control is transferred to statementx.
EXAMPLE:
..
if(code==1)
boy=boy+1;
else
girl=girl+1;
MODULE 2
MCA- 105 Structured Programming in C
true
ADMN
2014-17
false
test exp
true-block stmts
false-block stmts
statement x
fig 2.3
2.5.1.3 NESTING OF IF ELSE STATEMENTS
When a series of decisions are involved, we may have to use more than one if else
statement in nested form as follows:
if(test condition1)
{
if (test condition 2)
{
Statement 1;
}
else
{
Statement 2;
}
}
else
{
Statement 3;
}
statement x;
EXAMPLE:
.
if(sex ==female)
{
if(balance>5000)
bonus=.05*balance;
else
bonus=.02*balance;
}
else
{
bonus=.01*balance;
Dept.of Computer Science And Applications, SJCET, Palai
Page 55
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
}
balance=balance+ bonus;
..
The logic of execution is in figure. If the condition1 is false, the statement3 will be executed;
otherwise it continues to perform the second test. If the condition 2 is true, the statement1 will be
evaluated; otherwise the statement2 will be evaluated and then the control is transferred to the
statement x.
Entry
False
Test
Condition
1
Fals
e
Stateme
nt3
Stateme
nt2
True
Test
Condition
2
Stateme
nt1
True
Statement
x
Next
Statement
(Fig: 2.4)
2.5.1.4 The ELSE IF LADDER
There is another way of putting ifs together when multipath decisions are involved. A
multipath decision is a chain of ifs in which the statement associated with each else is an if. It takes
the following general form:
if (condition 1)
{
Statement 1;
}
else if(condition 2)
{
Statement 2;
Dept.of Computer Science And Applications, SJCET, Palai
Page 56
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
.
}
else if(condition n)
{
Statement n;
}
else
default statement;
Statement x;
The conditions are evaluated from the top (of the ladder), downwards. As soon as a true
condition is found, the statement associated with it is executed and the control is transferred to the
statement-x (skipping the rest of the ladder). When all the n conditions become false, then the final
else containing the default-statement will be executed.
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
entry
true
cond1
stmt 1
false
false
true
cond2
true
stmt 2
false
cond3
stmt 3
true
false
condn
stmt n
default stmt
stmt -x
next stmt
fig 2.5
2.5.2.SWITCH STATEMENT
When one of the many alternatives is to be selected, we can use an if statement to control the
selection. However the complexity of such a program increases when the number of alternative
increases. The program becomes difficult to read and follow. In C there is a built-in multi way
decision statement known as switch. The switch tests the value of a variable(or expression) against a
list of case values and when a match is found ,block of statements associated with that case is
executed.
Syntax :switch(expression)
{
Dept.of Computer Science And Applications, SJCET, Palai
Page 58
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
case value-1:
block-1
break;
case value-2:
block-2
break;
------------------default:
default-block
break;
}
statement x;
The expression is an integer expression or characters.Value-1,value-2are constants or
constant expressions and are known as case labels. Each of these values should be unique within a
switch statement.block-1,block-2.. are statement lists and may contain zero or more statements.
There is no need to put braces around these blocks. Note that case labels end with a colon(:).
When the switch is executed, the value of the expression is compared against the values of
Value-1,value-2If a case is found whose values match with the value of the expression, then the
block of statements that follows the case are executed.
The break statement at the end of each block signals the end of a particular case and causes
an exit from the switch statement, transferring the control to the statement following the switch.The
default is an optional case. When present, it will be executed if the value of the expression doesnt
match with any of the case values. If not present no action takes place if all matches fail and the
control goes to the statement-x.
EXAMPLE:
switch(c)
{
case + : printf(Enter the values for a & b\n);
scanf(%d %d,&a,&b);
printf(a + b = %d\n,a+b);
break;
case - : printf(Enter the values for a & b\n);
scanf(%d %d,&a,&b);
printf(a - b = %d\n,a-b);
break;
Dept.of Computer Science And Applications, SJCET, Palai
Page 59
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
goto abc;
Exit from
loop
--------abc:
------}
stop:
In its general form, the goto statement is written as
goto label;
Where label is an identifier that is used to label the target statement to which control will be
transferred.
Control may be transferred to any other statement within the program. The target statement
must be labeled, and the label must be followed by a colon. Thus the target statement will appear as
label: statement
Dept.of Computer Science And Applications, SJCET, Palai
Page 61
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
Each labeled statement within the program must have a unique label; i.e., no two statements can have
the same label.
goto label;
label:
statement;
--------------------------goto label;
--------------------------label:
statement
Forward jump
Backward jump
If the label: is before the statement goto label; a loop will be formed and some statements will
be executed repeatedly. Such a jump is known as a backward jump. On the other hand, if the label: is
placed after the goto label; some statements will be skipped and the jump is known as a forward
jump.
A goto is often used at the end of a program to direct the control to go to the input statement,to
read further data.
Points to be remembered while using the goto statement:
The use of goto statement in a structured programming language like C should be
avoided. Use if and only if it is unavoidable
The goto statement may create an infinite loop where the computer enters a
permanent loop. The careful and cautious design would resolve such situations.
A program may contain any number of goto statements.
No two statements can have the same label.
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
digit=number%10;
rev=rev*10+digit;
number/=10;
if (number>0)
goto START;
printf(Input number=%5d\n,temp_num);
printf(Reversw number=%5d\n,rev);
}
OUTPUT
Enter a number
9876
Input number=9876
Reverse number=6789
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
The C language provides for three constructs for performing loop operations.They are:
Based on the nature of control variable and the kind of value assigned to it for testing the
control expression, the loops may be classified into two general categories:
Counter-controlled loops
Sentinel-controlled loops
When we know in advance how many times a loop should be executed, we use a
counter controlled loop.We use a control variable called counter. The counter must be initialized,
tested and updated properly for desired loop operations.The no;of times the loop should be executed
may be constant or a variable that is assigned value. It is also called definite repetition loop
.EXAMPLE:
sum=0;
n=1;
while(n<=10)
{
sum=sum+n*n;
n=n+1;
}
Here this loop will be executed 10 times. The variable n is called counter or control variable.
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
In a Sentinel-Controlled loop, a special value called a sentinel value is used to change the loop
control expression from true to false. For example, when reading data we may indicate the "end of
data" by a special value, like -1 and 999. The control variable is called sentinel variable. A sentinelControlled loop is often called indefinite repetition loop because the number of repetitions is not
known before the loop begins executing
char character=;
while(character!=y)
{
character = getchar();
putchar(c);
}
Here the loop is executed as long as the key y is pressed. When Y is pressed the condition
becomes false the loop terminates and control transfers to the statement following the loop. Here the
value Y is called sentinel value and the variable character is called the sentinel variable.
-------------------------
Initialization
Test condition
{
printf("%d",x);
-------------
x = x+1;
}
Dept.of Computer Science And Applications, SJCET, Palai
Page 65
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
PROGRAM:
# include<stdio.h>
main()
{
int count,n;
float x,y;
printf(Enter the values of x and n:);
scanf(%f %d, &x, &n);
y= 1.0;
count = 1;
/* LOOP BEGINS*/
while(count<= n)
{
y = y * n;
count ++;
}
/*End of loop*/
printf( x = %f ; n = %d ; to power n = %f n , x , n, y);
}
2.6 .2 DO STATEMENT
The do while loop is also a kind of loop, which is similar to the while loop in contrast to while
loop, the do while loop tests at the bottom of the loop after executing the body of the loop. Do is an
exit-controlled loop. Since the body of the loop is executed first and then the loop condition is
checked we can be assured that the body of the loop is executed at least once.
The syntax of the do while loop is:
do
{
statement;
}
while(expression);
Here the statement is executed, then expression is evaluated. If the condition expression is true
then the body is executed again and this process continues till the conditional expression becomes
false. When the expression becomes false the loop terminates. while loop says "Loop while the
condition is true, and execute this block of code", a do..while loop says "Execute this block of code,
and then continue to loop while the condition is true".
EXAMPLE:
void main()
{
int c;
Dept.of Computer Science And Applications, SJCET, Palai
Page 66
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
do
{
printf(enter number);
scanf(%d,&c);
printf(%d\n,c);
} while (c>0);
}
Here the loop is executed at least once. Even if we are entering a value less than 0 at first time
itself, the loop will be executed once and then terminates. Or else the loop will be iterated as long as
the value of c is greater than 0
The initialization of control variable is done first,using assignment statements like int i=0 or
count=0.The variables i and count are called loop control variable. int i=0,which creates a new
variable with initial value 0, to act as a counter. Multiple,
comma separated, expressions are
allowed in the initialization section. But declaration expressions may not be mixed with other
expressions.
2.
Value of the control variable is evaluated using test condition. It is a relational expression like
i<0 that determines when the loop will exit. If condition is true, body of the loop is executed,
Otherwise loop is terminated and execution will continue with statement that immediately follow
the loop.
3.
When the body of the loop is executed, the control is transferred back to the for statement
after evaluating last statement in loop control. Now the control variable is
incremented
using assignment statement such as i=i+1 and the new value is tested to check whether it
satisfies the test condition. If condition is satisfied body of the loop will be again executed. This
process continues till value of control variable fails to satisfy test condition.
EXAMPLE:
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
for(x=0;x<10;x=x+1)
{
printf(%d\n,x);
}
Here loop will be executed 10 times and the digits 0 to 9 will be printed. The 3 sections should be
separated by semicolon.
for statement allows negative increment also
EXAMPLE:
for(x=9;x>0;x=x-1)
{
printf(%d\n,x);
}
Here loop will be executed 10 times and the digits 9 to 0 will be printed.
Since we are checking condition at the beginning of loop, the body of loop may not be executed
if the condition fails at the start.
EXAMPLE:
for(x=9;x<9;x=x-1)
{
printf(%d\n,x);
}
The above loop will never be executed.
In for statement, initialization, testing and incrementing are placed in for statement itself making it
visible to programmers and users in one place.
Additional Features
More than one variable can be initialized and incremented at a time in a for loop.
for(n=1,m=50;n<=m;n=n+1,m=m-1)
{
p=m/n;
}
The test-condition may have any compound relation and the testing need not be limited only to
the loop control variable.
for(i=1;i<20 && sum<100;i++)
{
sum=sum+i;
Dept.of Computer Science And Applications, SJCET, Palai
Page 68
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
printf(%d%d,i,sum);
}
It is permissible to use expressions in the assignment statements of initialization and increment
sections.
EXAMPLE:
for(x=(m+n)/2;x>0;x=x/2)
One or more sections of the for loop can be omitted ,if necessary
EXAMPLE:
m=0;
for(; m<100;)
{
printf(%d,m);
m=m+5;
}
We can set up time delay loops using for statement
EXAMPLE: for(j=0;j<1000;j++);
Here the semi colon at the end is known as null statement. Here the loop will be executed 1000
times without executing any output.
It is possible to use a scanf statement in for statement to assign value to the control variable
EXAMPLE: for(scanf(%d,&n);n>0;n=n/2)
2.6.3.1 NESTED FOR LOOPS
In some cases a set of statements which are repeated for a known number of times need to be
executed as long as a condition holds. In such case we will enclose this loop in another loop. The
enclosing loop is called Outer loop and the enclosed loop is called Inner loop.
for (i=0;i<5;i++)
{
-------Inner Loop
----------for(j=0;j<3;j++)
{
-----------------}
Outer Loop
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
}
EXAMPLE :Multiplication tables of a range of numbers
# include<stdio.h>
main ()
{
int m,n,i,j,p;
printf(enter the lower limit and upper limit\n);
scanf(%d%d,&m,&n);
for(i=m;i<=n;i++)
{
for(j=1;j<=10;j++)
{
p=i*j;
printf(%d,p);
printf();
}
printf(\n);
}
}
We need to generate multiplication table of the given range of numbers. So we need to nest
one loop within another. The outer loop is to select each number in the range and the inner loop is to
generate the multiplication table of the number selected by the outer loop.
In the outer loop when i takes m, the lower limit, the inner loop generates the multiplication
table of m. Once the inner loop completes, control goes to the outer loop again.i gets incremented it
becomes m+1.Then inner loop generates the multiplication table of m+1>this continues as long as the
test condition of outer loop is true.
break
continue
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
permits a jump from one statement to another within a loop as well as to jump out of the loop. The
break statement allows us to accomplish this task. A break statement provides an early exit from for,
while, do and switch constructs. A break causes the innermost enclosing loop or switch to be exited
immediately.
A break statement used within a loop is expected to be associated with an if statement. When the
test expression of if statement evaluates to true, the loop is exited prematurely.
while(test-expression 1)
{
Statements-1;
If (test-expression 2)
break;
Statements-2;
}
During the course of iteration if the test expression-2 evaluates to true, The control reaches the
break statement and executes it causing the loop to be exited prematurely.
EXAMPLE :
# include<stdio.h>
main()
{
int n,i,sum=0,number;
printf(enter no;of elements\n);
scanf(%d,&n);
printf(enter numbers\n);
for(i=1;i<=n;i++)
{
scanf(%d,& number);
if(number<0)
break;
sum=sum+number;
}
printf(sum of %d positive numbers=%d,i-1,sum)
}
Here in the course of accepting numbers, if the positive numbers are entered, they are added to
the variable sum. But if a negative number is entered, then control reaches the break statement,
which, when executed causes the loop to be executed prematurely. The value of sum is then
displayed.
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
2.7.2CONTINUE STATEMENT
During loop operations it may be necessary to skip a part of the body of the loop under certain
conditions. Like the break statement C supports similar statement called continue statement. The
continue statement causes the loop to be continued with the next iteration after skipping any
statement in between. The format of the continue statement is simply:
continue;
Similar to the break statement ,continue statement is also associated with an if statement.
While(test-expression 1)
{
Statements-1;
if (test-expression 2)
Dept.of Computer Science And Applications, SJCET, Palai
Page 72
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
continue;
Statements-2;
}
During the course of iteration if the test expression-2 evaluates to true, the control reaches the
continue statement ,the statement-2 are skipped for those iterations and the control goes to the
beginning of the loop. The loop is not prematurely exited as in the case of break statement
EXAMPLE
# include<stdio.h>
main()
{
int n,i,sum=0,number;
printf(enter no;of elements\n);
scanf(%d,&n);
printf(enter numbers\n);
for(i=1;i<=n;i++)
{
scanf(%d,& number);
if(number<0)
continue;
sum=sum+number;
}
printf(sum of %d positive numbers=%d,i-1,sum);
}
Here in the course of accepting numbers, if the positive numbers are entered, they are added to
the variable sum. But if a negative number is entered, then control reaches the continue statement,
which, when executed, causes the skipping of the statements following it within in the loop and
causes the control to be transferred back to the beginning of the loop, so that the loop continues with
the next iteration.
2.7.2.2 CONTINUE IN NESTED LOOPS
for(initialization1;test-condition1;increment1)
{
for(initialization2;test-condition2;increment2)
{
if(test-condition3)
continue;
}
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
Statements;
}
Here when test-condition 3 is true the continue statement is encountered control is transferred
to the next iteration of inner for loop skipping all the statements following continue in inner loop.
for(initialization1;test-condition1;increment1)
{
for(initialization2;test-condition2;increment2)
{
Statements- 2;
}
if(test-condition3)
continue;
}
Statements;
Continue
MODULE 2
MCA- 105 Structured Programming in C
ADMN
2014-17
EXAMPLE:
# include<stdio.h>
# include<conio.h>
void main()
{
int num1,num2,res;
while(1)
{
scanf("%d%d",&num1,&num2);
if(num2==0)
exit(0);
else
{
res=num1/num2;
printf("result of division is %d\n",res);
}
}
}
Usually in our programs we will use exit(0) which indicates a normal termination of program.