if条件语句里数据类型的逻辑判断与分析

if条件语句里数据类型的逻辑判断与分析


在写编写代码前,理顺逻辑顺序是非常重要的第一步。

代码里面逻辑关系是第一步,那么逻辑里面免不了会使用到条件判断语句了。

  • 今天主要讲解我对if语句里数据类型的逻辑判断问题一些见解,不能以偏概全,姿势个人的经验总结。
  • 我就是在这个if条件语句里踩过坑的人。
    *if条件语句执行的条件,说白了就是非真既假的执行方式。执行if下面的语句就看条件满不满足。满足条件就往下执行,不满足就不执行。
  • 看似很简单的逻辑,但是在实际应用的时候,会遇到各种各样的实际问题。
  • 有时候,逻辑关系自己理清了,但是要通过代码写出来实现告诉编译器,按照人的思想和意愿去执行想要的一个运行结果,往往并不容易。这里就是不逻辑问题了,还是逻辑代码转换能力了。
  • 知与行,还隔着代码的书写和表达能力上。
言归正传,步入主题。

1,if条件语句

if...else...
if(条件){
	满足条件时输出的内容
}else{
	内容
}

在if条件里面的做判断逻辑数据类型:整数型和字符串型的逻辑判断。
条件里不管是什么类型的数据,它们的

1.整形数据类型

逻辑运算符
&& 短路与,表达式:条件一 && 条件二 ,说明:仅仅两个条件同时为真,结果为真。
|| 短路或,表达式: 条件一 || 条件二, 说明:只要两个条件有一个为真,结果为真。
非,表达式: 条件, 说明:条件为真时,他为假;条件为假时,他为真。

if(a==1&&b==2){
    //do something
}//条件是a等于1  并且  b等于2时才能成立,两个条件必须同时满足

if ((a <20) && (b > 12)){
    //do something
}//条件是a小于20  并且  b大于12时才能成立,两个条件必须同时满足

if(a==1||b==2){//短路或
    //do something
}//条件是a等于1  或者  b等于2时才能成立,两个条件满足一个就行

if(!(a==1)){
    //do something
}//条件是a等于1 然后取反成立,也就是!=条件成立。
 if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
 等价于
 if ((httpCode == HTTP_CODE_OK) || (httpCode == HTTP_CODE_MOVED_PERMANENTLY))
等价于
if (((httpCode == HTTP_CODE_OK) || (httpCode == HTTP_CODE_MOVED_PERMANENTLY)))

&&、|| 和 ! 的优先级
  • &&、|| 和 !的优先级为:
! > && > ||
  • ! 运算符比许多 C++ 运算符具有更高的优先级。因此,为了避免错误,应始终将其操作数括在括号中,除非打算将其应用于没有其他操作符的变量或简单表达式。例如,来看以下表达式:
 ! (x > 2)
! x > 2

第一个表达式将 !运算符应用于表达式 x>2,它是在问“x 是否不大于 2”,然而,第二个表达式是将 !运算符应用于到 x。它是在问“x 的逻辑是否大于 2”。假设 x 被设置为 5,由于 5 是非零值,所以被认为是 true,而 !运算符会将其反转为 false,即为 0,然后,> 运算符将确定 0 是否大于 2。为了避免这种错误,始终使用括号是明智的。

  • && 和 || 运算符的优先级低于关系运算符,这意味着关系表达式先进行计算,然后再通过 && 和 || 运算符进行评估,因此有:
a > b && x < y 等同于(a > b) && (x < y)
a > b || x < y 等同于(a > b) | | (x < y)

在关系运算符与 && 和 || 混合时,通常不需要括号。当然,无论如何,使用括号都是一个好主意,因为它们可以让程序更易读。

在 && 和 || 同时使用的情况下,更是强烈地推荐使用括号,这是因为 && 的优先级高于 ||。如果没有括号表示执行顺序,那么 && 将永远在 || 之前完成,这可能并不符合编写者的初衷。

假设有 3 个布尔变量 recentGrad、employed 和 goodCredit,则以下表达式:

recentGrad || employed && goodCredit

等同于以下表达式:

recentGrad || (employ && goodCredit)

但是它并不等同于以下表达式:

(recentGrad || employed)&& goodCredit

内容源自:C语音中文网:http://c.biancheng.net/view/1360.html

字符串数据类型条件判断
  • 运算符 == 和 equals()函数是一样的
    *if (stringOne.equals(stringTwo)) { 等价于if (stringOne ==stringTwo) {
  • “>”(大于)和”<“(小于)运算符根据字母表来在最开始的字符分析字符串。所以,例如,”a” < “b” 和 “1” < “2”, 但”999″ > “1000” ,因为9跟在1的后面。
  • 注意:当你比较数字字符串时,字符串比较运算符可以会拒绝,因为数字是被看成字符串而不是数字。如果你需要比较数字,把它们作为ints, floats, 或 longs等数据类型比较,而不是作为字符串。

Arduino示例:

String stringOne, stringTwo;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  stringOne = String("this");
  stringTwo = String("that");
  // send an intro:
  Serial.println("\n\nComparing Strings:");
  Serial.println();

}

void loop() {
  // two strings equal:
  if (stringOne == "this") {
    Serial.println("StringOne == \"this\"");
  }
  // two strings not equal:
  if (stringOne != stringTwo) {
    Serial.println(stringOne + " =! " + stringTwo);
  }

  // two strings not equal (case sensitivity matters):
  stringOne = "This";
  stringTwo = "this";
  if (stringOne != stringTwo) {
    Serial.println(stringOne + " =! " + stringTwo);
  }
  // you can also use equals() to see if two strings are the same:
  if (stringOne.equals(stringTwo)) {
    Serial.println(stringOne + " equals " + stringTwo);
  } else {
    Serial.println(stringOne + " does not equal " + stringTwo);
  }

  // or perhaps you want to ignore case:
  if (stringOne.equalsIgnoreCase(stringTwo)) {
    Serial.println(stringOne + " equals (ignoring case) " + stringTwo);
  } else {
    Serial.println(stringOne + " does not equal (ignoring case) " + stringTwo);
  }

  // a numeric string compared to the number it represents:
  stringOne = "1";
  int numberOne = 1;
  if (stringOne.toInt() == numberOne) {
    Serial.println(stringOne + " = " + numberOne);
  }



  // two numeric strings compared:
  stringOne = "2";
  stringTwo = "1";
  if (stringOne >= stringTwo) {
    Serial.println(stringOne + " >= " + stringTwo);
  }

  // comparison operators can be used to compare strings for alphabetic sorting too:
  stringOne = String("Brown");
  if (stringOne < "Charles") {
    Serial.println(stringOne + " < Charles");
  }

  if (stringOne > "Adams") {
    Serial.println(stringOne + " > Adams");
  }

  if (stringOne <= "Browne") {
    Serial.println(stringOne + " <= Browne");
  }


  if (stringOne >= "Brow") {
    Serial.println(stringOne + " >= Brow");
  }

  // the compareTo() operator also allows you to compare strings
  // it evaluates on the first character that's different.
  // if the first character of the string you're comparing to
  // comes first in alphanumeric order, then compareTo() is greater than 0:
  stringOne = "Cucumber";
  stringTwo = "Cucuracha";
  if (stringOne.compareTo(stringTwo) < 0) {
    Serial.println(stringOne + " comes before " + stringTwo);
  } else {
    Serial.println(stringOne + " comes after " + stringTwo);
  }

  delay(10000);  // because the next part is a loop:

  // compareTo() is handy when you've got strings with numbers in them too:

  while (true) {
    stringOne = "Sensor: ";
    stringTwo = "Sensor: ";

    stringOne += analogRead(A0);
    stringTwo += analogRead(A5);

    if (stringOne.compareTo(stringTwo) < 0) {
      Serial.println(stringOne + " comes before " + stringTwo);
    } else {
      Serial.println(stringOne + " comes after " + stringTwo);

    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值