0% found this document useful (0 votes)
9 views

Lecture 9

Uploaded by

Umer Naveed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 9

Uploaded by

Umer Naveed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Programming Fundamentals

Lecture # 9

Dr. Shafiq Hussain


Associate Professor & Chairperson
Department of Computer Science

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.

• For example, a for loop can be inside a while loop or vice


versa.

6
Nested Loops in C (Cont..)
#include <stdio.h>
#include <conio.h>

main ()
{
int i, j, count;

for(i=2; i<=100; i++)


{
Count = 0;

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

• When break statement is encountered inside a loop, the loop is


immediately terminated and program control resumes at the
next statement following the loop.

• It can be used to terminate a case in the switch statement .

• 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;

/* while loop execution */


while( a < 20 )
{

11
Break Statement in C (Cont..)
printf("value of a: %d\n", a);
a++;

if( a > 15)


{
/* terminate the loop using break statement */
break;
}
}

getch();
}
12
Continue Statement in C
• The continue statement in C programming language works
somewhat like the break statement.

• Instead of forcing termination, however, continue forces the


next iteration of the loop to take place, skipping any code in
between.

13
Continue Statement in C (Cont..)
• For for loop, continue statement causes the conditional test
and increment portions of the loop to execute.

• For the while and do...while loops, continue statement causes


the program control passes to the conditional tests.

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.

• NOTE: Use of goto statement is highly discouraged in any


programming language because it makes difficult to trace the
control flow of a program, making the program hard to
understand and hard to modify.

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

• The increment operator ++ takes a single variable as its


operand.

• The value of its operand is incremented by one.


for (counter = 0; counter < limit; ++counter)

23
Increment & Decrement Operators
(Cont..)
• The value of the expression in which the ++ operator is used
depends on the position of the operator.

• When the ++ is placed immediately in front of its operand


( prefix increment ), the value of the expression is the
variable’s value after incrementing.

• When the ++ comes immediately after the operand ( postfix


increment ), the expression’s value is the value of the variable
before it is incremented.

24
Increments & Decrements Other Than 1

The loop counter can also be used to increment or


decrement values more than 1.

For Example:

for(i=0; i<=100; i= i+5)


0r
for(i=0; i<=100; i +=5)

25
Assignment-2
• Write a program in C to find the sum of first 10 numbers using while
loop.

• Write a program in C to find the factorial of a number using while loop.

• Write a program in to find whether a number is prime number or not


using while loop

• Write a program in C to write a number is reverse order using while


loop.

• Write a program in C to find the sum of digits of a number using while


loop.
26
Assignment-2 (Cont..)
• Write a program in C to display the table of 2 up to 10 using
while loop.

• Write a program in C to sum the series


• 1+ 2 + 4 + 6 + 8 + 10 +12 +14 + 16 + 18 + 20 using while
loop.

• Write a program in C to sum the series


• 1! +2! + 3! + 4! + 5! + 6! +7! +8! + 9! + 10! using while loop.

27
Assignment-2 (Cont..)
• Write all above programs using for loop and while loop

28
Questions
Any Question Please?

You can contact me at: [email protected]

Your Query will be answered within one working day.

29
Further Readings
• C How to Program by Dietel, Chapter 5.

• Lecture Handouts, Introduction to Programming (VU).

30
Thanks

31

You might also like