Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Contents • What is nested loop
• Working of nested loop using flow chart
• Nested for loop
• Infinite loop
• The continue statement
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.
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.
Continue statement in C
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.
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);
....
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
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 * * *
* * * * * * *
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.
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
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
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
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
Flow chart of nested loop
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
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
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
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(); }
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;
}
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";
}
}
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; }
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; }
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; }
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; }
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";
}
}
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;
}