Friday, 1 July 2022

Complexity of Infinite Loop

Infinite Loop in C                           

"Infinite Loop in C-code means the loop does not terminate."


Example 1.

C-programing in Linux platform:

1. Open a text-editor -> Write C-code -> Press (ctrl+shift+s) -> type filename.c -> Click on save .
2. Open command prompt (a) For compilation: Type gcc filename.c (b) For execution: Type ./a.out

There are mainly four types of infinite loop in C-programming.

(i) By calling the main function into the main().
(ii) By using for-loop.
(iii) By using while-loop.
(iv) By using do-while-loop.   

# include<stdio.h>
void main ()
{
printf ("hello");
main();
}

The output of this C-programming: hellohellohello--------------till interrupt (ctrl+z). 

#include<stdio.h>
int main()
{
for(;;)
{
printf("hello \n");
}
return(0);
}


#include<stdio.h>
int main()
{
while(1)
{
printf("hello\n");
}
return(0);
}

#include<stdio.h>
int main()
{
do
{
printf("hello\n");
}
while(1);
return(0);
}

If the return type of main() function is an integer then we must include a return(0) function in our code.  

On Windows Platforms (Turbo C++):

Example 2.

#include<stdio.h>
#include<conio.h>
void main ()
{
for (;;)
{
printf ("hello");
}
clrscr ();
getch ();
}

In "Turbo c++" compiler, we must include getch() and clrscr() function for displaying the output on clear screen.
 

Home

Soft Computing Laboratory Assignments-I, II, III (click here) PRINCIPAL COMPONENT ANALYSIS (PCA) (click here) Soft Computing Laboratory Assi...