[{"@context":"http:\/\/schema.org\/","@type":"BlogPosting","@id":"https:\/\/wiki.edu.vn\/en\/wiki14\/off-by-one-error-wikipedia\/#BlogPosting","mainEntityOfPage":"https:\/\/wiki.edu.vn\/en\/wiki14\/off-by-one-error-wikipedia\/","headline":"Off-by-one error – Wikipedia","name":"Off-by-one error – Wikipedia","description":"Logical error that can often be found in programming An off-by-one error or off-by-one bug (known by acronyms OBOE, OBO,","datePublished":"2015-05-03","dateModified":"2015-05-03","author":{"@type":"Person","@id":"https:\/\/wiki.edu.vn\/en\/wiki14\/author\/lordneo\/#Person","name":"lordneo","url":"https:\/\/wiki.edu.vn\/en\/wiki14\/author\/lordneo\/","image":{"@type":"ImageObject","@id":"https:\/\/secure.gravatar.com\/avatar\/44a4cee54c4c053e967fe3e7d054edd4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/44a4cee54c4c053e967fe3e7d054edd4?s=96&d=mm&r=g","height":96,"width":96}},"publisher":{"@type":"Organization","name":"Enzyklop\u00e4die","logo":{"@type":"ImageObject","@id":"https:\/\/wiki.edu.vn\/wiki4\/wp-content\/uploads\/2023\/08\/download.jpg","url":"https:\/\/wiki.edu.vn\/wiki4\/wp-content\/uploads\/2023\/08\/download.jpg","width":600,"height":60}},"image":{"@type":"ImageObject","@id":"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/8\/82\/Fencepost_error.svg\/250px-Fencepost_error.svg.png","url":"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/8\/82\/Fencepost_error.svg\/250px-Fencepost_error.svg.png","height":"59","width":"250"},"url":"https:\/\/wiki.edu.vn\/en\/wiki14\/off-by-one-error-wikipedia\/","wordCount":2381,"articleBody":"Logical error that can often be found in programmingAn off-by-one error or off-by-one bug (known by acronyms OBOE, OBO, OB1 and OBOB) is a logic error involving the discrete equivalent of a boundary condition. It often occurs in computer programming when an iterative loop iterates one time too many or too few. This problem could arise when a programmer makes mistakes such as using “is less than or equal to” where “is less than” should have been used in a comparison, or fails to take into account that a sequence starts at zero rather than one (as with array indices in many languages). This can also occur in a mathematical context.Table of ContentsLooping over arrays[edit]Fencepost error[edit]Security implications[edit]See also[edit]References[edit]Citations[edit]Sources[edit]Further reading[edit]Looping over arrays[edit]Consider an array of items, and items m through n (inclusive) are to be processed. How many items are there? An intuitive answer may be n\u00a0\u2212\u00a0m, but that is off by one, exhibiting a fencepost error; the correct answer is(n\u00a0\u2013\u00a0m)\u00a0+\u00a01. For this reason, ranges in computing are often represented by half-open intervals; the range from m to n (inclusive) is represented by the range from m (inclusive) to n\u00a0+\u00a01 (exclusive) to avoid fencepost errors. For example, a loop that iterates five times (from 0 to 4 inclusive) can be written as a half-open interval from 0 to\u00a05:for (index = 0; index "},{"@context":"http:\/\/schema.org\/","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https:\/\/wiki.edu.vn\/en\/wiki14\/#breadcrumbitem","name":"Enzyklop\u00e4die"}},{"@type":"ListItem","position":2,"item":{"@id":"https:\/\/wiki.edu.vn\/en\/wiki14\/off-by-one-error-wikipedia\/#breadcrumbitem","name":"Off-by-one error – Wikipedia"}}]}]