loops 2
TRANSCRIPT
-
8/8/2019 Loops 2
1/33
Loops
Pretest loops
post test loops
-
8/8/2019 Loops 2
2/33
Pretest loop- first test condition should be
evaluated if it true executes the body of the
loop.-while and for loopspost test loop- first body of the loop is executed
after that condition would be tested.
do-while.
-
8/8/2019 Loops 2
3/33
Looping process include 4 steps
1.Loop counter initialization
2. test the condition.
3. execute the body of the loop.4.increment or decrement the loop counter.
-
8/8/2019 Loops 2
4/33
While Statements
The "while" loop is a generalized looping structure that employs avariable or expression for testing the ending condition.
Is a repetition statement that allows an action to be repeated while some
conditions remains true. Variables used in the loop control testing must be initialized
Testing for the termination condition is done at the top of the while() loop
The body of WHILE statement can be a single statement or compound
statements
Syntax:
Form 1: Simple Statement
while (condition)
statement;Form 2: Compound Statements
while (condition)
{ s1; s2; s3; ..}
-
8/8/2019 Loops 2
5/33
While statement
The statement (or a block of statements) is executedThe statement (or a block of statements) is executed
repetitively until the condition becomes false.repetitively until the condition becomes false.
while ( condition )
statement;
whilewhile is ais a
reserved wordreserved word If the condition is true, theIf the condition is true, the
statement is executed.statement is executed.
Then the condition isThen the condition is
evaluated again.evaluated again.
-
8/8/2019 Loops 2
6/33
Tips on while loop
1. Statements within the while loop would keep on executed until the conditionbecomes false. Then the control passes to the first statement after the while loop.
2. Condition may be any valid expression
while(i=10&&j
-
8/8/2019 Loops 2
7/33
4. Condition must eventually become false.otherwise the loop would be executedforever indefinitely.
main()
{
int i=1;
while(i
-
8/8/2019 Loops 2
8/33
5. Loop counter can also decremented to execute the body of theloop repeatedly.
main()
{
int x=10;
while(x>=1)
{
printf(%d\n,x);
x=x-1;
}//while
}//main
-
8/8/2019 Loops 2
9/33
6. Loop counter can also be float need not be in int.
main()
{
float x=2.0;while(x
-
8/8/2019 Loops 2
10/33
What is the output of the following program.
main()
{
int x=1;while(x
-
8/8/2019 Loops 2
11/33
No, it doesnt print numbers from 1 to 32767
goes to indefinite loop.
main()
{
int x=1;
while(x
-
8/8/2019 Loops 2
12/33
Program to compute y=xpow n
main()
{
int i=1,n,x,y=1;
printf(enter the value of x:);
scanf(%d,&x);
printf(enter the n value);
scanf(%d,&n);
while(i
-
8/8/2019 Loops 2
13/33
Nesting of while loops
While loops within the other while loop.
Write a program to generate the given format
using while loops1
2 3
4 5 67 8 9 10
-
8/8/2019 Loops 2
14/33
main()
{
int n,i=1,j=1,k=1;
printf(\n enter the n value);
scanf(%d, &n);
while(i
-
8/8/2019 Loops 2
15/33
The do while statement
Statements in the loop are executedfirst (at least once, and condition istested last
Loop is controlled by a condition orcounter
Syntax
do {
statement;
statement;
} while (condition);
statement;
-
8/8/2019 Loops 2
16/33
While example
#include
int main(){
int i = 1, sum = 0;
while ( i 1);
printf(Factorial of 5=%d,result);
}
-
8/8/2019 Loops 2
17/33
Program to reverse a number
main()
{
int n,x,y=0;
printf(enter the number);
scanf(%d, &n);
do{
x=n%10;
y=(y*10)+x;
n=n/10;
}while (n>0);printf(\n the reverse number is %d,y);
getch();
}//main
-
8/8/2019 Loops 2
18/33
The for Loop
When the number of passesthrough a loop is known inadvance, a for statement is often
used
for(expr1; expr2; expr3)
statement;
expr1 controls the looping action,expr2 represents a condition that
ensures loop continuation, expr3modifies the value of the controlvariable initially assigned by expr1
When a for statement is executed, expr2 is evaluated and testedat the beginning of each pass through the loop. expr3 is
evaluated at the end of each passIf the loop continuation condition is initially false, the body partof the loop is not performed
Any of the three parts can be omitted, but the semicolons mustbe kept
-
8/8/2019 Loops 2
19/33
for (i=1; i
-
8/8/2019 Loops 2
20/33
Few points on for loop
1. Counter variable is initialized only once when thefor statement is executed for the first time.
2. The condition is tested. If it is true the body of theloop is executed.
3. When it reaches to the closed braces, the control issend back to the for statement, where the value ofthe variable gets incremented or decremented
4. Again the test is performed, if true it enters into the
next iteration.5. The body of the loop executed repeatedly until the
condition becomes false.
-
8/8/2019 Loops 2
21/33
Few points on for loop
6. initialization, testing and increment part of a loop can be replaced by anyvalid expression.
ex:
for(x=10;x;x--)
for(i
-
8/8/2019 Loops 2
22/33
Few tips on for loop7. We can omit any part of the for statement but we need to keep semicolons
int I;
for(i=1;i
-
8/8/2019 Loops 2
23/33
Nested forloops
Nested means there is a loop within a loop
Executed from the inside out
Each loop is like a layer and has its own counter variable, its own loopexpression and its own loop body
In a nested loop, for each value of the outermost counter variable, thecomplete inner loop will be executed until it satisfies its condition.
General formfor(loop1_exprs) {
loop_body_1a
for(loop2_exprs) {
loop_body_2
}
loop_body_1b
}
Most compilersallow 15 nestinglevels DONTDO IT!!
-
8/8/2019 Loops 2
24/33
#include
#include
int main()
{
int n,i=1,j=1;
clrscr();
printf(enter n value\n);
scanf(%d,&n);
for(i=1;i
-
8/8/2019 Loops 2
25/33
Output
enter the n value
12 2
3 3 3
4 4 4 4
-
8/8/2019 Loops 2
26/33
Multiple initialization of for loop
* The initialization part of the for loop can have more than onevariable initialization separated by a comma.
ex:
for(i=0,j=0;j
-
8/8/2019 Loops 2
27/33
Program to find whether the
number is prime or not#include#include
int main()
{
int n,i,counter=1;
clrscr();
printf(enter the number\n);
scanf(%d,&n);for(i=1;i
-
8/8/2019 Loops 2
28/33
Break statement
-When you want to jump out of the loop
instantly use keyword break
-W
hen a break is used inside the loop, controlautomatically passes to the statement which
immediately follows the loop.e
-
8/8/2019 Loops 2
29/33
Write a program to whether a given
number is prime or notmain()
{
int n,i=2;
printf(enter the number\n);
scanf(%d, &n);
while(i
-
8/8/2019 Loops 2
30/33
GOTO statement
Used to alter the sequence of program execution by transferring control(jump) to some other parts of the program.
The goto statement in C allowsthe transfer of control from one
position to another position in the
same program.
The destination must be marked
with a label.
The general syntax:
label: statement;
The syntax for goto:
goto label;
Main()
{
. . .
If (. . . ) goto End;
. . .
End:
. . .
}
The goto statement
allows one to jumpfrom any place to
anywhere else within afunction.
-
8/8/2019 Loops 2
31/33
- Using goto we can move from forward to backward and vice-versa
example
int i=0,total=0;
sum:total=toatal+i;
i++;
if(i
-
8/8/2019 Loops 2
32/33
Continue statement
- To take the control to the beginning of the loop, by skippingsome statements inside the loop.
- use keyword continue
ex:-
int i=1,j=0;While(i
-
8/8/2019 Loops 2
33/33
BREAK and CONTINUE
- break; causes an exit from the innermost loop or switch.terminate : for, while, do while, and switch
- continue; causes the current iteration of a loop to stop
and the next iteration to begin immediately.effective only in : for,while and do while
Break Example
.....while (1){
scanf("%f", &x);
if (x < 0.0)
break;
/* exit loop if the value is
negative */printf("\n%f", sqrt(x));
....
}
/* break jumps to here */
Continue Example
. . . . . .
for (i = 0; i < TOTAL; ++i){
c = getchar();
if ( '0'