'Boolean' data type.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

JavaScript Lecture 6 Rachel A Ober
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Variables. A variable is a space in your robot’s memory where you can store data, such as whole numbers, decimal numbers, and words. Variable names follow.
JavaScript: Conditionals contd.
JavaScript Controlling the flow of your programs with ‘if’ statements
Bill Tucker Austin Community College COSC 1315
Choosing Data Types Database Administration Fundamentals
Precedence Operators Error Types
Chapter # 2 Part 2 Programs And data
The Ohio State University
Regular Expressions 'RegEx'.
Variables Variables are used to store data or information.
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Introduction to Python
Introduction to Python
Selection (also known as Branching) Jumail Bin Taliba by
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
Documentation Need to have documentation in all programs
ECS10 10/10
EGR 2261 Unit 4 Control Structures I: Selection
Variables and Primative Types
Introduction to C++ October 2, 2017.
Variables, Printing and if-statements
Programming Fundamentals
Programming – Touch Sensors
Control Statement Examples
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Logical Operators, Boolean Data Types
CSE341: Programming Languages Section 1
Conditions and Ifs BIS1523 – Lecture 8.
Introduction to C++ Programming
Today in CS161 Week #3 Learn about… Writing our First Program
CSE341: Programming Languages Section 1
Variables ICS2O.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Introduction to Primitive Data types
C++ Data Types Data Type
Coding Concepts (Basics)
JavaScript What is JavaScript? What can JavaScript do?
Conditions and Boolean Expressions
Module 4 Loops.
Logical Operations In Matlab.
Variables Kevin Harville.
Programming with Alice
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
JavaScript What is JavaScript? What can JavaScript do?
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Tutorial 10: Programming with javascript
Introducing JavaScript
Variables and Computer Memory
Chap 7. Advanced Control Statements in Java
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Variables and Constants
Introduction to Primitive Data types
Working with Strings.
Working with Numbers parseInt() and parseFloat() Math.round()
Retrieving information from forms
Presentation transcript:

'Boolean' data type

Learning Objectives By the end of this lecture, you should be able to: Understand a new data type: Boolean Appreciate the importance and benefit of Boolean values Know how to use Boolean values in your code

New Data Type: 'Boolean' Up to this point, we have learned a few key data types: string, int, and float. We are going to add an additional – and very important – data type to our repertoire: 'Boolean'. This data type holds only one of two possible values: true or false. Pretty much every programming language makes extensive use of this data type. However, they may refer to the values differently. Some languages choose '0' for false, and non-zero for everything else. Others refer to them as True or False (i.e. capitalized first letter). Suppose we wanted to keep track of whether someone was eligible to vote. We will create a variable: var eligibleToVote; Now we could store the value in a variety of different ways: We could store it as a string: eligibleToVote = "yes"; We could store it as an integer: eligibleToVote=1; However, we'd have to know and keep track of how I chose to represent these values. For example, suppose another programmer who stored the variable as a string, chose to say: eligibleToVote="YES" or perhaps eligibleToVote="Y", or how about: eligibleToVote="Yes" Similarly with numbers: Does 1 mean they are eligible? If so, what represents that they are not eligible? A 2? A 0? A negative number? Because true and false are so important in programming, every language has some way of representing this value. In many languages including JavaScript, the datatype is referred to as 'Boolean'. Some languages call it 'bool'. There are other variations out there as well.

Using a Boolean JavaScript Booleans store their values as either true or false. So now if we wanted to keep track of a user's eligibility to vote, we might code: var eligibleToVote = true; NOTE: Do not put quotes around the true. If you do, then you will be storing a string that holds the characters t-r-u-e. You will not be storing a Boolean value, you will be storing a string. Booleans are extremely common and useful in programming. For example, in form validation, you might create a variable called 'isFormValid'. If, at any point, in your validation, something is wrong, you can set this Boolean to false. Then at the end of the validation, if the Boolean is false, your code can refuse to proceed with the submission of the form. var isFormValid = true; if ( user forgot to enter email address ) isFormValid = false; else if ( user entered an invalid character ) else if ( etc etc ) Then, somewhere towards the end of your code: if (isFormValid == true) //Process the form else alert('Something is wrong with your form');

Booleans inside conditionals One of the most common places you will encounter Booleans is inside conditional expressions. Recall that a conditional must always evaluate to true or false. if ( conditional ) ... Therefore we can have a variable as discussed previously that keeps track of whether a user is eligible to vote: var eligibleToVote = true; At some point in the program, we might have code similar to the following: if ( eligibleToVote == true ) alert('Congrats, you can vote!'); else if ( eligibleToVote == false ) alert('Sorry, you can not vote.');

Leaving out ==true or ==false in a conditional Again, recall that a conditional must ultimately evaluate to true or false if ( conditional ) ... Therefore if we have a variable such as a Boolean inside the conditional, we can leave out the '==true' or '==false': var eligibleToVote = true; if ( eligibleToVote ) //this will work just fine! alert('Congrats, you can vote!'); else alert('Sorry, you can not vote.'); For now, however, I would recommend you include '==true' or ' ==false' inside your conditionals until you get very comfortable with the data type.

Example boolean_example.htm museum_admission_validation_boolean.htm