Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Programming Fundamentals

CS-1001
Lecture 11
Outlines
• Nested loops
• Nested for, while and do while
• Examples of nested loops
• Loop control statements
– Break
– Continue
– goto
Nested Loops
• C programming language allows us to use one
loop inside another loop
• You can put any type of loop inside any other
type of loop. For example, a for loop can be
inside a while loop or vice versa
• General syntax
– Loop (…)
– Loop(…)
– statement/s
– End Loop
– End Loop
Nested for loop
• Inserting for loop within another for loop
• Syntax
for(init ; condition; update)
{ for(init ; condition;
update) {
statement/s;
}
statement/s;
}
Nested while loop
• Inserting a while loop inside another while loop
• Syntax
– while(condition)
–{
– while(condition)
– {
– statement(s);
– }
– statement(s);
–}
Nested do while loop
• Nesting a do while loop inside another one
• Syntax
do
{
statement(s);
do
{
statement(
s);
}while( condi
tion );

}while( conditio
n );
Nested loop example (nestedLoop.c)
#include <stdio.h>

int main ()
{
// local variable definition
int i=0, j=0;

for(i=0; i<10; i++) {


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

return 0;
}
Nested loop example (nestedLoop.c)
#include <stdio.h>

int main ()
{
// local variable definition
int i=0, j=0;

for(i=0; i<10; i++) {


for(j=0; j < 10; j++) {
printf("%d ", j);
}
printf("\n");
}

return 0;
}
Nested loop example (nestedLoop.c)
#include <stdio.h>

int main ()
{
// local variable definition
int i=0, j=0;

for(i=0; i<10; i++) {


for(j=0; j < 10; j++) {
printf("%3d ",
i+j);
}
printf("\n");
}

return 0;
}
Example (nestedLoop2.c)
Creating a 10x10 identity matrix
#include <stdio.h>

int main ()
{
// local variable definition
int i=0, j=0;

for(i=0; i<10; i++) {


for(j=0; j < 10; j++) {
if(i==j)
printf("1");
else
printf("
0");
}
printf("\n");
}

return 0;
}
Loop control statements
• Loop control statements change execution of
a loop from its normal sequence
• When execution leaves a scope, all automatic
objects that were created in that scope are
destroyed
Break statement
• Two usages
– Terminate a loop prematurely
– Terminate a case in the switch statement
• In case of nested loops, if the break statement
is in the inner loop, it will stop the execution
of the inner loop and start executing the next
line of code of the outer loop.
Example (primes.c)
#include<stdio.h>

int main(){
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;break;
}
}
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0; }
Example (break.c)
#include <stdio.h>

int main ()
{
/* local variable definition */
int a = 10;
Output
value of a: 10
/* while loop execution */
value of a: 11
while( a < 20 )
value of a: 12
{
printf("value of a: %d\n", a);
value of a: 13
a++; value of a: 14
if( a > 15) value of a: 15
{
/* terminate the loop using break statement */
break;
}
}
return 0;
}
Continue statement
• Works 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.
Example (continue.c)
#include <stdio.h>

int main () { Output


int a = 10; value of a: 10
do { value of a: 11
if( a == 15) { value of a: 12
value of a: 13
a = a + 1;
value of a: 14
continue; value of a:
} 16 value of
printf("value of a: %d\n", a); a: 17 value
a++; of a: 18
value of a: 19
}while( a < 20 );
return 0;
}
goto statement
• A goto statement provides an unconditional jump from the goto to
a labeled statement
• NOTE: Use of goto statement is highly discouraged because it
makes difficult to trace the control flow of a program, making the
program hard to understand and modify.
– Any program that uses a goto can be rewritten so that it doesn't need
the goto
• 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.
• Syntax
goto label;
..
.
label: statement;
Example (goto.c)
#include <stdio.h>

int main () { Output:


int a = 10; value of a: 10
LOOP:do { value of a:
11 value of
if( a == 15) { a: 12 value
a = a + of a: 13
1; goto value of a: 14
value of a:
LOOP; 16 value of
} a: 17 value
printf("value of a: %d\n", a); of a: 18
value of a: 19
a++;
}while( a < 20 );
return 0;
Infinite loops
• There are several ways to create infinite loops in c (please
use with caution)
• Infinite loops may also run due to an error in your logic that
causes loop condition to always return true
• You can terminate an infinite loop by pressing Ctrl+C
• Examples
– for(;;) //infinite for loop
– while(true) or while(1) //infinite while loop
– do { //infinite do while loop
} while(true); or
do {
} while(1);
Next Lecture
• Lecture 11 – Functions

You might also like