Which Is the Correct C# Infinite Loop, for (;;) or While (True)

Which is the correct C# infinite loop, for (;;) or while (true)?

while(true)
{

}

Is always what I've used and what I've seen others use for a loop that has to be broken manually.

while(true) / while(1) vs. for(;;)

There is no difference once the program is compiled.

Here are some excerpts from three C programs and the corresponding generated assembly for all of them.

Let's try the for loop first:

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

Now we will try the while loop:

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

A terrible solution, the goto loop:

#include <stdio.h>
int main(){
alpha:
printf("This is a loop\n");
goto alpha;
return 0;
}

Now, if we examine the generated assemblies, using the command gcc -S loop.c, they all look like this (I didn't see any reason to post them separately, since they are identical):

   .file "loop.c"
.section .rodata
.LC0:
.string "This is a loop"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $4, %esp
.L2:
movl $.LC0, (%esp)
call puts
jmp .L2
.size main, .-main
.ident "GCC: (GNU) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)"
.section .note.GNU-stack,"",@progbits

This part is the loop. It declares a label, copies the address to the string into a register, calls a routine called puts, and jumps back to the label:

.L2:
movl $.LC0, (%esp)
call puts
jmp .L2

Since they all do exactly the same thing, clearly there is no technical advantage of any of them (at least if you are using gcc).

However, people have opinions, and may favor one over the others for whatever reason. Since for(;;) is only seven characters long, it is easier to type (this is my preference). On the other hand, while(1) gives the illusion of a test which always evaluates to true, which some may find more intuitive. Only a few crazy people like the goto solution best.

Edit: Apparently some compilers might produce a warning for while(1) because the condition is always true, but such warnings can be easily disabled and have no effect on the generated assembly.

How do I correctly use a while loop in C#?

This still has your infinite loop (NOT recommended), but it will do the job. There's no need to use anything but "true" in while loop bool, because you're going to break out of it with the "break;".

Console.WriteLine("Entry denied");
Console.WriteLine("Please re-enter your username:");
string username3 = Console.ReadLine();
Console.WriteLine("Please re-enter your password");
string password3 = Console.ReadLine();

while (true)
{
Console.WriteLine("Please re-enter your username once more:");
string username4 = Console.ReadLine();
Console.WriteLine("Please re-enter your password once more");
string password4 = Console.ReadLine();

usernamematch = username == username4;
passwordmatch = password == password4;
if (usernamematch && passwordmatch)
{
Console.WriteLine("You, at this point, would be redirected to our webpage but this is c# programming.");
break;
}
}

When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto (in C)?

They are equivalent, even if you turn the optimizer off.

Example:

#include <stdio.h>

extern void f(void) {
while(1) {
putchar(' ');
}
}

extern void g(void) {
for(;;){
putchar(' ');
}
}

extern void h(void) {
z:
putchar(' ');
goto z;
}

Compile with gcc -O0 gives equivalent assembly for all 3 functions:

 f:
; [ EXTERNAL ]
;
+00000 00000fb4 80402DE9 stmdb sp!,{r7,lr}
+00004 00000fb8 00708DE2 add r7,sp,#0x0
+00008 00000fbc 2000A0E3 loc_000008: mov r0,#0x20
+0000c 00000fc0 0A0000EB bl putchar (stub)
+00010 00000fc4 FCFFFFEA b loc_000008
;
;
g:
; [ EXTERNAL ]
;
+00000 00000fc8 80402DE9 stmdb sp!,{r7,lr}
+00004 00000fcc 00708DE2 add r7,sp,#0x0
+00008 00000fd0 2000A0E3 loc_000008: mov r0,#0x20
+0000c 00000fd4 050000EB bl putchar (stub)
+00010 00000fd8 FCFFFFEA b loc_000008
;
;
h:
; [ EXTERNAL ]
;
+00000 00000fdc 80402DE9 stmdb sp!,{r7,lr}
+00004 00000fe0 00708DE2 add r7,sp,#0x0
+00008 00000fe4 2000A0E3 loc_000008: mov r0,#0x20
+0000c 00000fe8 000000EB bl putchar (stub)
+00010 00000fec FCFFFFEA b loc_000008

infinite loop example with minimum code in c#

The typical examples are the for and while loops. For example

for(;;)
{}

and

while(true)
{}

However, basically any looping construct without a break or a terminating condition will loop infinitely. Different developers have different opinions on which style is best. Additionally, context may sway which method you choose.

Is while (true) with break bad programming practice?

There is a discrepancy between the two examples. The first will execute the "do something" at least once every time even if the statement is never true. The second will only "do something" when the statement evaluates to true.

I think what you are looking for is a do-while loop. I 100% agree that while (true) is not a good idea because it makes it hard to maintain this code and the way you are escaping the loop is very goto esque which is considered bad practice.

Try:

do {
//do something
} while (!something);

Check your individual language documentation for the exact syntax. But look at this code, it basically does what is in the do, then checks the while portion to see if it should do it again.

Clarification on while (true) loop with embedded If-Else

public void ValidateInteger()
{
while(true)
{
if (int.TryParse(Console.ReadLine(), out int value)
return Console.WriteLine($"{value} is an integer") <-- right here
else
Console.WriteLine($"{value} is not an integer, try again")
}
}

The return line is where the while loop breaks because there's no reason to continue the loop if a value has been returned.

while(true) versus for(;;)

With optimizations enabled, they will compile identically.

You should use whichever one you find more readable.



Related Topics



Leave a reply



Submit