declare

76. Flags - Learn Java



Sharing buttons:

okay hello everyone all right so let's

talk about flags okay so a flag is a

boolean variable that signals to our

program when a certain condition has

been reached on when a certain condition

exists in our program

all right so assuming we wanted to

create a program that allows the user to

let's say enter a score let's go from a

test a test that was taking for example

and so we're going to display to the

user that okay you passed it if the user

score is greater than or equal to let's

say a past mark that we set right so we

can set a pass mark let's say the value

80 and so if the users value or if the

user score is greater than or equal to

80 which is the pass mark then let's

display a message to the user and say

that okay congratulations you've passed

all right so we need a way to kind of

signal to a program when the user has

passed right so I kind of said that

faster that's that's funny all right so

let's go ahead and create a boolean

variable let's kind of demonstrate this

so that you can see how flags work right

so it's basically a boolean variable I'm

going to go ahead and create a boolean

variable and I'm going to call it passed

and we know boolean variables can either

store the values true or false I'm going

to set it initially to false right and

the only time we're going to set this

past boolean variable to true is when

the users value of when the user score

is greater than or equal to the pass

mark now I mention the pass mark if I'm

if you're talking about past months we

can create you can store that past mark

in a variable right but then a good way

or a very neat way to store your values

like that past marks or values like that

values that you don't want to change

when your program is running is by using

final finals right or constant so I'm

going to go ahead and declare a final

double right it's going to be if it's a

double because if the past market can be

something like ninety five point five or

sixty six point eight something like

that so I'm going to create a final

double and then I'm going to set its

name to

passmark now we know by convention when

you're naming your your final values or

your final yeah your final values you by

convention we're naming them you

separates them with underscores if it's

more than one word and then you

capitalize all of it okay so you

capitalize pause and mark and you

separate them and underscores if it's

more than one word let's set the pass

mark to let's say 80 all right so that's

going to be a pass mark it this value

won't change when the program is running

and then we will need the user score now

ideally ideally we will have to use

something like the scanner or the G

option pane to accept the user's input

but then for this program just because I

want to demonstrate how flags work

I just let's go ahead and just store

this value in a variable so I'm going to

create a double variable I'm going to

call it user score and then let's set it

initially to 85 right okay so we can

create an if statement and then we can

set this past boolean variable to true

only if the user score is greater than

or equal to 80 if it's 80 or above then

let's set it or let's change the value

of this pass above boolean variable to

true true let's set it to true so if the

user score is greater than or equal to

85 sorry not 85 the pass mark right the

pass mark if it's greater than or equal

to the pass mark 85 is a user score so

if the user score is greater than or

equal to the pass mark right then we

know the user has passed so let's change

past the blue of the boolean where I

will pass to true right that's the only

time we we are going to change path to

true if not pass will always stay false

now you may ask yourself why why that's

what's the purpose of doing this once

you've set the value of this boolean

variable to true then work them were

next now later on in your program you

can use an if statement to test on the

value of the boolean variable passed we

can test on it

to see what's what what what value

stored in there so we can write it this

way we can write it this way

sorry my typing is coming off here if

past now you now you may think how does

this work now we know for an if

statement to run run the code order in

the lines of code in the curly braces

what's in the parentheses over here

what's in the boolean expression the

boolean expression over here has two

results to true right if it results true

then the lines of code or line of code

in this in this Cal Callie braces are

going to run it so are going to run if

it's false then we know that nothing in

the in the calibrations you get executed

or get run so it's going to skip this is

going to skip the statements in the if

statements and then continue down right

so the only time that the code or the

lines of code in the curly braces are

going to run or is going to run is if

the boolean expression over here if

what's in the parentheses over here

results to true if it's true now we know

this is a boolean variable contains the

value true or false it will be true only

if the user score is greater than the

pass mark and it will be false if the

user score is not greater than the pass

mark right so if they you just call is

greater than the park written or equal

to the pass mark then set it to true and

over here if it's true we don't have to

go ahead okay we don't have to go ahead

and do something like this if pass is

equal to true if what's in pass is

equivalent no notice I'm using the

double equal sign to kind of compare and

ask something like is what on the right

echo towards the left that's why I'm

using a double equal sign now we can go

ahead and do something like this and

this will work but we don't have to do

that because pass itself is a boolean

variable and boolean variables can

either contain the value the value is

true or false

and it will contain true if the user

score is greater than the pass mark so

as even though you just cause greater

than the pass mark and past pad the path

boolean variable has been set to true so

if this whole thing if the boolean

expression or if what's in the

parentheses here is also true if past

contains the value true then this whole

thing will be true

right if paths contains the value over

here then this will be true so if true

then the only time the if statement will

execute the coding in the curly braces

is when what's in the parentheses over

here is also true and if past contains

true then this whole thing okay path

contains true then this if statement

will run what's in the curly braces

because what wasn't the parentheses over

here is true I hope you get me

all right so if past then we want to go

ahead and is clear message to the user

saying something like you passed so

system dot out dot println you passed

all right congrats right so a boolean

variable is just also a flag is

basically a way to signal to our program

that okay a setting condition has has

been reached or setting condition

existed now so over here this is this

was our way to signal to our program

that okay someone has passed the user

has passed right because we determine it

we've checked to see if the user score

is greater than or equal to past one and

if it's greater than or equal to the

past mark then we set the past boolean

variable to true if not then the value

of states you stay false right and over

here we are checking on the path VAR

value value either be true or false do

the if statement if this value is true

then the code is code will run or what's

new what's new curly braces will run if

it's false then what's in the

calibrators won't run so let's test this

out compile this and run and it says you

pass the congrats right let's go ahead

and change the user score to something

like 65 and then compile and then run

and we see that nothing happens because

it checks over here to say well first of

all we know pass is equal to false it

checks over here to say and and ask is

the user score which is 65 is it greater

than or equal to the past mark which is

80 60 is not greater than or equal to 80

so past is not set to true it's not set

to true this code like this line here

doesn't run because what the boolean

expression over here resulted to false

and only time that the cause and the

curly brace is executed is when the

boolean expression over here is also

true so past remain false if the user

score is not greater than or you

to the past mark and so we over here we

check the value of past if it contains

false we know this line of code one

front the only time an if statement runs

is when the boolean expression over here

results true okay whatever you type over

here is also true so if it's false then

this won't run and that's why when the

user school wasn't greater than the past

greater than equal to pass mark this

statement over here didn't run because

this resulted to false and this

statement it don't run and that's why we

didn't see anything we know that it

skips if this doesn't run then it skips

it skips it if this the boolean

expression was also false it jumps over

jumps you go move to the end of the if

statement a very end and continues

what's below it but we know there's

nothing below it so that's why we don't

see anything here I hope this is clear

so a flag is just a way to kind of

signal to your programmers okay setting

condition has been reached right and

we'll see more flags later on as we move

forward writing programs so if you have

any questions comments down below and

I'll do everything to respond to them

thank you very much for watching take

care of yourselves and I'll see you next

time with the next video

alright then bye bye