[go: up one dir, main page]

0% found this document useful (0 votes)
3 views9 pages

Looping Statements

The document provides an overview of loop statements in programming, specifically focusing on C language. It explains the structure and types of loops, including while, do-while, and for loops, along with their syntax and examples. Additionally, it discusses nested loops and various idioms for using for statements effectively.

Uploaded by

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

Looping Statements

The document provides an overview of loop statements in programming, specifically focusing on C language. It explains the structure and types of loops, including while, do-while, and for loops, along with their syntax and examples. Additionally, it discusses nested loops and various idioms for using for statements effectively.

Uploaded by

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

INTRODUCTION- LOOP STATEMENTS

Iterations or loops are used when we want to execute a statement or block of statements
several times. The repetition of loops is controlled with the help of a test condition. The
statements in the loop keep on executing repetitively until the test condition becomes
false. Looping is a process of repeating a single statement or a group of statements
until some condition for termination of the loop is satisfied.

There are four Parts of a loop -


1. Initialization.
2. Conditions
3. Statements
4. Incrementation or Decrementation.

Type of loops -
1.Entry control loops: Those loops in which condition is checked before the execution of the
statement. Thus if the condition is false in the beginning the loop will not run even once.
e.g. for loop, while loop.
2.Exit control loops: Those loops in which the condition is checked after the execution of the
statement. Thus if the condition is false in the beginning the loop will run at least once.
e.g. do while loop
Definition of a loop

A loop is a statement whose job is to repeatedly execute some other statement (the loop body).
In C, every loop has a controlling expression. Each time the loop body is executed (an
iteration of the loop), the controlling expression is evaluated. If the expression is true the loop
continues to execute.

C provides three iteration statements


 While, do and for.
 The while statement is used for loops whose controlling expression is tested before
the loop body is executed.
 The do statement is used if the expression is tested after the loop body is executed.
 for statement is convenient for loops that increment or decrement a counting variable.

ITERATION STATEMENTS
Iterations or loops are used when we want to execute a statement or block of statements
several times. The repetition of loops is controlled with the help of a test condition. The
statements in the loop keep on executing repetitively until the test condition becomes false.
There are the following three types of loops:-
1. While loop
2. Do-while loop
3. For loop

1.while loop
 Provides a mechanism to repeat one or more statements while a particular condition is true.
 In while loop, the condition is tested before any of the statements in the statement block is
executed.
 If the condition is true, only then statements will be executed otherwise if the condition is
false, the control will jump to the statement y, that is the immediate statement outside the
while loop.
 while loop will execute as long as the condition is true.
 It is referred as top- checking loop since control condition is placed as the first line of code. if
the control condition evaluates to false, then the statements enclosed in the loop are never
executed.

Syntax

while(condition)
{
statement(s);
}

Here, statement(s) may be a single statement or a block of statements. The condition may
be any expression, and true is any nonzero value. The loop iterates while the condition is
true. When the condition becomes false, the program control passes to the line immediately
following the loop.

Here, the key point to note is that a while loop might not execute at all. When the condition
is tested and the result is false, the loop body will be skipped and the first statement after the
while loop will be executed.
Example: 1

Write a C program to initialize the value of a=10, Till the value reaches 19 execute the loop
and increment the value of a’ by 1 . When the value of a=20, ext from loop. Use the while
loop.

Pseudo code:

1. initialize the value of a=10.


2. Till the value reaches 19 execute the loop.
3. Print the value of ‘a’ in every iteration.
4. increment the value of a’ by 1 inside the loop.
5. If the value reaches 20, exit from the loop

#include<stdio.h>
int main ()
{
int a =10;/* local variable definition */
while( a <20)/* while loop execution */
{
printf("value of a: %d\n", a);
a++;
}
return0;
}

OUTPUT
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

2. do-while loop
The do-while statement is also used for looping. It is similar to while loop and is used for the
problems where it is not known in advance that how many times the statement or block of
statements will be executed. However, unlike while loop, in case of do-while, firstly the
statements inside the loop body are executed and then the condition is evaluated. As a result
of which this loop is executed at least once even if the condition is initially false. After that
the loop is repeated until the condition evaluates to false. Since in this loop the condition is
tested after the execution of the loop, it is also known as post- test loop.
The do-while loop always executes its body at least once, because its conditional expression
is at the bottom of the loop. Its general form is:

do
{
// body of loop
} while(condition);

Each iteration of the do-while loop first executes the body of the loop and then evaluates the
conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop
terminates. As with all of C’s loops, condition must be a Boolean expression.

Example

#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}while (j<=3);
return 0;
}
Output:

Value of variable j is: 0


Value of variable j is: 1
Value of variable j is: 2
Value of variable j is: 3

Difference between while and do-while loop

3.FOR loop
 it is actually the best way to write many loops.
 The for loop is ideal for loops that have a “counting” variable, but itis versatile
enough to be used for other kinds of loops as well.
 “For loop” is constructed from a control statement that determines how many times
the loop will run and a command section. Command section is either a single
command or a block of commands.

Syntax
for loop is also an entry control loop which provides a more concise loop control structure.
The loop is used as:

for (initialization expr; test expr; update expr)


{
// body of the loop
// statements we want to execute
}

In for loop, a loop variable is used to control the loop. First initialize this loop variable to
some value, then check whether this variable is less than or greater than counter value. If
statement is true, then loop body is executed and loop variable gets updated . Steps are
repeated till exit condition comes.
 Initialization Expression: In this expression we have to initialize the loop counter to
some value. for example: int i=1;
 TestExpression: In this expression we have to test the condition. If the condition
evaluates to true then we will execute the body of loop and go to update expression
otherwise we will exit from the for loop. For example: i <= 10;
 Update Expression: After executing loop body this expression
increments/decrements the loop variable by some value. for example: i++;

//example program to illustrate more than one statement using the comma for looping
#include <stdio.h>
int main ()
{ int a, b;
for (a = 1, b = 4; a < b; a++, b--)
{
printf("a = %d \n", a);
printf("b = %d \n", b);
}
}
output
a=1
b=4
a=2
b=3
Here, the initialization portion sets the values of both a and b. The two comma separated
statements in the iteration portion are executed each time the loop repeats.

EXAMPLE
/* program to generate the Fibonacci series*/ #include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
int i,n; x=0; y=1;
printf("enter the number of terms");
scanf("%d",&n);
printf("%d",y);
for(i=1;i<n;i++)
{
z=x+y;
printf(" %d ",z);
x=y;

y=z;
}
printf("\n");
}
OUTPUT
enter the number of terms6
1 1 2 3 5 8

Enter type of table:If the user wants a “7 times” table, he will enter 7. The program will then
go ahead andproduce a “7 times” table. The following Program shows how.

#include <stdio.h>
int main()
{
int factor;
printf("Type of table? ");
scanf("%d", &factor);
for (int m = 1; m <= 12; m++)
printf("%2d x %d = %2d\n", m, factor, m * factor);
}
output
Type of table? 7

1x7=7
2 x 7 = 14
3 x 7 = 21
4 x 7 = 28
5 x 7 = 35
6 x 7 = 42
7 x 7 = 49
8 x 7 = 56
9 x 7 = 63
10 x 7 = 70
11 x 7 = 77
12 x 7 = 84

For statement idioms

Omiting expressions in a for statement


Case 1:
If the first expression is omitted , no initialisation is performed before the loop is executed.

Example
i=10;
for ( ; i>0; --i)
printf(“%d ”, i);

in this example, ‘i’ has been initialised by a separate assignment, so we have omitted the first
expression in the for statement.

Case 2:
If we omit the third expression in a for statement, the loop body is responsible for ensuring
that the value of the second expression eventually becomes false.
for (i=10; i>0; )
printf(“%d \n”, i--);

‘i’ has been initialised by a separate assignment, so we have omitted the first expression in
the for statement.
Case 3:
When the first and third expressions are both omitted, the resulting loop is nothing more than
a while statement. For example the loop

for( ; i>0; )
printf( “%d”, i--)

is same as

while (i>0)
printf( “%d”, i--)

When the first and third expressions are both omitted, the resulting loop is nothing
morethan a while statement.

Case 4 : for(;;)
Some programmers use the following forstatement to establish in infinite loop

4.Nested Loops
Loops can be nested as per requirement. Here is example of nesting the loops.

// nested loops
#include <stdio.h>
int main ()
{
int i, j;
for (i = 0; i < 8; i++)
{
for (j = i; j < 8; j++)
printf(" * ");
printf("\n");
}
}
Here, two for loops are nested. The number times inner loop iterates depends on the value
of i in outer loop.
The output of the program is:

* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

You might also like