Lecture 9
Lecture 9
Lecture # 9
1
Learning Objectives
• To describe Nested Loops in C.
• To demonstrate Break Statement in C.
• To demonstrate Continue Statement in C.
• To demonstrate Goto Statement in C.
• To demonstrate Increment and Decrement Operators in C.
2
Nested Loops in C
C programming language allows using one loop inside another
loop.
Syntax:
The syntax for a nested for loop statement in C is as follows:
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
3
Nested Loops in C (Cont..)
The syntax for a nested while loop statement in C
programming language is as follows:
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
4
Nested Loops in C (Cont..)
The syntax for a nested do...while loop statement in C
programming language is as follows:
do
{
statement(s);
do
{
statement(s);
}while( condition );
}while( condition );
5
Nested Loops in C (Cont..)
• A final note on loop nesting is that you can put any type of
loop inside of any other type of loop.
6
Nested Loops in C (Cont..)
#include <stdio.h>
#include <conio.h>
main ()
{
int i, j, count;
7
Nested Loops in C (Cont..)
for(j=1; j <= i; j++)
if((i%j) == 0)
count++;
if(count == 2)
printf("%d is prime\n", i);
}
getch();
}
8
Break Statement in C
• If you are using nested loops (i.e., one loop inside another
loop), the break statement will stop the execution of the
innermost loop and start executing the next line of code after
the block.
break;
9
Break Statement in C (Cont..)
10
Break Statement in C (Cont..)
#include <stdio.h>
#include <conio.h>
main ()
{
/* local variable definition */
int a = 10;
11
Break Statement in C (Cont..)
printf("value of a: %d\n", a);
a++;
getch();
}
12
Continue Statement in C
• The continue statement in C programming language works
somewhat like the break statement.
13
Continue Statement in C (Cont..)
• For for loop, continue statement causes the conditional test
and increment portions of the loop to execute.
Syntax:
The syntax for a continue statement in C is as follows:
continue;
14
Continue Statement in C (Cont..)
15
Continue Statement in C (Cont..)
#include <stdio.h>
#include <conio.h>
main ()
{
int a = 10;
do
{
if( a == 15)
{
a = a + 1;
continue;
} 16
Continue Statement in C (Cont..)
printf("value of a: %d\n", a);
a++;
}while( a < 20 );
getch();
}
17
Goto Statement in C
• A goto statement in C programming language provides an
unconditional jump from the goto to a labeled statement in the
same function.
18
Goto Statement in C (Cont..)
Syntax:
The syntax for a goto statement in C is as follows:
goto label;
..
.
label: statement;
Here label can be any plain text except C keyword and it can be
set anywhere in the C program above or below to goto statement.
19
Goto Statement in C (Cont..)
20
Goto Statement in C (Cont..)
#include <stdio.h>
#include <conio.h>
main ()
{
int a = 10;
/* do loop execution */
LOOP:do
{
if( a == 15)
{ 21
Goto Statement in C (Cont..)
a = a + 1;
goto LOOP;
}
printf("value of a: %d\n", a);
a++;
}while( a < 20 );
getch();
}
22
Increment & Decrement Operators
• The counting loops that you have seen have all included
assignment expressions of the form:
counter = counter + 1 or counter += 1
23
Increment & Decrement Operators
(Cont..)
• The value of the expression in which the ++ operator is used
depends on the position of the operator.
24
Increments & Decrements Other Than 1
For Example:
25
Assignment-2
• Write a program in C to find the sum of first 10 numbers using while
loop.
27
Assignment-2 (Cont..)
• Write all above programs using for loop and while loop
28
Questions
Any Question Please?
29
Further Readings
• C How to Program by Dietel, Chapter 5.
30
Thanks
31