In the code explained by sir why he has used boolean variable when we have already used print statement that key is found and included the condition of key is found. Actually I’m facing problem in when to use boolean variables in our code.
Staircase search
bool variable is used to check that whether we found the key or not
if in question you only have to find that key is present or not
how will you do this
for that you have to use bool variable
But for finding the key we have included the if statement right
yes if statement will always be applied
Then by applying if statement it is identified that key is present.
yes
if(arr[i][j] = key)
you break the loop and come out of loop
but if key is not found in the entire matrix then also you come out of loop
now how do you identify when you come out of loop
by finding key or by not getting any key
for this reason we have to make a bool variable
Yeah,I completely agree with you but there we have given cout statement before break so when the if condition is met that statement will be printed.
obviously yes it will be printed first then break executed
By that printing statement it is identified that key is present
by printing you can’t identified that key is present or not
because printing will be shown at the end of completion of program