开发者

how to jump from the if state without executing else.? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
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?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜