Yesterday I have attended an interview, there I saw a weird program snippets.
By initial glance I decide that Snippet has compilation error in it. But when came home and tried manually in C compiler I found I was totally wrong
See interview code
#include<stdio.h>
void main()
{
for(int i=0;i=5;i=5)// here condition and increment are assigned
//wrongly and where I thought it is compilation
//error during interview which isn't wrong
{
printf("helloworld \n");
}
}
Output:
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
.
.
.
.(goes on and on)
output in C++ is similar to C
But,
when we run this code in java compiler
public class Forhottest {
public static void main(String args[])
{
for(int i=0;i=5;i=5)// here it throws compilation error
{
System.out.println("helloworld");
}
}
}
similarly I tried in PHP ,same problem arise as in java. Why C and C++ allow this kind of weird condition statement inside "for loop". what is reason behind it