0
What happens if a for loop ends with semicolon?? Does it gives any error??
4 Respostas
+ 4
The error is usually due to the accessing of the variable after the for loop like this:
for(int i=0;i<10;i++); // i local to loop only
cout<<i; // i remains undeclared in this scope...
+ 3
@aklex
But it does seem that a single for loop slightly increase compilation time...
Eg - In the CodeBlocks Console Runner Window, an empty main and a main with a single incrementing for loop operation (no printing, just incrementing i), returns a time difference <= 0.002s...
I calculated this from the Time returned by CodeBlocks after any program is finished executing...
Maybe the runtime isn't affected, but the compilation time must be affected...
+ 2
Nothing happens, it just loops and increments until the condition is met
+ 2
@Kinshuk Vasisht "All that does is slow the net program execution by 0.002 seconds in most modern compilers."
???? It does not add any execution time for compilers. It produces the exact same code.
GCC:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], 0
.L3:
cmp DWORD PTR [rbp-4], 2
jg .L4
add DWORD PTR [rbp-4], 1
jmp .L3
.L4:
nop
pop rbp
ret
GCC without ; :
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], 0
.L7:
cmp DWORD PTR [rbp-4], 2
jg .L8
add DWORD PTR [rbp-4], 1
jmp .L7
.L8:
nop
pop rbp
ret
Clang:
push rbp
mov rbp, rsp
mov dword ptr [rbp - 4], 0
.LBB0_1: # =>This Inner Loop Header: Depth=1
cmp dword ptr [rbp - 4], 3
jge .LBB0_4
jmp .LBB0_3
.LBB0_3: # in Loop: Header=BB0_1 Depth=1
mov eax, dword ptr [rbp - 4]
add eax, 1
mov dword ptr [rbp - 4], eax
jmp .LBB0_1
.LBB0_4:
pop rbp
ret
Clang without ; :
push rbp
mov rbp, rsp
mov dword ptr [rbp - 4], 0
.LBB1_1: # =>This Inner Loop Header: Depth=1
cmp dword ptr [rbp - 4], 3
jge .LBB1_4
jmp .LBB1_3
.LBB1_3: # in Loop: Header=BB1_1 Depth=1
mov eax, dword ptr [rbp - 4]
add eax, 1
mov dword ptr [rbp - 4], eax
jmp .LBB1_1
.LBB1_4:
pop rbp
ret