Unit -2
Loops in programming are used to repeat a block of code until the specified condition is met.
The looping simplifies the complex problems into the easy ones. A loop statement allows
programmers to execute a statement or group of statements multiple times without repetition of
code. We can repeat the same code for a finite number of times.
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.”
Advantage of loops in C
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
3) Using loops, we can traverse over the elements of data structures (array or linked lists).
There are mainly two types of loops in C Programming:
1. Entry Controlled loops: In Entry controlled loops, a condition is checked before
executing the body of a loop. It is also called as a pre-checking loop. For Loop and While
Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops, a condition is checked after executing the
body of a loop. It is also called as a post-checking loop.The loop body will execute at
least once, irrespective of whether the condition is true or false. do-while Loop is Exit
Controlled loop.
While Loop
While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code
to be executed multiple times depending upon a given boolean condition. The while loop is
mostly used in the case where the number of iterations is not known in advance.
1. We have initialized a variable called num with value 1. We are going to print from 1 to
10 hence the variable is initialized with value 1. If you want to print from 0, then assign
the value 0 during initialization.
2. In a while loop, we have provided a condition (num<=10), which means the loop will
execute the body until the value of num becomes 10. After that, the loop will be
terminated, and control will fall outside the loop.
3. In the body of a loop, we have a print function to print our number and an increment
operation to increment the value per execution of a loop. An initial value of num is 1,
, after the execution, it will become 2, and during the next execution, it will become 3.
This process will continue until the value becomes 10 and then it will print the series on
console and terminate the loop.
Syntax: #include <stdio.h>
Counter initialization int main()
while (condition test) {
{ int numt=1;
Statements to be while (num <= 10)
{
executed repeatedly
printf("%d \t", num);
Increment (++) or num++;
Decrement (--) counter }
} return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
do…while Loop
The do…while in C is a loop statement used to repeat some part of the code till the given
condition is fulfilled. It is a form of an exit-controlled or post-tested loop where the test
condition is checked after executing the body of the loop. Due to this, the statements in the
do…while loop will always be executed at least once no matter what the condition is. The do-
while loop is mainly used in the case where we need to execute the loop at least once. The do-
while loop is mostly used in menu-driven programs where the termination condition depends
upon the end user.
Syntax: #include <stdio.h>
int main()
Counter initialization {
{ int numt=1;
Statements to be {
executed repeatedly printf("%d \t", num);
num++;
Increment (++) or
} while (num <= 10);
Decrement (--) counter return 0;
} while (condition test); }
Output:
1 2 3 4 5 6 7 8 9 10
1. We have initialized a variable called num with value 1.
2. In the body of a loop, we have a print function to print our number and an increment
operation to increment the value per execution of a loop. An initial value of num is 1,
after the execution, it will become 2, and during the next execution, it will become 3.
This process will continue until the value becomes 10 and then it will print the series on
console and terminate the loop.
, 3. In a do while loop, we have provided a condition (num<=10), which means the loop will
execute the body until the value of num becomes 10. After that, the loop will be
terminated, and control will fall outside the loop.
For Loop in C
When you know exactly how many times you want to loop through a block of code, use
the for loop instead of a while.
1. The initial value of the for loop is performed only once. It is executed (one time) before
the execution of the code block.
2. 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. It defines the
condition for executing the code block.
3. The incrementation/decrementation increases (or decreases) the counter by a set value. It
is executed (every time) after the code block has been executed.
4.
Syntax: #include <stdio.h>
int main()
for (initial value; condition; {
incrementation or int i;
decrementation ) for (i = 0; i <= 10; i = i + +)
{ {
printf("%d \t", i);
statements;
}
} return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Difference between while and do while loop
While Do While
It checks the condition first and then This loop will execute the statement(s) at
executes statement(s) least once, then the condition is checked.
It is an entry controlled loop. It is an exit controlled loop.
The Keyword “while” is used. The keyword “do while” is used.
Do while loop allows initialization of
While loop allows initialization of counter
counter variables before and after starting
variables before starting the body of a loop.
the body of a loop.
We do not need to add a semicolon at the We need to add a semicolon at the end of
end of a while condition. the while condition.
, In case of a single statement, we do need to
Brackets are always needed.
add brackets.
Statement(s) can be executed zero times if
Statement is executed at least once.
the condition is false.
Generally while loop is written as: Generally do while loop is written as:
while (condition) { do{
Statements; // loop body Statements; //loop body
} } while (condition);
Difference between while and for loop
While Loop For Loop
It is used when the number of iterations is It is used when the number of iterations is
not known. known.
Initialization is always outside the loop. Initialization may be either in loop statement
or outside the loop.
In case of no condition, an error will be In case of no condition, the loop is repeated
shown. infinite times.
Condition may be expression or non-zero Condition is a relational expression.
value.
while ( condition ) { statement(s); } for ( init ; condition ; iteration ) {
statement(s); }
break and continue
break
The break statement ends the loop
immediately when it is
encountered.
Its syntax is: break;
This statement terminates the
smallest enclosing loop
(i.e., while, do-while, for loop,
or switch statement). Below is the
program to illustrate the same:
The break statement is almost
always used with if...else statement
inside the loop.
Loops in programming are used to repeat a block of code until the specified condition is met.
The looping simplifies the complex problems into the easy ones. A loop statement allows
programmers to execute a statement or group of statements multiple times without repetition of
code. We can repeat the same code for a finite number of times.
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.”
Advantage of loops in C
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
3) Using loops, we can traverse over the elements of data structures (array or linked lists).
There are mainly two types of loops in C Programming:
1. Entry Controlled loops: In Entry controlled loops, a condition is checked before
executing the body of a loop. It is also called as a pre-checking loop. For Loop and While
Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops, a condition is checked after executing the
body of a loop. It is also called as a post-checking loop.The loop body will execute at
least once, irrespective of whether the condition is true or false. do-while Loop is Exit
Controlled loop.
While Loop
While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code
to be executed multiple times depending upon a given boolean condition. The while loop is
mostly used in the case where the number of iterations is not known in advance.
1. We have initialized a variable called num with value 1. We are going to print from 1 to
10 hence the variable is initialized with value 1. If you want to print from 0, then assign
the value 0 during initialization.
2. In a while loop, we have provided a condition (num<=10), which means the loop will
execute the body until the value of num becomes 10. After that, the loop will be
terminated, and control will fall outside the loop.
3. In the body of a loop, we have a print function to print our number and an increment
operation to increment the value per execution of a loop. An initial value of num is 1,
, after the execution, it will become 2, and during the next execution, it will become 3.
This process will continue until the value becomes 10 and then it will print the series on
console and terminate the loop.
Syntax: #include <stdio.h>
Counter initialization int main()
while (condition test) {
{ int numt=1;
Statements to be while (num <= 10)
{
executed repeatedly
printf("%d \t", num);
Increment (++) or num++;
Decrement (--) counter }
} return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
do…while Loop
The do…while in C is a loop statement used to repeat some part of the code till the given
condition is fulfilled. It is a form of an exit-controlled or post-tested loop where the test
condition is checked after executing the body of the loop. Due to this, the statements in the
do…while loop will always be executed at least once no matter what the condition is. The do-
while loop is mainly used in the case where we need to execute the loop at least once. The do-
while loop is mostly used in menu-driven programs where the termination condition depends
upon the end user.
Syntax: #include <stdio.h>
int main()
Counter initialization {
{ int numt=1;
Statements to be {
executed repeatedly printf("%d \t", num);
num++;
Increment (++) or
} while (num <= 10);
Decrement (--) counter return 0;
} while (condition test); }
Output:
1 2 3 4 5 6 7 8 9 10
1. We have initialized a variable called num with value 1.
2. In the body of a loop, we have a print function to print our number and an increment
operation to increment the value per execution of a loop. An initial value of num is 1,
after the execution, it will become 2, and during the next execution, it will become 3.
This process will continue until the value becomes 10 and then it will print the series on
console and terminate the loop.
, 3. In a do while loop, we have provided a condition (num<=10), which means the loop will
execute the body until the value of num becomes 10. After that, the loop will be
terminated, and control will fall outside the loop.
For Loop in C
When you know exactly how many times you want to loop through a block of code, use
the for loop instead of a while.
1. The initial value of the for loop is performed only once. It is executed (one time) before
the execution of the code block.
2. 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. It defines the
condition for executing the code block.
3. The incrementation/decrementation increases (or decreases) the counter by a set value. It
is executed (every time) after the code block has been executed.
4.
Syntax: #include <stdio.h>
int main()
for (initial value; condition; {
incrementation or int i;
decrementation ) for (i = 0; i <= 10; i = i + +)
{ {
printf("%d \t", i);
statements;
}
} return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Difference between while and do while loop
While Do While
It checks the condition first and then This loop will execute the statement(s) at
executes statement(s) least once, then the condition is checked.
It is an entry controlled loop. It is an exit controlled loop.
The Keyword “while” is used. The keyword “do while” is used.
Do while loop allows initialization of
While loop allows initialization of counter
counter variables before and after starting
variables before starting the body of a loop.
the body of a loop.
We do not need to add a semicolon at the We need to add a semicolon at the end of
end of a while condition. the while condition.
, In case of a single statement, we do need to
Brackets are always needed.
add brackets.
Statement(s) can be executed zero times if
Statement is executed at least once.
the condition is false.
Generally while loop is written as: Generally do while loop is written as:
while (condition) { do{
Statements; // loop body Statements; //loop body
} } while (condition);
Difference between while and for loop
While Loop For Loop
It is used when the number of iterations is It is used when the number of iterations is
not known. known.
Initialization is always outside the loop. Initialization may be either in loop statement
or outside the loop.
In case of no condition, an error will be In case of no condition, the loop is repeated
shown. infinite times.
Condition may be expression or non-zero Condition is a relational expression.
value.
while ( condition ) { statement(s); } for ( init ; condition ; iteration ) {
statement(s); }
break and continue
break
The break statement ends the loop
immediately when it is
encountered.
Its syntax is: break;
This statement terminates the
smallest enclosing loop
(i.e., while, do-while, for loop,
or switch statement). Below is the
program to illustrate the same:
The break statement is almost
always used with if...else statement
inside the loop.