0% found this document useful (0 votes)
11 views16 pages

Loop and Jump Statement

Looping statements in C are used to execute a block of code repeatedly until a specified condition is false, with three main types: while, do-while, and for loops. The while loop checks the condition before executing the loop body, the do-while loop executes the body at least once before checking the condition, and the for loop is used when the number of iterations is known in advance. Additionally, jumping statements like goto, break, and continue allow control to be transferred within the program, enhancing flexibility in execution.

Uploaded by

amitsingkumar981
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)
11 views16 pages

Loop and Jump Statement

Looping statements in C are used to execute a block of code repeatedly until a specified condition is false, with three main types: while, do-while, and for loops. The while loop checks the condition before executing the loop body, the do-while loop executes the body at least once before checking the condition, and the for loop is used when the number of iterations is known in advance. Additionally, jumping statements like goto, break, and continue allow control to be transferred within the program, enhancing flexibility in execution.

Uploaded by

amitsingkumar981
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

Looping Statements in C

Looping statements are control statements used to execute a block of code repeatedly
until a specified condition becomes false.
[Link]
Sometimes in programming we need to perform
the same task many times.. Instead of writing the same statem
statement
ent again and again, we use
loops. A loop reduces code length and makes the program efficient.
efficient

In looping statements, a condition is checked


checked.. As long as the condition is true, the
statements inside the loop continue to execute. When the condition becomes false, the
loop stops and the program moves to the next statement.

Types of Looping Statements in C

1. while loop
2. do–while loop
3. for loop

While Loop in C

The while loop is a looping control statement in C programming that is used to execute a
block of statementss repeatedly as long as the given condition is true.
true

In a while loop, the condition is checked before the execution of the loop body.
body If the
condition is true,, the statements inside the loop are executed. After execution, the
condition is checked again. Thisis process continues until the condition becomes false.

Because the condition is tested at the beginning of the loop, the while loop is also called
an entry-controlled loop.
Syntax
while(condition)
{
// statements
}

Program to Print Numbers from 1 to 5 Using While Loop


#include<stdio.h>

int main()
{
int i = 1;

while(i <= 5)
{
printf("%d\n", i);
i++;
}

return 0;
}
Output
1
2
3
4
5

Do–While Loop in C

The do–while loop is a looping control statement used to execute a block of statements
repeatedly until a given condition becomes false.

In a do–while loop, the statements inside the loop are executed first, and after that the
condition is checked. If the condition is true, the loop continues to execute again. If the
condition becomes false, the loop stops.
Because the condition is checked after executing the loop body,, the do–while
do loop is
called an exit-controlled loop.

A very important feature of the do


do–while loop is that the loop body executes at least
once, even
en if the condition is false.

Syntax
do
{
// statements
}
while(condition);

Program to Print Numbers from 1 to 5 Using Do


Do–While Loop
#include<stdio.h>

int main()
{
int i = 1;

do
{
printf("%d\n",
n", i);
i++;
}
while(i <= 5);

return 0;
}
5. Output
1
2
3
4
5

Example: Program to Print Table of a Number Using Do–While Loop

Program
#include<stdio.h>

int main()
{
int num, i = 1;

printf("Enter a number: ");


scanf("%d",&num);

do
{
printf("%d x %d = %d\n", num, i, num*i);
i++;
}
while(i <= 10);

return 0;
}

Output Example
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

For Loop in C

The for loop is a looping control statement in C programming used to execute a block of
statements repeatedly for a specific number of times
times.

It is commonly used when the number of iterations is known in advance.


advance The for loop
combines initialization,
tialization, condition checking, and increment/decrement in a single
statement.

The loop continues executing as long as the condition is true


true.. When the condition
becomes false,, the loop stops and the program continues with the next statement.

The for loop is also called an entry


entry-controlled loop because the condition is checked
before executing the loop body.

Syntax
for(initialization; condition; increment/decrement)
{
// statements
}
Working of For Loop

1. First, the initialization is exec


executed.
2. Then the condition is checked
checked.
3. If the condition is true, the loop body executes.
4. After executing the statements, the increment/decrement part updates the
variable.
5. The condition is checked again.
6. This process continues until the condition becomes false.

Simple Program Example

Program to Print Numbers from 1 to 5 Using For Loop


#include<stdio.h>

int main()
{
int i;

for(i = 1; i <= 5; i++)


{
printf("%d\n", i);
}

return 0;
}

5. Output
1
2
3
4
5

Advantages of For Loop

1. Combines initialization, condition, and increment in one line.


2. Makes programs short and easy to read.
3. Best used when the number of repetitions is known.
Nested Loop in C

1. Definition / Theory

A nested loop is a loop inside another loop. In other words, when one loop is placed
inside the body of another loop, it is called a nested loop.

The outer loop controls how many times the inner loop will execute. For every single
iteration of the outer loop, the inner loop runs completely.

Nested loops are commonly used when we need to work with rows and columns, such as
printing patterns, tables, or matrices.

2. General Syntax
for(initialization1; condition1; increment1)
{
for(initialization2; condition2; increment2)
{
// statements
}
}

Explanation

 Outer loop runs first.


 For every iteration of the outer loop, the inner loop executes fully.
 After the inner loop finishes, control goes back to the outer loop.

3. Working of Nested Loop

1. The outer loop starts execution.


2. Then the inner loop begins and runs completely until its condition becomes false.
3. After the inner loop finishes, control returns to the outer loop.
4. The outer loop continues to the next iteration, and the inner loop runs again.
5. This process continues until the outer loop condition becomes false.
Program to Print Number Triangle Using Nested Loop

Program
#include<stdio.h>

int main()
{
int i, j;
// Outer loop runs 4 times
for(i = 1; i <= 4; i++)
{
// Inner loop runs
for(j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}

return 0;
}

Output
1
1 2
1 2 3
1 2 3 4

Jumping Statements in C

1. Definition / Theory

Jumping statements are control statements used to transfer the control of a program
from one part to another part without following the normal sequential order of
execution.
Normally, a program executes line by line from top to bottom
bottom.. But sometimes we need
to skip some statements, exit a loop, or move directly to another part of the program.
program
Jumping statements
ents are used for this purpose.

These statements interrupt the normal flow of execution and jump to another
statement or location in the program
program.

2. Types of Jumping Statements in C

There are mainly four types of jumping statements


statements:

1. goto statement
2. break statement
3. continue statement

Goto Statement in C

1. Definition / Theory

The goto statement is a jumping statement in C programming. It is used to transfer the


control of the program directly to another part of the program marked by a label.
label

When the goto statement is executed


executed, the program control jumps to the labeled
statement and continues execution from there. It does not follow the normal sequential
order of execution.

The goto statement is generally used when the program needs to jump forward
f or
backward within the same function
function.

There are two types of goto statements


statements:
1. Forward Goto
2. Backward Goto

1. Forward Goto

Definition

In forward goto, the program control jumps from the current position to a label that
appears later in the program.

Syntax
goto label;

...

label:
statement;

Example Program (Forward Goto)


#include<stdio.h>

int main()
{
int num;

printf("Enter a number: ");


scanf("%d",&num);

if(num < 0)
goto negative;

printf("Number is positive");

negative:
printf("\nProgram Finished");

return 0;
}

Output Example
Enter a number: -5
Program Finished

Explanation

1. The user enters a number.


2. If the number is less than 0, the program jumps to the label negative.
3. The statement "Program Finished" is printed.
4. If the number is positive, the program prints "Number is positive" and then
continues.

2. Backward Goto

Definition

In backward goto, the program control jumps back to a label that appears earlier in
the program. It is often used to repeat a set of statements similar to a loop.

Syntax
label:
statements
goto label;

Example Program (Backward Goto)


#include<stdio.h>

int main()
{
int i = 1;
start:
printf("%d ", i);
i++;

if(i <= 5)
goto start;

return 0;
}

Output
1 2 3 4 5

Explanation

1. Variable i is initialized with value 1.


2. The label start marks the position where the program may return.
3. The number i is printed.
4. The value of i is increased by 1.
5. If i ≤ 5, the program jumps back to the label start.
6. This process continues until i becomes greater than 5.

Break Statement in C

1. Definition / Theory

The break statement is a jumping statement in C programming. It is used to terminate


a loop or switch statement immediately and transfer the control of the program to the
statement that appears after the loop or switch block.

When the break statement is executed, the program stops the execution of the current
loop and continues with the next statement outside the loop without executing the
remaining iteration of the loop or checking remaining cases in switch statement.
The break statement is mainly used in:

 for loop
 while loop
 do-while loop
 switch statement

Syntax
break;

The break statement is usually written inside a loop or switch block..

Example Program Using Break Statement


The statements inside the loop are executed sequentially. When the condition
conditio
for the break statement becomes true and the break statement is encountered
within the loop , the program flow breaks out of the loop, regardless of any
remaining iterations.

Program: Print Numbers from 1 to 10 but Stop at 5


#include<stdio.h>

int main()
{
int i;

for(i=1; i<=10; i++)


{
if(i==5)
break;

printf("%d ", i);


}
printf("Loop exited.\n");
return 0;
}

Output
1 2 3 4 Loop exited
Explanation:
 Loop Execution Starts and goes normally till i = 5.
 When i = 6, the condition for the break statement becomes true and the
program control immediately exits the loop.
 The control continues with the remaining statements outside the loop.

Continue Statement in C

1. Definition / Theory

The continue statement is a jumping statement in C programming. It is used to skip the


current iteration of a loop and move to the next iteration.

When the continue statement is executed, the remaining statements inside the loop for
that iteration are skipped, and the program control goes back to the beginning of the
loop for the next iteration.

Unlike the break statement, the continue statement does not terminate the loop. It only
skips the current loop cycle.
The continue statement is mainly used in:

 for loop
 while loop
 do-while loop

Syntax
continue;

The continue statement


tatement is written inside the loop body.

Example Program Using Continue Statement


// C Program to illustrate the continue statement
#include <stdio.h>
int main()
{
int i;
// loop
for (i = 0; i < 5; i++) {
if (i == 2) {
// continue to be executed if i = 2
printf("Skipping iteration %d
%d\n", i);
continue;
}
printf("Executing iteration %d\n", i);
}
return 0;
}

Output
Executing iteration 0
Executing iteration 1
Skipping iteration 2
Executing iteration 3
Executing iteration 4
Explanation: The for loop iterates from 0 to 4. Inside the loop, we check if i is
equal to 2. If the condition is true, the continue statement is executed, and it skips
the remaining code within the loop for that iteration. If the condition is false, the
code proceeds normally.

✅Simple Difference (Very Important for Exams)

Break Continue

Terminates the loop completely Skips only the current iteration

Control moves outside the loop Control goes to next loop iteration

You might also like