这种情况是什么(s [i]& s [i]!= c)意味着什么?

时间:2017-07-10 09:48:55

标签: c for-loop

#include <stdio.h>

char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";

int main ()  
{
   int i, c;
   while ((c = getchar()) != EOF) {
      for (i = 1; s[i] && s[i] != c; i++);
      if (s[i]) putchar(s[i-1]);
      else putchar(c);
      /* code */
   }
   return 0;
}

for循环中的条件s[i] && s[i] != c;是什么意思? 我以前没见过。 谢谢!

2 个答案:

答案 0 :(得分:5)

首先,变量s是一个空终止的C-string,它是一个最后一个字符为'\0'的字符数组。您可以在代码中使用索引i访问该数组 使用for循环,循环遍历s字符串,直到找到空终止符'\0'或char c。 空终止符'\0'是0十进制,这在布尔逻辑中表示为false,因为除0之外的其他值为真。

如果你写的话:

char a = 'A';   /* same as char a = 65; */
if (a) { ... }; /* same as if (a != 0)  */

这意味着:if a is trueif a is not false更好:if a is not equal to 0。此语句将评估为true,因为'A'为65十进制(ASCII code),不等于0。

您要求的for循环可以改写为:

for (i = 1; s[i] != '\0' && s[i] != c; i++);

我建议使用像s[i] != '\0'这样的显式语句,因为它更容易阅读。

答案 1 :(得分:-3)

s [i] in if将检查s [i]上是否存在元素。如果元素存在于s [i]上那么它的真实性如果不存在那么它就是假的。

c正在使用while循环中的用户输入进行初始化

db.collection.aggregate([
  {
    "$project": {
      "statusandcreateddate": {
        "$concatArrays": [
          [
            {
              "status": "$status",
              "createdDate": "$createdDate"
            }
          ],
          {
            "$map": {
              "input": "$communications",
              "as": "l",
              "in": {
                "status": "$$l.callResponse.Status",
                "createdDate": "$$l.createdDate"
              }
            }
          }
        ]
      }
    }
  },
  {
    "$unwind": "$statusandcreateddate"
  },
  {
    "$group": {
      "_id": {
        "$dateToString": {
          "format": "%d/%m/%Y",
          "date": "$statusandcreateddate.createdDate"
        }
      },
      "total_call": {
        "$sum": {
          "$cond": [
            {
              "$or": [
                {
                  "$eq": [
                    "$statusandcreateddate.status",
                    "failed"
                  ]
                },
                {
                  "$eq": [
                    "$statusandcreateddate.status",
                    "busy"
                  ]
                },
                {
                  "$eq": [
                    "$statusandcreateddate.status",
                    "completed"
                  ]
                },
                {
                  "$eq": [
                    "$statusandcreateddate.status",
                    "no-answer"
                  ]
                }
              ]
            },
            1,
            0
          ]
        }
      },
      "engaged": {
        "$sum": {
          "$cond": [
            {
              "$eq": [
                "$statusandcreateddate.status",
                "completed"
              ]
            },
            1,
            0
          ]
        }
      },
      "not_engaged": {
        "$sum": {
          "$cond": [
            {
              "$or": [
                {
                  "$eq": [
                    "$statusandcreateddate.status",
                    "failed"
                  ]
                },
                {
                  "$eq": [
                    "$statusandcreateddate.status",
                    "busy"
                  ]
                },
                {
                  "$eq": [
                    "$statusandcreateddate.status",
                    "no-answer"
                  ]
                }
              ]
            },
            1,
            0
          ]
        }
      },
      "Pending": {
        "$sum": {
          "$cond": [
            {
              "$eq": [
                "$statusandcreateddate.status",
                "ToDo"
              ]
            },
            1,
            0
          ]
        }
      },
      "InProgress": {
        "$sum": {
          "$cond": [
            {
              "$eq": [
                "$statusandcreateddate.status",
                "InProgress"
              ]
            },
            1,
            0
          ]
        }
      },
      "Done": {
        "$sum": {
          "$cond": [
            {
              "$eq": [
                "$statusandcreateddate.status",
                "Done"
              ]
            },
            1,
            0
          ]
        }
      }
    }
  }
])