I want to return 0.Here, the return value is not overwriting for i=5... Is it possible to return 0?
#include<stdio.h>
int main(){
int i;
for(i=2;i<=5;i ){
if(5%i==0){
return 0;
}else{
return 1;
}
}
}
CodePudding user response:
No it is not possible to return 0.
Since the function will return regardless on the first iteration the for loop can be ignored. If we rewrite that code it becomes this:
int i = 2;
if (i <= 5) {
if (5 % i == 0) {
return 0;
} else {
return 1;
}
}
CodePudding user response:
when first loop excuted, i == 2, 5 % i == 1 means i % 5 != 0, so obviously it will return 1, or to say this program has no chance to return 0. And the "return" means the function contains this "return" will terminate executing when return.
Your requirement is too vague, what do you mean by "I want to return 0", can you explain by literal description?
If you mean you want this loop return only when 5 % i == 0, you should write
#include <stdio.h>
int main(){
int i;
for (i = 2; i < =5; i ) {
if(5 % i == 0){
continue;
} else {
return 1;
}
}
}