loop statements in c - wordpress.com · • a loop allows one to execute a statement or block of...

22
LOOP STATEMENTS IN C

Upload: others

Post on 04-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

LOOP STATEMENTS IN C

Page 2: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

• A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded iteration or unbounded loop and bounded iteration or bounded loop.

• A loop can either be a pre-test loop or be a post-test loop as illustrated in the diagram.

Iteration and Repetitive Execution

Page 3: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

while statement is a pretest loop. The basic syntax of the while statement is shown below:

“while” loop construct

Page 4: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

#include <stdio.h>

int main()

{

int c;

c=0; // Initialization

while(c<5) // Test Expression

{

printf(“ \nHello”);

c++; // Updating

}

return 0;

}

This loop contains all the parts of a while loop. When executed in a program, this loop will output

Hello Hello Hello Hello Hello

An Example

Page 5: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

“for” Loop Construct

• The general form of the for statement is as follows:

for(initialization; TestExpr; updating)

{

stmT;

}

for construct

flow chart

Page 6: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

Example

#include <stdio.h> int main() { int c; for(c=0;c<5;c++) // initialization,Test Expression, updating { printf(“ \nHello”); } return 0; }

Page 7: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

While and For loop syntax

• for(initialization; TestExpr; updating) { stmT; for loop } • Initialization; while(TestExpr) { stmT; while loop Updating; }

Page 8: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

Initialization;

do

{

stmT;

updating;

}while(TestExpr);

“do-while” loop Construct

Page 9: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

#include <stdio.h>

int main()

{

int c;

c=0; // Initialization

do

{

printf(“ \nHello”);

c++; // Updating

}while(c<5);

return 0;

}

This loop contains all the parts of a while loop. When executed in a program, this loop will output

Hello Hello Hello Hello Hello

An Example

Page 10: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

Test

Condition

True

False

Body of The Loop

Entry

Statement

False

True

Entry

Exit Control

Body of The Loop

Test

Condition

PRE-TEST AND POST-TEST LOOPS

ENTRY EXIT

CONTROLLED LOOP CONTROLLED LOOP

Page 11: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

Point to Note

With a do-while statement, the body of the loop is executed first and the test expression is checked after the loop body is executed. Thus, the do-while statement always executes the loop body at least once.

Page 12: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

A nested loop refers to a loop that is contained within another loop.

If the following output has to be obtained on the screen

1

2 2

3 3 3

4 4 4 4

then the corresponding program will be

#include <stdio.h>

int main()

{

int row, col;

for(row=1;row<=4; row++)

{

for(col=1;col<=row; col++)

printf(“%d \t”, row);

printf(“\n”);

}

return 0;

}

NESTED LOOPS

Page 13: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

SPECIAL CONTROL STATEMENTS/JUMP STATEMENTS

“goto” statements

“break” statements

“continue” statements

Page 14: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

Examples using while loop

• WAP to calculate the sum of first 10 numbers

• WAP to print 5 times table

• WAP to accept a number from the user and calculate the sum of its digits

• WAP to accept a number from the user and check whether it is an Armstrong number or not.

• WAP to accept a number from the user and display its binary equivalent.

Page 15: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

Examples using for loop

• WAP to calculate the sum of first 100 numbers

• WAP to accept ‘n’ numbers from the user and calculate its sum and average.

• WAP to accept a number from the user and calculate its factorial.

• WAP to calculate the first ‘n’ terms of the Fibonacci series where ‘n’ is given by the user.

• WAP to calculate the value of Xn

Page 16: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

Examples using for loop

• WAP to calculate the sum of the following series upto n terms

1/2+ 3/4 + 5/6 + 7/8 + …….. +n

• WAP to calculate the sum of the following series upto n terms

x + x2/2 + x3/3 + x4/4 + …… + n

• WAP to calculate the sum of the following series upto n terms

x + x2/2! + x3/3! + x4/4! + …… + n

Page 17: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

The control is unconditionally transferred to the statement associated with the label specified in the goto statement. The form of a goto statement is

goto label_name;

The following program is used to find the factorial of a number.

#include <stdio.h> int main() { int n, c; long int f=1; in: printf(“\n Enter the number:”); scanf(“%d”,&n); if(n<0) goto in: for(c=1; c<=n; c++) f=f*c; printf(“\n FACTORIAL IS %ld”, f); return 0; }

GOTO STATEMENT

Page 18: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

Break Statement

When the break statement is encountered inside a loop , the loop is immediately exited and program continues with the statement immediately following the loop

Page 19: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

19 Department Of Information

Technology

While(…………)

{. . . . . .

If (condition)

break;

. . . . . .

}

. . . . . .

do

{. . . . . .

If (condition)

break;

. . . . . .

} While(…………);

. . . . . .

For(…………)

{

. . . . . .

If (error)

break;

. . . . . .

}

. . . . . .

for(. . . . . . .)

{. . . . . . .

for(…………)

{. . . . . .

If (condition)

break;

. . . . . .

}

. . . . . . }

Exit

From

loop

Exit

From

loop

Exit

From

loop

Exit

From

Inner

loop

Page 20: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

Continue Statement

Causes the loop to be continued with the next iteration after skipping any statements in between

Page 21: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

21 Department Of Information

Technology

while (Test Condition)

{. . . . . .

if (. . . . . .)

continue;

. . . . . .

}

do

{. . . . . .

if (. . . . . .)

continue;

. . . . . .

} while (Test Condition);

for ( initialization; test condition ; increment)

{

. . . . . .

if(. . . . . . . )

continue;

. . . . . . .

. . . . . . .

}

Page 22: LOOP STATEMENTS IN C - WordPress.com · • A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded

“break” and “continue” statements

break continue

1. It helps to make an early exit from the block where it appears.

1. It helps in avoiding the remaining statements in a current iteration of the loop and continuing with the next Iteration

2. It can be used in loop constructs statements and switch construct.

2. It can be used only in loop constructs.