0% found this document useful (0 votes)
6 views

Loops Flow

Uploaded by

kotaa6731
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)
6 views

Loops Flow

Uploaded by

kotaa6731
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/ 5

Flowcharts of the Loops:

For Loop:

Initialization

Condi- FALSE

tion

TRUE

Statements

Incrementation

Rest of Program

While Loop:

Condi- FALSE

tion

TRUE

Statements Rest of Program

Page 1 of 5
do..while Loop:

Statements

Condi- FALSE
tion

TRUE Rest of Program

Page 2 of 5
break statement:
The break keyword used to terminate a loop. It terminates the loop statements block execution
though the loop condition present stuts is true.
Generally, break appears in association with a condition. The condition that is different than loop
condition.
The break can also be used to terminate execution of indefinite loops.
The following is the format of indefinite loops.
for ( ; ; ) /* no initialization, condition and incrementation */
{
statements;
}

while(1) /* The value 1 always indicates true state */


{
statements;
}

do
{
statements;
} while(1);

The following program verifies if the given number num is prime. The for loop terminates when the
first divisor found between 2 and num-1.

#include <stdio.h>
void main()
{
int num, I;
printf(“Enter number : “);
scanf(“%d”, &num);

for (i=2; i<num; i++)


{
if (num % i == 0)
break;
}
if (i < num)
printf(“%d is not prime \n”, num);
else
printf(“%d is prime \n”, num);
}

Page 3 of 5
Another example program to illustreate break given below.

/* Program to print sum of given numbers until sum > 100 */


#include <stdio.h>
void main()
{
int num, sum=0;
while(1)
{
printf("Enter number : ");
scanf("%d", &num);
sum += num;
printf("Current Sum %d \n", sum);
if(sum > 100)
break;
}
}

continue:
continue is another keyword that can be used in loops.
The continue keyword skips execution of the statements block that exists after it.
The continue generally used in association with a condition where the condition differ from
loop condition.

In for loop, when continue executed, it sends the control to loop incrementation (step)
statement.
In while and do..while loops, continue sends the control to verify the loop condition status.

for (initialization; condition; increment)


{
statements-I;
if (condition)
continue;

statements-II;
}

while (condition)
{
statements-I;
if (condition)
continue;
statements-II;
}

do
{
statements-I;
if (condition)
continue;
statements-II;
} while (condition);
Statements-II block execution skipped when continue is executed.

Page 4 of 5
The following is example program to illustrate use of continue keyword.
The program adds sum of even numbers. It skips adding the number to sum when the number is
odd.

/* Program to read any 10 numbers and print sum of evens */


#include <stdio.h>

void main()
{
int num, i, sum=0;

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


{
printf("Enter number : ");
scanf("%d", &num);

if (num % 2 != 0 )
continue;
sum+=num;
}
printf("\nSum of even numbers : %d", sum);
}

Page 5 of 5

You might also like