Doubt in my code

Hello, I just want to know the flow of conditional statements used in the function binary_Search ( ). What if I use ’ if ’ instead of using ’ else if ’ ?

Here is my code :

hello @yashsharma4304
image

u can use if in line 12 and else in line 15.

But will it create any difference if I use if in all cases ?

yeah it will make difference.
on using if it may perform more that one updation in single iteration which we dont want.

Yeah but in this case how is it possible to have both conditions correct at the same time ?
key > a[m] and key < a[m] .

it is possible.
let say initilally key > a[m] this condition holds true.
and u perform updation which makes this condition key < a[m] true.
so now second if will also execute and may produce wrong result

1 Like

Oh… :hushed: . Okkk Thanks for clarifying my thought. :slightly_smiling_face:

But, I want to know if I use the same code

Let’s say key == a[m] holds false, key < a[m] holds false and key >a[m] holds true.

How will the flow works. I mean will it check all the conditions whether it is false or true ? Or it will simply move to the condition which is true without checking for the false conditions?

see in if- else if (some number of times) -else

first condition of if get checked if it satisfies then no other block will execute.

if condition fails then next else if condition will get checked if it satisfies then no other block will execute .
if it also fails then next else if will get checked and same thing will followed for remaining blocks

1 Like

Ok so if- else if (some number of times) -else is used for checking exactly one condition. But if I use all ’ if ’ statements then it will check all the conditions and executes all the blocks which are true. Right ?

yeah right . … … .

1 Like