KEMBAR78
Nested Loops and Jumping Statements | PDF | Control Flow | Computer Engineering
0% found this document useful (0 votes)
60 views9 pages

Nested Loops and Jumping Statements

The document explains the concept of nested loops in C programming, detailing how loops can be defined within other loops without restrictions. It provides syntax examples for nested for, while, and do-while loops, as well as examples of using break, continue, goto statements, and the exit function. Additionally, it includes sample code snippets demonstrating the functionality of these constructs.

Uploaded by

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

Nested Loops and Jumping Statements

The document explains the concept of nested loops in C programming, detailing how loops can be defined within other loops without restrictions. It provides syntax examples for nested for, while, and do-while loops, as well as examples of using break, continue, goto statements, and the exit function. Additionally, it includes sample code snippets demonstrating the functionality of these constructs.

Uploaded by

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

Nested Loops in C

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside
another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining
any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another
loop; for example, you can define 'while' loop inside a 'for' loop.

Syntax of Nested loop

Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}

Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or 'do-while' loop.

Syntax 1
The syntax for a nested for loop statement in C is as follows −
for ( initialization; condition; increment/decrement )
{

for ( initialization; condition; increment/decrement )


{
//statements of inner loop;
}
// statements of outer loop;
}

Syntax 2
The syntax for a nested while loop statement in C programming language is as follows −
while(condition)
{

while(condition)
{
//statements of inner loop
}
//statements of inner loop
}
Syntax 3
The syntax for a nested do...while loop statement in C programming language is as follows −
do
{
//statements of inner loop
do
{
//statements of inner loop
}while( condition );

}while( condition );

Example of nested while loop

#include <stdio.h>
#include <conio.h>
void main()
{
int i=1, j=2;
clrscr();
while(i<=10)
{
while(j<=20)
{
printf(“\n%d”, j);
J=j+2;
}
printf(“\n%d”, i);
i++;
}
getch();
}

Example of nested do-while loop

#include <stdio.h>
#include <conio.h>
void main()
{
int i=1, j=2;
clrscr();
do
{
printf(“\n%d”, i);
i++;
do
{
printf(“\n%d”, j);
j=j+2;
} while(j<=20);
printf(“\n”);
} while(i<=10);
getch();
}

Example of nested for loop

#include <stdio.h>
#include <conio.h>
void main()
{
int i, j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i; j++)
{
printf(“ %d “, j);
}
printf(“\n”);
getch();
}
Jumping Statement
Jump Statement makes the control jump to another section of the program unconditionally when
encountered. It is usually used to terminate the loop or switch-case instantly. It is also used to
escape the execution of a section of the program.

break statement
The break is a keyword in C which is used to bring the program control out of the loop. The break statement is
used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested
loops, it breaks the inner loop first and then proceeds to outer loops. The break statement in C can be used in the
following two scenarios:

1. With switch case


2. With loop

Example
#include<stdio.h>
#include<stdlib.h>
void main ()
{
int i;
clrscr();
for(i = 1; i<=10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
getch();
}

Output

1 2 3 4 5 came outside of loop i = 5

Example
#include<stdio.h>
#include<conio.h>

void main ()
{
int n=2,i,choice;
do
{
i=1;
while(i<=10)
{
printf("%d X %d = %d\n",n,i,n*i);
i++;
}
printf("do you want to continue with the table of %d , enter any nonzero value to continue.",n+1);
scanf("%d",&choice);
if(choice == 0)
{
break;
}
n++;
}while(1);
getch();
}

Output

2X1=2
2X2=4
2X3=6
2X4=8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
do you want to continue with the table of 3 , enter any non-zero value to continue.3
3X1=3
3X2=6
3X3=9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30
do you want to continue with the table of 4 , enter any non-zero value to continue.0

continue statement
The continue statement in C language is used to bring the program control to the beginning of the loop. The
continue statement skips some lines of code inside the loop and continues with the next iteration. It is mainly
used for a condition so that we can skip some code for a particular condition.

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
for(i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
printf("%d \n",i);
}
getch();
}

Output

1
2
3
4
6
7
8
9
10

goto statement
The goto statement is known as jump statement in C. As the name suggests, goto is used to transfer the program
control to a predefined label. The goto statment can be used to repeat some part of the code for a particular
condition. It can also be used to break the multiple loops which can't be done by using a single break statement.

Syntax:

label:
//some part of the code;
goto label;

goto example
Let's see a simple example to use goto statement in C language.

#include <stdio.h>
#include <conio.h>
void main()
{
int num,i=1;
clrscr();
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
getch();
}
Output:
Enter the number whose table you want to print?10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

exit() function in C
The exit() function is used to terminate a process or function calling immediately in the program. It means any
open file or function belonging to the process is closed immediately as the exit() function occurred in the
program. The exit() function is the standard library function of the C, which is defined in the <stdlib.h> or
<process.h> header file. So, we can say it is the function that forcefully terminates the current program and
transfers the control to the operating system to exit the program. The exit(0) function determines the program
terminates without any error message, and then the exit(1) function determines the program forcefully terminates
the execution process.

The exit() function has no return type.

Example:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main ()
{
int i, num;
clrscr();
printf ( " Enter the last number: ");
scanf ( " %d", &num);
for ( i = 1; i<num; i++)
{
if ( i == 6 )
{
exit(0);
}
else
{
printf (" \n Number is %d", i);
}
getch();

Output

Enter the last number: 10

Number is 1
Number is 2
Number is 3
Number is 4
Number is 5

You might also like