Browse By Unit
2 min read•july 11, 2024
Milo Chang
Milo Chang
When you use boolean operators like && or || to compare statements, the computer takes shortcuts to be more efficient. These short-cuts result in "short-circuiting", where the computer doesn't evaluate all of the statements.
Since a statement involving && is only true if both of the statements are true, the computer doesn't evaluate the second part of the conditional if the first part is false. This helps it save time by avoiding unnecessary calculations.
int x = 10;
int y = 8;
if (x<7 && y==8)
{
System.out.println("Works");
}
else
{
System.out.println("Failed");
}
Since x is not less than 7, the computer didn't even check if y was equal to 8, as the statement would evaluate to false either way because the first part was false.
This becomes important in situations like the one below:
int a = 8;
int b = 0;
if (a>10 && a/b==3)
{
System.out.println("Works");
}
else
{
System.out.println("Failed");
}
A multiple-choice question on the AP Computer Science A Exam may ask you what the outcome of executing this code would be. One of the choices will likely be that there will be a "divide by 0" error. However, this is the incorrect option.
Why is that the incorrect option? Due to short-circuiting, the computer running this code won't even try to run a/b because once it finds that a is not greater than 10, it will immediately deem the entire statement false and move to the else statement. Thus, the output of this code would be "Failed".
Since a statement involving || is true if even one of the statements is true, the computer won't evaluate the second part if the first part is true. Once again, this helps save time by avoiding unnecessary calculations.
This is important in situations like the one below:
int x = 8;
int y = 10;
if (x<10 || x/0==8)
{
System.out.println("True");
}
else
{
System.out.println("False");
}
A multiple-choice question on the AP Computer Science A Exam may ask you what the outcome of executing this code would be. One of the choices will likely be that there will be a "divide by 0" error. However, this is the incorrect option.
Why is that the incorrect option? Due to short-circuiting, the computer running this code won't even try to run x/0==8 because once it finds that x is less than y, it will immediately deem the entire statement true and run the code inside the if statement. Thus, the output of this code would be "True".
<< Hide Menu
2 min read•july 11, 2024
Milo Chang
Milo Chang
When you use boolean operators like && or || to compare statements, the computer takes shortcuts to be more efficient. These short-cuts result in "short-circuiting", where the computer doesn't evaluate all of the statements.
Since a statement involving && is only true if both of the statements are true, the computer doesn't evaluate the second part of the conditional if the first part is false. This helps it save time by avoiding unnecessary calculations.
int x = 10;
int y = 8;
if (x<7 && y==8)
{
System.out.println("Works");
}
else
{
System.out.println("Failed");
}
Since x is not less than 7, the computer didn't even check if y was equal to 8, as the statement would evaluate to false either way because the first part was false.
This becomes important in situations like the one below:
int a = 8;
int b = 0;
if (a>10 && a/b==3)
{
System.out.println("Works");
}
else
{
System.out.println("Failed");
}
A multiple-choice question on the AP Computer Science A Exam may ask you what the outcome of executing this code would be. One of the choices will likely be that there will be a "divide by 0" error. However, this is the incorrect option.
Why is that the incorrect option? Due to short-circuiting, the computer running this code won't even try to run a/b because once it finds that a is not greater than 10, it will immediately deem the entire statement false and move to the else statement. Thus, the output of this code would be "Failed".
Since a statement involving || is true if even one of the statements is true, the computer won't evaluate the second part if the first part is true. Once again, this helps save time by avoiding unnecessary calculations.
This is important in situations like the one below:
int x = 8;
int y = 10;
if (x<10 || x/0==8)
{
System.out.println("True");
}
else
{
System.out.println("False");
}
A multiple-choice question on the AP Computer Science A Exam may ask you what the outcome of executing this code would be. One of the choices will likely be that there will be a "divide by 0" error. However, this is the incorrect option.
Why is that the incorrect option? Due to short-circuiting, the computer running this code won't even try to run x/0==8 because once it finds that x is less than y, it will immediately deem the entire statement true and run the code inside the if statement. Thus, the output of this code would be "True".
© 2024 Fiveable Inc. All rights reserved.