contents · continue in a loop •is valid only within loops •terminates the current loop...
TRANSCRIPT
![Page 1: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/1.jpg)
Contents • What is nested loop
• Working of nested loop using flow chart
• Nested for loop
• Infinite loop
• The continue statement
![Page 2: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/2.jpg)
Continue statement in C
• The continue statement in C programming language works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.
• For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control passes to the conditional tests.
![Page 3: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/3.jpg)
The break and continue Statements
• The break statement causes an exit from the innermost enclosing loop or switch statement.
• The continue statement causes the current iteration of a loop to stop and the next iteration to begin immediately.
![Page 4: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/4.jpg)
Continue statement in C
![Page 5: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/5.jpg)
Example of continue in a loop
int main ( )
{
int i;
for (i = 1; i < 10; i++)
{
if (i == 5)
{
continue;
}
cout<<i;
}
cout<<“\nDone.”;
return 0;
}
OUTPUT:
1 2 3 4 6 7 8 9
Done.
![Page 6: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/6.jpg)
Continue in a loop
The continue statement in a loop will force the
program to check the loop condition immediately.
do {
printf( “do you want to continue?”); scanf(“%d”,ans);
if (and != “Y” && ans != “y”) continue;
printf(“Great?”);
break;
} while (true);
....
![Page 7: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/7.jpg)
Continue in a loop
• is valid only within loops
• terminates the current loop iteration, but not the entire loop
• in a For or While, continue causes the rest of the body statement to be skipped--in a For statement, the update is done.
• in a Do-While, the exit condition is tested, and if true, the next loop iteration is begun
![Page 8: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/8.jpg)
Loop example with continue
int x,y; for (x=1; x<5; x++) { for (y=1; y<5; y++) { if (y > x) break; cout<<“* ”; } if (x % 2) continue; cout<<“\n”; }
OUTPUT * * *
* * * * * * *
![Page 9: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/9.jpg)
Infinite Loops • An example of an infinite loop:
int main()
{
int count = 1;
while (count <= 25)
{
cout<< "value of count variable is:"<<count;
count = count - 1;
}
}
This loop will continue executing until interrupted
(Control-C) or until an underflow error occurs
NOTE: You can terminate an infinite loop by pressing
Ctrl + C keys.
![Page 10: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/10.jpg)
The for Statement
• Each expression in the header of a for loop is optional
• If the initialization is left out, no initialization is performed
• If the condition is left out, it is always considered to be true, and therefore creates an infinite loop
– We usually call this a “forever loop”
• If the increment is left out, no increment operation is performed
![Page 11: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/11.jpg)
Infinite Loops
• The body of a while loop eventually must make the condition false
• If not, it is called an infinite loop, which will execute until the user interrupts the program
• This is a common logical (semantic) error.
• You should always double check the logic of a program to ensure that your loops will terminate normally
![Page 12: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/12.jpg)
Nested Loops
• Similar to nested if statements, loops can be nested as well
• That is, the body of a loop can contain another loop
• For each iteration of the outer loop, the inner loop iterates completely
![Page 13: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/13.jpg)
Nested Loops • How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
printf("Here");
count2++;
}
count1++;
} 10 * 20 = 200
![Page 14: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/14.jpg)
Flow chart of nested loop
![Page 15: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/15.jpg)
Scopes of Variables
int main()
{
........
........
int i=0;
cout<<“\n”<<i;
for (i = 1; i < 5; i++)
{
cout<<"loop in :: “<<i;
i+=2;
cout<<"loop end :: “<<i;
}
cout<<i;
return 0;
}
0
loop in :: 1
loop end :: 3
loop in :: 4
loop end :: 6
7
Press any key to
continue
![Page 16: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/16.jpg)
Loops and Scopes
int main()
{
int i=0,j;
Cout<<\n”<<i;
for (i = 1; i < 5; i++)
{
cout<<"loop in :: \n“<<i;
for (j = i; j < 3; j++)
{
cout<<" inner for loop j :: \n“ <<j;
}
cout<<"loop end :: \n“<<i;
}
cout<<i;
return 0;
}
0
loop in :: 1
inner for loop j :: 1
inner for loop j :: 2
loop end :: 1
loop in :: 2
inner for loop j :: 2
loop end :: 2
loop in :: 3
loop end :: 3
loop in :: 4
loop end :: 4
5
![Page 17: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/17.jpg)
Nested loops (loop in loop)
int main()
{
int a,b,i,j;
cout<<"enter value a”;
cin>>a;
cout<<"enter value of b";
cin>>b;
for(i=0; i<a;i++)
{
for(j=0; j<b; j++)
{
cout<<"*";
}
cout<<“\n”;
}
Return 0;
}
*************
*************
*************
*************
b
a
![Page 18: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/18.jpg)
Empty Box void main() { int row, col; clrscr();
for(row=0; row<5; row++) { for(col=0; col<7; col++) { if(row>0 && row<5-1) { if(col>0 && col<7-1) { printf(" ---"); } else printf("*"); } else { printf("*"); } } printf("\n"); } getch(); }
![Page 19: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/19.jpg)
Nested loops (2) *
**
***
****
b
a
int main()
{
int i,j;
clrscr();
for(i=1; i<=4;i++)
{
for(j=1; j<=i;j++ )
{
cout<<" *";
}
cout<<"\n";
}
Return 0;
}
![Page 20: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/20.jpg)
Triangle
int main()
{ int row,col;
int no_ofrow, space;
cout<<"enter rows";
cin>>no_ofrow;
for(row=1;row<=no_ofrow; row++)
{
for(space=no_ofrow-row; space>=1; space--)// row= current row
{
cout<<" ";
}
for(col=1;col<=row; col++) //col<=(row*2)-1
{
cout<<"* ";
}
cout<<"\n";
}
}
![Page 21: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/21.jpg)
C Program to print half pyramid as using numbers
int main() { int i,j; for(i=1; i<=5;i++) { for(j=1; j<=i; j++ ) { cout<< j; } cout<<"\n"; } return 0; }
![Page 22: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/22.jpg)
C Program to print inverted half pyramid as using numbers
int main() { int i,j; clrscr(); for(i=5; i>=1;i--) { for(j=1; j<=i;j++ ) { cout<< ",j; } cout<<"\n"; } return 0; }
![Page 23: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/23.jpg)
C Program to display Floyd's Triangle.
int main() { int i,j,k=1; for(i=0; i<=4;i++) { for(j=0; j<i; j++ ) { cout<<k; k++; } cout"\n"; } return 0; }
![Page 24: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/24.jpg)
Odd Number Triangle
1
3 5
7 9 11
13 15 17 19
int main() { int i,j,k=1; for(i=1; i<=4;i++) { for(j=1;j<=i;j++) { cout<<k; k=k+2; } cout<<"\n"; } return 0; }
![Page 25: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/25.jpg)
Print character pyramid int main()
{
int n, i, j;
char temp='A';
for(i=1; i<='E'-'A'+1; i++)
{
for(j=1; j<=i; j++)
{
cout<<temp;
}
temp++;
cout<<"\n";
}
}
![Page 26: Contents · Continue in a loop •is valid only within loops •terminates the current loop iteration, but not the entire loop •in a For or While, continue causes the rest of the](https://reader034.vdocuments.mx/reader034/viewer/2022042917/5f5aee5fe93b3b7cfd75df96/html5/thumbnails/26.jpg)
Count number of digits
int main()
{
long int num;
int i=0;
cout<<"enter digits";
cin>>num;
while(num!=0)
{
num=num/10;
i++;
}
cout<<"lenght is"<<i;
return 0;
}