Why only condition is working for "if" condition?
-
Hi,
I am trying to set the range of integers using Validator and showing the notification message if integer is not within the range. But, I can see that only one condition is working in "if" condition for "data2", not for "data3". I have checked for multiple datas but it is working for only one condtion. Can I know why?
int data2 = ui->lineEdit9_11->text().toInt(); int data3 = ui->lineEdit10_11->text().toInt(); qDebug()<<data2; qDebug()<<data3; if(((data2<50||data2>100)&&(data3<0||data3>40) )) { QMessageBox msg; msg.setText("Range should be 50 to 100\n" "Range should be 0 to 40\n"); msg.exec(); return; }
-
change your && to an ||,
otherwise you only enter the if branch when both, data2 and data3 are out of rangecurrently:
data2 = true && data3 == false -> does not enter
data2 = false && data3 == true -> does not enter
data2 = false && data3 == false -> does not enter
data2 = true && data3 == true -> does enter -
@Mohit-Tripathi said in Why only condition is working for "if" condition?:
Can I know why?
This is simple Boolean algebra:
False && False ==> False False && True ==> False True && False ==> False True && True ==> True False || False ==> False False || True ==> True True || False ==> True True || True ==> True