DOC PREVIEW
FIU COP 2210 - Primitive Type boolean

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Computer Programming I Instructor: Greg ShawCOP 2210 Primitive Type booleanThe boolean type is named for George Boole (1815-1864), a Britishmathematician who was a pioneer in the study of logic. In his book,The Mathematical Analysis of Logic, Boole created a system of algebrawhich quantified human logical reasoning. Boole’s algebra has only twovalues – 0 and 1 – and is the basis for the modern digital computer. I. Boolean Literals- There are only two values of type boolean, false and true- These are not strings or variables but literals of type boolean,just as 37 is a literal of type int - If this code is executed:int x = 13 ;System.out.println(x > 0) ;the output will be: trueII. Boolean VariablesBoolean variables store a value of true or false and are declaredjust like a variable of any other type, with or without initialvaluesboolean eligible ;boolean done = false ;III. Boolean Assignment Statements- A boolean assignment statement assigns a value of true or falseto a boolean variable, e.g.eligible = age >= 18 && age <= 35 ;- Note that this has the exact same effect asif (age >= 18 && age <= 35) eligible = true ;elseeligible = false ;but is considerably more elegant!IV. Using boolean Variables as Program “Flags”- Once a value has been stored in a boolean variable, the variablemay be used as a program “flag,” in the sense of a signal flag,which tells us to do something if the flag is up (true) or not doit – or maybe do something else - if the flag is down (false)- Examplesif (! done) // pronounced, “if not done”{statements} if (eligible){statements ;}else{other statements ;}V. Why boolean Variables?1. They make the program more “English-like” 2. They provide more efficient evaluation (only 1 bit needs to bechecked) Compare if (eligible)vs. if (age >= 18 && age <= 35)VI. Don’t be Afraid!Some people don’t trust boolean variables and write expressions likeif (eligible == true) instead of if (eligible) andif (done == false) instead of if (!done)  Although these have exactly the same effect, they are inelegantand defeat the purpose of boolean variables (see V., above)VII. Boolean Methods- A boolean method is a method that returns a value of true orfalse- Boolean methods are also known as “predicate methods” becausethey answer a yes/no question For examples of all things boolean – variables, assignments,operators !, &&, and ||, and a boolean method, seeLeapYearTester.javaLeapYearTester2.java has two more examples of boolean


View Full Document
Download Primitive Type boolean
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Primitive Type boolean and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Primitive Type boolean 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?