can we use for loop without condition? [duplicate] - c

This question already has answers here:
What is the full "for" loop syntax in C?
(6 answers)
Closed 5 years ago.
My question is simple,thus I will not go in deep
can we use for() loop without condition like this
for(;;space+=1)
{
printf(" ");
break;
}

Of course you can. An empty condition is taken to evaluate to 1.
for (;;){/*ToDo - your code here*/} is idiomatic C.

Yes it is perfectly correct to do so.
But since you have provided a break immediately after printf, it will only execute once. I'm not sure whether this is what you wanted. But if so, then this works fine.

Related

Should I put semicolons after JSX expression? [duplicate]

This question already has answers here:
What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(7 answers)
Closed 7 months ago.
If I have the following:
...
let p1 = <p>qwer</p>
let p2 = <p>asdf</p>
...
Should I put semicolons at the end of each of these?
It is not wrong to leave it like that, you can put semicolons if you want, but it is better you be consistent and follow one code style check https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
it will make your life easier

What is the difference between recursion and loops and which one is faster [duplicate]

This question already has answers here:
Recursion or iteration?
(14 answers)
Closed 2 years ago.
What is the difference between loops and recursion and which one is more preferred for solving a problem?
Loop is repeated execution of a block of code until the given condition is false.Loop has a linear flow from initial condition to terminating condition.
On the other hand recursion is a method/function being called by itself. Recursion takes place creating stack of task ie, each calling function returns to its caller function after execution.
Loop and recursion aren't exactly alternatives to have preferences, they are used for different type of tasks.

code won't compile generates infinite 2222s [duplicate]

This question already has answers here:
C code won't finish running
(4 answers)
Closed 9 years ago.
The purpose of this code is to construct a char Hadamard matrix of the size of my choosing.
This question is related to a previous question I asked. The answer given there was an integer not char matrix, but the code here is pretty much the same format.
The code compiles but when executed it doesn't finish and I don't know why. When executed infinite 2's are printed.
I get the same result when swap the dynamic Hadamard matrix section for one of a fixed size.
Note: I've no idea what your program does, but obviously this is wrong. You failed to actually change the control variable in your for-loop (which can be done in the final expression or the loop body itself).
Change this:
for (ind=1;ind<=sizeH;ind*2)
to this:
for (ind=1;ind<=sizeH;ind*=2) // << note *=

why c language needs this for(; ;) loop? [duplicate]

This question already has answers here:
Why choosing for (;;){} over while(1)? [duplicate]
(4 answers)
Closed 9 years ago.
why c language needs this for(; ;) loop ? It is actually an infinite loop and while(1) also infinite loop. My question is that which one we should use ? I want to know which one is more efficient according to compiler point of view?
Both are same. I believe infinite loops are needed when you want to break on a certain condition which the executing code knows only once it enters the loop.
For me this looks more intuitive and easier:
while(1) {}
while(true) {}
Remember even this is an infinite loop :
// just don't provide the condition
for(int i=0; ;i++) // don't mind the syntax as I don't belong to C
Whichever fits your needs. Both do the same thing.
For an infinite loop, both do the same thing, but for and while are used for different things, and are not generally interchangeable.

Can compilers detect infinite looping condition? [duplicate]

This question already has answers here:
When have you come upon the halting problem in the field? [closed]
(13 answers)
Closed 9 years ago.
I'm building a compiler for a custom language. Is it possible for the compiler to detect any infinite looping condition without running the program?
If so how can I implement it?
You may be able to detect some infinite loops, but in general you can't detect all possible infinite loops (unless your custom language is specifically designed to eliminate general looping constructs). See http://en.wikipedia.org/wiki/Halting_problem.

Resources