English 中文(简体)
如何使用? C. 食宿问题标志?
原标题:How to use ? question mark in for loop in C?

如果i 是,for(int j = 0;j < m; j++>

Else if iis odd, for(int j = m-1; j > 0; j--)

我想将以下两个条件结合起来。

for( (i%2==0) ? (int j = 0; j < m; j++) : (int j = m-1; j > 0; j--))
            

But it seems to fail.

我想问,是否用<条码>将两个条件合并起来了?,还是用其他方式使用C语文。

衷心感谢大家,

最佳回答

你们可以做些什么,像以下那样做些什么,但我不建议这样做。 该数值从<代码>m-1降至i> 奇数,在<代码>i0至m-1时算出。

#include <stdio.h>

int main()
{
    int i = 1;
    int m = 8;
    
    int start = i%2 ? m-1 : 0;
    int end = i%2 ? -1 : m;
    int delta = i%2 ? -1 : 1;
    
    for (int j = start; j != end; j += delta)
    {
        printf("j=%d
", j);
    }

    return 0;
}
问题回答

No, you can t combine them with the conditional operator. The operands to a conditional expression are each expressions that return a value, and the result of the conditional expression is a single value.

休息室的表格是for( init expression; conditions expression; iteration expression) loop-one,它使用 3<>。

您再次试图从房主那里收回某些三个要素:<代码>(init, conditions, iteration)tuple of expressions,以作为娱乐场所的内装/condition/iteration表述,但你可以将这些事情包起来,然后将这些事情包装起来。 yn子根本是错误的。

使用<代码>if/>。 如果你想要分享 lo体,使其发挥功能,从每一 lo中起功能:

if (i % 2 == 0) {
  for (int j = 0; j < m; j++) {
    do_things(j);
  }
} else {
  for (int j = m-1; j > 0; j--) {
    do_things(j);
  }
}

Don counting counting • 必要时翻译假体:

void doit( int max, int i ) {
    // peculiar imbalance in OP s ranges :
    // if i is odd, bail out BEFORE reaching 0th value
    max -= i%2;
    for( int j = 0; j < max; j++ ) {
        int k = i%2 ? max - j : j;  // ternary, and begin using  k 

        printf( "%d ", k );
    }
    putchar(  
  );
}

int main( void ) {
    doit( 15, 0 );
    doit( 15, 1 );

    return 0;
}

结果:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
14 13 12 11 10 9 8 7 6 5 4 3 2 1

在C,你不能宣布永久经营人有条件表达的变数(吗? : )。 可变申报必须在私人经营者之外进行。 然而,你可以通过宣布休息室外的变数,然后利用固定经营人确定休息室。 下面是你如何能够做到这一点的一个例子:

int j;
if (i % 2 == 0) {
    for (j = 0; j < m; j++) {
        // Loop body for even i
    }
} else {
    for (j = m - 1; j > 0; j--) {
        // Loop body for odd i
    }
}

在这项法典中,我们宣布了休息室外的变数,并使用“假日”声明,根据一票的价值确定休息区。

这种做法在C中是常见的做法,因为它使你能够宣布地道控制变数,并在地圈内加以使用,而地圈经营者不可能这样做。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...