in Algorithms edited by
1,471 views
1 vote
1 vote
What is the time complexity for infinite loops

Question 1  what is T(n) for this case

While(1)

{

a=a+b;

}

 

Question 2 for this case

if(1)

{

for i to n

a=a+b

}

else

{

for i to n

for j to n

a=a+b

}

 

Edit 2: Compiled the code never goes to the else part

#include<stdio.h>
int main()
{
int a=2,b=6;
if(1)
{
a=a+b;
}
else
{
b=a;
printf("Test");
}
printf("%d %d",a,b);
return 0;
}

output I get is 8 6 which means the else case is never executed hence in worst case do we have to consider the else part.
in Algorithms edited by
by
1.5k views

4 Comments

And for question 1, there is no input. Since the program is in an infinite loop, there is no time complexity. And the runtime is infinite. 

0
0
@Soumya Tiwari I am also thinking on similar lines not sure hence want clarification.Subject experts please help.
0
0
yes , right!!
0
0

Please log in or register to answer this question.