You are on page 1of 22

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, PIEAS
Infinite Loops

Dr. Yousaf, PIEAS


Infinite Loop
// Infinite Loops, Page 226
#include<stdio.h>
int main()
{
int i = 7;
while (1) /* any non zero number can be provided
here. It is an infinite loop */
{
printf("i = %d\n", i);
i++;
}
getchar(); return 0;
}
Dr. Yousaf, PIEAS
Infinite Loop
// Infinite Loops, Page 226
#include<stdio.h>
int main()
{
int i = 7;
do
{
printf("i = %d\n", i);
i++;
} while (1); /* any non zero number can be
provided here. It is an infinite loop */
getchar(); return 0;
}
Dr. Yousaf, PIEAS
Infinite Loop
// Infinite Loops, Page 226
#include<stdio.h>
int main()
{
int i = 7;
for (; ;)
{
printf("i = %d\n", i);
i++;
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
break and continue Statements

Dr. Yousaf, PIEAS


Break Statement
#include<stdio.h>
int main()
{
int i;

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


{
if ( i ==5)
break;
printf("i = %d\n", i);
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Break Statement
#include<stdio.h>
int main()
{
int i;

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


{
if ( i ==5) // what’s about if we write if ( i > 3)
break;
printf("i = %d\n", i);
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Break Statement
#include<stdio.h>
int main()
{
int i;

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


{
if ( i ==5) // what’s about if we write if ( i < 3)
break;
printf("i = %d\n", i);
}
getchar();
return 0;
} Dr. Yousaf, PIEAS
The break Statement
• break
– Causes immediate exit from a while, for,
do/while or switch structure
– Program execution continues with the first
statement after the structure
– Common uses of the break statement are:
• Escape early from a loop
• Skip the remainder of a switch structure

Dr. Yousaf, PIEAS


// Infinite loop using while and break statement
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number from 10 to 20\n");
while (1)
{
scanf("%d",&num);
if (num>= 10 && num <=20)
break;
else
printf("Invalid. Please enter an integer number from 10
to 20\n");
}
// Rest of the code goes here
Dr. Yousaf, PIEAS
// Infinite loop using for loop and break statement
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number from 10 to 20\n");
for( ; ;)
{
scanf("%d",&num);
if (num >= 10 && num <=20)
break;
else
printf("Please enter an integer number from 10 to 20\n");
}
// Rest of the code goes here
Dr. Yousaf, PIEAS
// Infinite loop using do while loop and break statement
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number from 10 to 20\n");
do
{
scanf("%d",&i);
if (num >= 10 && num <=20)
break;
else
printf("Invalid. Please enter an integer number from 10 to
20\n");
} while (1);
// Rest of the code goes here
Dr. Yousaf, PIEAS
Another Example: for and break Together
int mynum = 3;
int guess;
The notation for(;;) is
for(;;)
used to create an
{
infinite for loop.
printf("Guess my number:");
while(1) creates an
scanf("%d",&guess);
infinite while loop
if(guess==mynum)
instead.
{
printf("Good guess!\n");
break; To get out of an
} infinite loop like this
else one, we have to use
printf("Try again.\n"); the break statement.
}

Dr. Yousaf, PIEAS


continue Statement
#include<stdio.h>
int main()
{
int i;

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


{
if ( i ==5)
continue;
printf("i = %d\n", i);
}
getchar();
return 0; }
Dr. Yousaf, PIEAS
continue Statement
#include<stdio.h>
int main()
{
int i;

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


{
if ( i ==5)
continue;
printf("i = %d\n", i);
}
getchar();
return 0; }
Dr. Yousaf, PIEAS
continue Statement
#include<stdio.h>
int main()
{
int i;

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


{
if ( i ==5) // what’s about if we write if ( i > 3)
continue;
printf("i = %d\n", i);
}
getchar();
return 0; }
Dr. Yousaf, PIEAS
continue Statement
#include<stdio.h>
int main()
{
int i;

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


{
if ( i ==5) // what’s about if we write if ( i < 3)
continue;
printf("i = %d\n", i);
}
getchar();
return 0; }
Dr. Yousaf, PIEAS
The continue Statement
Used for skipping the remainder of the body of a
while, for or do/while structure and
proceeding with the next iteration of the loop
– while and do/while
• Loop-continuation test is evaluated
immediately after the continue statement is
executed
– for
• Increment expression is executed, then the
loop-continuation test is evaluated

Dr. Yousaf, PIEAS


exit() function

Dr. Yousaf, PIEAS


exit()
// About exit() function. Page 236
#include<stdio.h>
#include<stdlib.h> // to use exit() function.
int main()
{
int i, j, k;
for (i = 0; i<5; i++)
printf("%d\n", i);
getchar();
exit(0); // it will exit the program
j = 23;
printf("j = %d\n", j);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Continue and exit together
#include<stdio.h>
// Continue to enter integer values. This program
#include<stdlib.h>
//will terminate when you enter 5 odd numbers.
int main()
{
int num;
int count=0;
while(1)
{
printf("\nEnter an odd number :");
scanf("%d",&num);
if(num%2==0)
continue;
count++;
if(count==5)
exit(0);
}
getchar();
return 0;
} Dr. Yousaf, PIEAS

You might also like