# How to declare a Boolean variable?

**URL:** https://discourse.julialang.org/t/how-to-declare-a-boolean-variable/41279
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [June 12, 2020, 4:23pm UTC](https://discourse.julialang.org/t/how-to-declare-a-boolean-variable/41279 "2020-06-12T16:23:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)
#### Post date: [June 12, 2020, 4:23pm UTC](https://discourse.julialang.org/t/how-to-declare-a-boolean-variable/41279/1 "2020-06-12T16:23:14Z")

</div>

I need to declare a Boolean variable but I don’t know how to use `Bool`. How should I do ?  
I need the variable “i” to assume two values (1, 0)

```julia
using JuMP, Cbc
Model1 = Model(with_optimizer(Cbc.Optimizer))

@variable(Model1,d[1:i], lower_bound=0, Bool = true)  

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 12, 2020, 4:46pm UTC](https://discourse.julialang.org/t/how-to-declare-a-boolean-variable/41279/2 "2020-06-12T16:46:30Z")

</div>

```julia
julia> a = true
true

julia> typeof(a)
Bool

```

Alternatively `Bool` can be called as constructor, `Bool(0)`

I’m not sure this answers your question though, as you seem to be using `i` to construct a range for indexing something in the code you posted (i.e. you would have `1:1` if `i == true` and `1:0` if `i == false` due to type promotion)

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [June 12, 2020, 5:10pm UTC](https://discourse.julialang.org/t/how-to-declare-a-boolean-variable/41279/3 "2020-06-12T17:10:36Z")

</div>

`@variable(Model1, d[1:i], Bin)`

[https://jump.dev/JuMP.jl/v0.21.1/variables/#Binary-(ZeroOne)-constraints-1](https://jump.dev/JuMP.jl/v0.21.1/variables/#Binary-(ZeroOne)-constraints-1)

---

<div class="post-metadata">

### Author: ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)
#### Post date: [June 15, 2020, 4:06pm UTC](https://discourse.julialang.org/t/how-to-declare-a-boolean-variable/41279/6 "2020-06-15T16:06:02Z")

</div>

How will i know if `i` really is assuming the value `[0,1]` ?

```julia
using JuMP, Cbc, MathOptInterface
Model1 = Model(with_optimizer(Cbc.Optimizer))

@variable(Model1,d[1:i], lower_bound=0, Bin) 
is_binary(d) #I can't compile the is_binary command, to return " true" to binary 

```

Thanks

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [June 15, 2020, 5:22pm UTC](https://discourse.julialang.org/t/how-to-declare-a-boolean-variable/41279/7 "2020-06-15T17:22:47Z")

</div>

Here is the documentation for a binary variable: [Variables · JuMP](https://jump.dev/JuMP.jl/v0.21.1/variables/#Binary-(ZeroOne)-constraints-1)  
Here is the documentation for variable containers: [Variables · JuMP](https://jump.dev/JuMP.jl/v0.21.1/variables/#Variable-containers-1)

Avoiding the `i` issue, you code will make a vector of binary variables, where `d[i] in {0, 1} `.

```nohighlight
using JuMP
model = Model()
@variable(model, d[1:2], Bin)
is_binary(d[1]) # true
is_binary(d[2]) # true

```

If you really want `i` to be the index, and for it to be in `{0, 1}`, that doesn’t make sense. Do you want 0 variables or 1 variable? If you just want a variable called `i` to be 0 or 1, use:

```nohighlight
using JuMP
model = Model()
@variable(model, i, Bin)

```

p.s. You should also post in the [Optimization (Mathematical) - JuliaLang](https://discourse.julialang.org/c/domain/opt/13) category if you’re posting about JuMP. It makes the post a little more visible to the people interested in optimization.
