0% found this document useful (0 votes)
31 views13 pages

CPPM Unit-4

The document discusses various looping constructs in C including while, do-while, for loops and nested loops. It provides syntax and examples of each loop type. Key looping constructs are while loops execute the body if the condition is true, do-while always executes the body at least once and checks the condition after, and for loops allow initialization, condition checking and increment/decrement in one statement.

Uploaded by

adobephoto0011
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)
31 views13 pages

CPPM Unit-4

The document discusses various looping constructs in C including while, do-while, for loops and nested loops. It provides syntax and examples of each loop type. Key looping constructs are while loops execute the body if the condition is true, do-while always executes the body at least once and checks the condition after, and for loops allow initialization, condition checking and increment/decrement in one statement.

Uploaded by

adobephoto0011
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/ 13

104-CPPM Unit-4 SDJ International College

Use of goto statement for


iteration
whileloop
do..whileloop
forloop
Nestedwhile,do..whileandfor loops
Jumpingstatement:(breakandcontinue)

goto Statement
The goto statement is a jump statement which is sometimes also referredto as unconditional jump
statement. The goto statement can be used to jump from anywhere to anywhere within a
function.

Prof. Akansha Srivastav Page1


104-CPPM Unit-4 SDJ International College
What is Loop in C?
Looping Statements in C execute the sequence of statements many times until the stated
condition becomes false. A loop in C consists of two parts, a
body of a loop and a control statement. The control statement is a combination of some
conditions that direct the body of the loop to execute until the specified condition becomes false.
The purpose of the C loop is to repeat the same code a number of times.

Depending upon the position of a control statement in a program, looping statement in C is


classified into two types:

1. Entrycontrolledloop
2. Exitcontrolledloop
In an entry control loop in C, a condition is checked before executing the body of a loop. It is
also called as a pre-checking loop.

In an exit controlled loop, a condition is checked after executing the body of a loop. It is also
called as a post-checking loop.

The control conditions must be well defined and specified otherwise the loop will execute an
infinite number of times. The loop that does not stop executing and processes the statements
number of times is called as an infinite loop. An infinite loop is also called as an “Endless loop.”

Following are some characteristics of an infinite loop:


1. No termination condition is specified.
2. The specified conditions never meet.
Thespecifiedconditiondetermineswhethertoexecutetheloopbodyor not.

„C‟ programming language provides us with three types of loop constructs:


1. The while loop
2. The do-while loop
3. The For Loop

Sr. No. Loop Type Description

1 While Loop In while loop, a condition is evaluated before


Processing a body of the loop. If a condition is

Prof. Akansha Srivastav Page2


104-CPPM Unit-4 SDJInternationalCollege
true the n and only the n the body of a loop is executed.

2 Do-WhileLoop In a do…while loop, the condition is always executed after


the body of a loop. It is also called an exit-controlled loop.

3 For Loop In a for loop, the initial value is performed only once, then
the condition tests and compares the counter to a fixed value
after each iteration, stopping the for loop when false is
returned.

While Loop in C

A while loop is the most straight forward looping structure. While loop syntax in C programming
language is as follows:

Syntax of While Loop in C:

while(condition)
{
statements;
}

It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of


the loop. If a condition is true then and only then the body of a loop is executed. After the body
of a loop is executed the n control again goes back at the beginning, and the condition is checked
if it is true, the same process is executed until the condition becomes false. Once the condition
becomes false, the control goes out of the loop.

After exiting the loop, the control goes to the statements which are immediately after the loop.
The body of a loop can contain more than one statement. If it contains only one statement, then
the curly braces are not compulsory. It is a good practice though to use the curly braces even we
have a single statement in the body.

Prof. Akansha Srivastav Page3


104-CPPM Unit-4 SDJ International College
In while loop, if the condition is not true, then the body of a loop will not be executed, not even once. It
is different in do while loop.

Following program illustrates while loop in C programming example:

#include<stdio.h>
#include<conio.h>
void main()
{
int num=1; //initializing the variable while(num<=10) //while loop
while condition
{
printf("%d\n",num);
num++; //incrementing operation
}
return 0;
}

Do-While loop in C
A do…while loop in C is similar to the while loop except that the condition is always executed
after the body of a loop. It is also called an exit-controlled loop.

Syntax of do while loop in C programming language is as follows:


do
{
statements
}while(expression);

As we saw in a while loop, the body is executed if and only if the condition is true. In somecases,
we have to execute a body of the loop at least once even if the condition is false. This type of
operation can be achieved by using a do-while loop.

In the do-while loop, the body of a loop is always executed at least once. After the body is
executed, then it checks the condition. If the condition is

Prof. Akansha Srivastav Page 4


104-CPPM Unit-4 SDJ International College
true, then it will again execute the body of a loop otherwise control is transferred out of the loop.

Similar to the while loop, once the control goes out of the loop the statements which are
immediately after the loop is executed.

The critical difference between the while and do-while loop is that in while loop the while is
written at the beginning. In do-while loop, the while condition is written at the end and
terminates with a semi-colon (;)
The following loop program in C illustrates the working of a do-while loop:

#include<stdio.h>
#include<conio.h>
void main()
{
int num=1; //initializing the variable do
//do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}

For loop in C
A for loop is a more efficient loop structure in „C‟ programming. The general structure of for loop
syntax in C is as follows:

for(initialvalue; condition; incrementation or decrementation)


{
statements;
}

The initial value of the for loop is performed only once.

The condition is a Boolean expression that tests and compares the counter to a fixed value after
each iteration, stopping the for loop when false is returned.

Prof. Akansha Srivastav Page 5


104-CPPM Unit-4 SDJ International College
The incrementation/decrementation increases(or decreases)the counter by a set value.

Following program illustrates the for loop in C programming example: #include<stdio.h>


void main()
{
int number;
for(number=1;number<=10;number++) //for loop to print 1-10
numbers
{
printf("%d\n",number); //to print the number
}
return0;
}

Nested Loop 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. Let's observe an example of nesting loops in C.

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.
}

Nested for loop

for(initialization;condition;inc/dec)
{
for(initialization;condition; inc/dec)
{

Prof. Akansha Srivastav Page6


104-CPPM Unit-4 SDJ International College
//inner loop statements.
}
//outer loop statements.
}
Example:
// C program to print right half pyramid pattern of star
#include <stdio.h>

void main()
{
int rows = 5;

// first loop for printing rows


for (int i = 0; i < rows; i++) {

// second loop for printing character in each rows


for (int j = 0; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Output:

Prof. Akansha Srivastav Page7


104-CPPM Unit-4 SDJ International College
Nested while loop

while(condition)
{
while(condition)
{
//inner loop statements.
}
//outer loop statements.
}

Example:
// C program to print right half pyramid pattern of star
#include <stdio.h>

void main()
{
int rows = 5;
// first loop for printing rows
i=0;

while(i < rows)


{
// second loop for printing character in each rows
j=0;
while(j <= i)
{
printf("*");
j++;
}
printf("\n");
i++;
}
getch();
}
Output:

Prof. Akansha Srivastav Page8


104-CPPM Unit-4 SDJ International College
Nested do..while loop
do
{
do
{
//inner loop statements.
}while(condition);
//outer loop statements.
}while(condition);

Jumping statement: (break and continue)


In the break statement, the control exits from the loop. In the continue statement, the control
remains within the loop. It is used to stop the execution of the loop at a specific condition. It is
used to skip a particular iteration of the loop.

CONTINUE
BREAK

Prof. Akansha Srivastav Page9


104-CPPM Unit-4 SDJ International College

Break statement
In all the C loops we have a way to break out of a loop at any point in time, immediately, regardless of the
conditions set for the loop.

This is done using the break keyword.

In simple words, The break statement is a loop control statement which is used to terminate
the loop immediately.

Basically break statements are used in the situations when we are not sure about the actual number of
iterations for the loop or we want to terminate the loop based on some condition.

Syntax :

break;

How break statement works?

Example : break statement


// program to use break statement inside for loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
// break condition

Prof. Akansha Srivastav Page10


104-CPPM Unit-4 SDJ International College
if (i == 5) {
break;
}
printf("%d ", i);
}
return 0;
}
Output
1234

In the above example, a for loop is used to print the value of i in each iteration. In which
a if statement is used with break statement in it.

The condition of if statment is i == 5 i.e. when i is equal to 5 and break statement is executed to
terminate the loop. Hence, the output doesn't include values greater than or equal to 5.

In C programming, break is also used with the switch statement. This will be discussed in the next
tutorial.

continue
This is one of the easiest and the most basic keywords in C/C++, which gives programmers control
over loops.

The continue exactly as the name suggests. Since we use this in loops, it will skip over the remaining
body of the current loop, and continue to the next iteration.

We can use this inside any loops like for , while , or do-while loops.
In Simple words,
1. The continue statement is used to skip the remaining portion of a for/while loop.
2. we continue onto the next iteration, and move to the loop condition check.
3. This is quite useful for programmers to be flexible with their approach in control loops.

Syntax :

continue;

Prof. Akansha Srivastav Page11


104-CPPM Unit-4 SDJ International College

How continue statement works?

Example : continue statement


// loop to print numbers 1 to 10 except 4
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
// if i is equal to 4 , continue to next iteration without printing 4.
if (i == 4) {
continue;
}
else{
// otherwise print the value of i.
printf("%d ", i);
}
}
return 0;
}
Output

1 2 3 5 6 7 8 9 10

In the above program, we have used the the for loop to print the value of i in each iteration. Here, notice the
code,

if (i == 4) {
continue;
}

Prof. Akansha Srivastav Page12


104-CPPM Unit-4 SDJ International College
This means

 When i is equal to 4, the continue statement skips the current iteration and starts the next
iteration
 Then, i becomes 5, and the condition is evaluated again.
 Hence, 5 and 6 are printed in the next two iterations.
 Loop prints the value of i until it became 11 and condition becomes false. Then, the loop
terminates.

Break vs Continue

Break Continue

Break statement stops the entire Continue statement only stops the
process of the loop. current iteration of the loop.

Break also terminates the remaining iterations. Continue doesn‟t terminate the next
iterations; it resumes with the
successive iterations.
Break statement can be used with switch Continue statement can be used with loops but not
statements and with loops. switch statements.
In the break statement, the control exits from the In the continue statement, the control remains within
loop. the loop.
It is used to stop the execution of the loop at a It is used to skip a particular iteration of the loop.
specific condition.

Prof. Akansha Srivastav Page13

You might also like