Condition in a for loop

I am experimenting with what can be placed in a loop cycle declaration in C and how it can be used. I tried the following:

#include <stdio.h>

int stupid(int a)
{
        if(a==3) return 1;
        else return 3;
}

int main(void)
{
        int i,j;
        for(i=0;stupid(i)==3,i<10;i++) printf("%d\n",i);
        return 0;
}

When I run the program, it just prints a number from 1 to 10, and if I put && instead of a coma between dumb (i) == 3 and I <10, then the program just prints the numbers to 3. Why? I really don’t understand how this works, and I expected the cycle to go through all the numbers and “skip” 3, but continue to 10, and it will not really be hapenning. Can someone explain to me why and point me to some site where this is more clearly explained? Thank you in advance.

+5
source share
9 answers

for ( stupid(i)==3,i<10) - , . true, . false, , .

(stupid(i)==3,i<10) stupid(i)==3, i<10 . , 0 9.

stupid(i)==3 && i<10 true , , , i=3, stupid(i)==3 , .

+8

, , . , for < 10, , for. 1 10, .

&& , , && , . , == 3, false .

+3

, . stupid() - , , , , i<10.

&&, true (non-zero) . , false, for , .

+2

for , . if for, , (i) , for .

0

, . , , , . , stupid() , i < 10, .

0

for , .

- , . .

- , , . , . false, .

- , , . , .

stupid(i)==3,i<10 . , . stupid(i)==3 . stupid(i)==3 && i<10 , .

, , - , . , ,

 for(i=0; i < 10; ++i) {
    if (stupid(i)==3) {
       printf("%d\n",i);
    }
 }

0-9, , stupid(i) 3.

0
int main(void)
{
        int i,j;
        for(i=0; i<10; i++, i+=i==3) printf("%d\n",i);
        return 0;
}

, , . , for() if()

i+=i==3 1 i, i 3, i==3 1, ant 0 ( 0 ).

0

for loop i-4, (i) == 3, 3, . . Dev-++

#include <stdio.h>
int i,j;
int stupid(int a)
{
    if(a==3) 
    i=4;
    else return 3;
}

int main(void)
{
    for(i=0;stupid(i)==3,i<10;i++) printf("%d\n",i);
    return 0;
}
-1

The comma in your for loop acts like an OR statement. If any statement evaluates to true, then the loop stops execution.

-2
source

All Articles