Home > Back-end >  Novice inquired, multiple don't increase the brackets if statements in C language of continuous
Novice inquired, multiple don't increase the brackets if statements in C language of continuous

Time:11-22

Two problems:

(1)
Format is as follows:
If (... )
If (... )
If (... )
{... }
Statement

Ask this statement is in the if statement? You need to perform? If you need to perform, then belong to which one if?

(2) example:

#include
Void main () {
Int I=1, j=1, k=5;
Do {
If (I % j==0)
If (I % k==0) {
printf("%d\n",i);
break; }
i++;
} while (I!=0);
}

Ask the i++; It is to belong to the if statement? If not, the first if statement to which had been stopped? What's the discriminant method?

CodePudding user response:

Does not belong to, the language equivalent structure is like this:
 # include 
Void main () {
Int I=1, j=1, k=5;
Do {
If (I % j==0)
{
If (I % k==0) {
Printf (" % d \ n ", I);
break;
}
}
i++;
} while (I!=0);
}

CodePudding user response:

C language statement is a semicolon; Or curly braces {} over, so the statement in question 1 do not belong to the if statement, I eventually results in question 2 is 15

CodePudding user response:

This i++ not in the if statement and that the inside of the braces are two of the if statement scope, look curly braces, in fact, the first if statement also had better add braces, or rewrite the two if statements for a judgment

CodePudding user response:

Can be seen from the result of question 2 to all if statements meet the conditions to perform output I, so I always sum to 15 can be divided exactly by 5 j and k,

CodePudding user response:

1: this statement is to belong to the if statement, if you want to, must be the front three fully meet the if condition will perform to the,
The second question: i++; , it does not belong to the if statement, c is the ";" To say that the end of a statement, so the front between the two if not the end of the symbol, they said it is the same with statements,

CodePudding user response:

1 means the if condition was established, execute an if statement, so finally the curly braces belong to the third (layer 3) perform part of the if statement
2 i++ not if the execution of the statement, the above situation is similar to and 1 is the if is true, then perform an if statement, and then if the second if is true, then executing the next statement (or the next big all statements in parentheses), so this twofold if statements to walk to the end of the brace is finished, the judgment method is just a statement after the if (XXX), don't need braces if
If (xx)
S1.//if here're finished
S2.

CodePudding user response:

To slightly modified the second application, to experiment, the building Lord knew:
 int main (int arg c, char * argv []) 
{
Int I=1, j=1, k=5, m=4.
Do {
If (I % j==0)
If (I % k==0)
If (I % m==0)
{
printf("%d\n",i);
break;
}
i++;
} while (I!=0);
system("pause");
return 0;
}

CodePudding user response:

 int main (int arg c, char * argv []) 
{
Int I=1, j=1, k=5, m=4.
Do {
If (I % j==0)
If (I % k==0)
If (I % m==0)//these three if statements, equivalent to: if (I % j==0 & amp; & I % k==0 & amp; & I % m==0)
{
printf("%d\n",i);
break;
}
i++;
} while (I!=0);
system("pause");
return 0;
}
  • Related