What are the cases in which a loop cannot be unrolled? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What are the cases in which a loop cannot be unrolled? I've been reading a paper which shows a loop that it says is not able to be unrolled. I cannot actually post the specific code as it is private, however, I am wondering if there is something obvious I am missing in regards to not being able to unroll.
Thanks in advance. If there's any other info that I can try to provide, let me know.

well you can't unroll a loop with any type of recursion in it because it could be infinitely long, also you cant unroll an infinite loop or one with some kind of method for breaking out that isn't incremental
recursion:
method(int x){
if(x > 0)
return 0;
else
return method(x-1);
infinite loop:
while(true){
...
if(some condition)
break;
}
last one:
boolean somevar = true;
while(somevar){
...
if(some condition)
somevar = false;
}

Related

I can't figure out why this code works fine [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
C newbie here. Was wondering why doesn't this code throw and error. It has missing parentheses and comparison between characters.
#include <stdio.h>
int main(){
while ('a'<'b')
printf("-");
return 0;
}
'a' and 'b' are character integer constants (6.4.4.4) and comparable like any other pair of integers. That comparison is probably evaluated at compile-time leaving you with an infinite loop:
while(1) print("-");
The answer is simple, if you are not using the curly brackets to create a block of code you if's while's etc... will execute the first line of code after the condition\loop etc...
while( condition )
func1();
But this is bad practice, you can fool yourself thinking that the example below will execute in a loop the 3 lines of code, in reality it will execute inside the while just the first one:
while( condition )
func1(); //<- just this line will be executed inside the while loop.
func2();
func3();
Correct way:
while( condition )
{
func1();
func2();
func3();
}

C programming : Stackoverflow is a cause of abrupt termination [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
In case of stack overflow in C programming, why do we tell it abrupt termination of loop. Shack-overflow is not a cause of infinite looping. But causing abrupt termination, though we cannot see, where is it terminating.
right??
Say for this program, it is causing a stack overflow but not a case of infinite looping
int foo(int val) {
int x=0;
while(val > 0) {
x = x + foo(val--);
}
return val;
}
But why we cannot say it as an infinite looping??and why is it saying as abrupt termination??
Since this is recursion, every call of the function foo() will increase the stack. And as we know the loop never ends so at some point stack will increase so much that the OS will terminate the code and give stack-overflow error.

Why not use a while loop for FIZZBUZZ in C? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Only been coding in C for about a month, but wondered why the while loop wasn't used more often for FIZZBUZZ. Is it not as clean or some other reason? Here's what I came up with:
int main()
{
int num=1;
while(num<=100)
{
if (num%3==0 && num%5==0)
{
printf("FIZZBUZZ!!!\n");
}
else if (num%3==0)
{
printf("FIZZ!!!\n");
}
else if (num%5==0)
{
printf("BUZZ!!!\n");
}
else
{
printf("%d\n", num);
}
num++;
}
return 0;
}
Your loop can be neatly folded into a for loop:
for(int num = 1; num <= 100; ++num)
There are two advantages here:
num is scoped inside the loop, when before it was bleeding into whatever scope followed the while. Restricting variables to the minimum possible scope is a good rule of thumb, because it minimizes the amount of variables to think about at any given point.
The range over which your program will operate is now summarized in a single place: you can see the bounds (1 to 100) and the step (1) inside the for. That kind of ranges is pretty well balanced to be read and memorized quickly before reading the body of the loop. For example, if you now wanted to only check odd numbers, it would be immediately clear from reading the num += 2 in the for header, rather than stumbling upon it at the very end of the loop's body.

C - while(true) vs while(condition) - performance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I would like to know if there is a difference in performance between :
while(true)
{
.....
}
And :
bool x;
x = true;
while(x)
{
.....
}
I need the best performance and a small difference between the two is important to my application.
Info from comment by OP:
The while(true) will at some point also be left, that is however rare.
If you need a truly endless loop, then why use a condition?
If you need a loop which can be left, then your while(true){...} will contain an if(!x) which your while(x) does not contain.
Any potential optimisation benefit of while(true) over while(x) will be lost at that point.
First of all...
If you want to make an infinite loop you (always) use:
while(true)
{
...
}
There would be absolutely no reason why defining a variable before that loop should speed up your "application". So there is no reason to use:
bool x = true;
while(x)
{
...
}

When to declarate a variable issue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Can someone explain me the most simple way the meaning of this syntax of C?
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(number[i]<number[j])
{
aux=number[i];
number[i]=number[j];
number[j]=aux;
}
}
}
I just trying to figure out I know is an iteration or a loop but specifically aux is a var. Why i need to follow this i'm trying to get pos and negs, into an array but this part i'm stuck is there another way ?
I just need to figure this syntax.
This looks like Bubble Sort. aux is a temporary variable used for exchanging the values of number[i] and number[j]. You can't do
number[i] = number[j];
number[j] = number[i];
to exchange the two, as both would be equal to number[j] this way. So you need a temporary variable.

Resources