how to jump from the if state without executing else.? [closed]
for(开发者_运维技巧....)
if(condition)
printf(_);
else
printf();
what code will come here....
If I understand you correctly, you want to jump out of the if
and the enclosing for
loop when condition
is true. You can achieve this using break
:
for(....) {
if(condition) {
printf(_);
break;
}
else
printf();
}
Note: I added proper indentation and angle brackets to make the code cleaner.
Update after OP's comment
int isPrime = 0;
for (i = 0; i < 10 && !isPrime; i++) {
isPrime = (16 / i == i);
}
if (isPrime)
printf("its a prime no");
else
printf("not a prime no.");
Disclaimer: th condition above is hardly the proper way to detect whether a given number is prime, but still this code snippet illustrates the general idiom of saving the result of an expression from a loop and inspecting it later.
The else
code is never executed if condition
is true:
if (condition) // If this condition evaluates to true
{
printf("Hello"); // Then this code is executed
}
else
{
printf("World"); // If it is false then this code is executed.
}
Edit: Wrapping it in a for loop makes no difference (unless you mean want to actually exit the for loop as well?)
精彩评论