Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluation of && boolean operator

If i have the following if statement

if ( (row != -1) && (array[row][col] != 10) ) {
    ....
}

Where row is an int value and array is an int[][] object.

My question is, if this will throw an exception if row = -1 as the array won't have a -1 field, so out of bounds exception? Or will it stop at the first part of the if, the (row!=-1) and because that is false, it will ignore the rest? Or to be sure it doesn't throw exception, i should separate the above if statement into two?

(Pls, don't tell me to check this out for my self :) I'm asking here 'cause i wanna ask a followup question as well ...)

like image 429
AndreiBogdan Avatar asked Mar 13 '12 05:03

AndreiBogdan


People also ask

Is it evaluation on or evaluation of?

A complete search of the internet has found these results: Evaluation of is the most popular phrase on the web.

What do you mean evaluation?

Evaluation is a process that critically examines a program. It involves collecting and analyzing information about a program's activities, characteristics, and outcomes. Its purpose is to make judgments about a program, to improve its effectiveness, and/or to inform programming decisions (Patton, 1987).

What are the 3 types of evaluation?

The main types of evaluation are process, impact, outcome and summative evaluation. Before you are able to measure the effectiveness of your project, you need to determine if the project is being run as intended and if it is reaching the intended audience.

What is evaluation and its example?

To evaluate is defined as to judge the value or worth of someone or something. An example of evaluate is when a teacher reviews a paper in order to give it a grade. verb. 29.


1 Answers

It will stop safely before throwing an exception

The && is a short-circuiting boolean operator, which means that it will stop execution of the expression as soon as one part returns false (since this means that the entire expression must be false).

Note that it also guaranteed to evaluate the parts of the expression in order, so it is safe to use in situations such as these.

like image 190
mikera Avatar answered Sep 29 '22 00:09

mikera