How to compare a var to 0 ?

34 views
Skip to first unread message

Zipette

unread,
Jan 23, 2015, 5:42:31 PM1/23/15
to swift-l...@googlegroups.com
Hello,
Why the code bellow doe not compile...
var a:INt=5
if(a !=0)
{
     Println("Hello word")
}

The compilator does't accept this code, hox can copare the var "a" to 0 ?

Thanks.

Jeremy Tregunna

unread,
Jan 23, 2015, 5:48:24 PM1/23/15
to Zipette, swift-l...@googlegroups.com
Problem seems to be that you’ve spelled Int as INt. Fixing that, and fixing Println to println resolves any issues when I punch this into the REPL.

If you’re still having issues, perhaps you can tell us what the exact error message says, and provide us with the actual subset of code that’s erroring. We might be able to help a bit better.

-Jeremy
-- 
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/cf8a056c-ee34-4eb3-8c78-77cd9d6c31d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Greene

unread,
Jan 23, 2015, 5:49:31 PM1/23/15
to Zipette, swift-l...@googlegroups.com
// Try this instead.
let a = 5
if a != 0 {
  println("hello world")
}

--

Zipette

unread,
Jan 23, 2015, 6:50:58 PM1/23/15
to swift-l...@googlegroups.com, conta...@votrecalendrier.com
I have try 
let a = 5
if a != 0 {
  println("hello world")
}

but the compiler says : "Expeted '{' after 'if' condition" . I do not realy undesrtand where is my error... Any other advizes ?

Thks

Darrell Nicholas

unread,
Jan 24, 2015, 6:19:26 PM1/24/15
to swift-l...@googlegroups.com
Another thing, other than Int being spelled as INt and println having a capital P, you also have to have equal spaces on either side of your operator "!=". Either (a != 0) but you don't even need the parenthesis in this instance. 

var a:Int = 5
if a != 0 {
println("Hello world")
}

Hope that helps you. In a lot of cases you can have equal spaces or equal no spaces on either side of your operand, I think maybe on in assignments. a=5 or a = 5, but I
a= 5 or a =5 is wrong and will give an error. This is due to Swift's ability to do operator overloading. You could create an operator called a= that does something you define. The compiler looks at those spaces or lack or spaces due to this reason.

I am by no means a Swift expert, I've only been using it since June of last year ;-)

No really I just do this stuff as a hobby and this answer may already be answered by someone else, but I knew the answer to this one so I wanted to jump in and contribute where I could.

Zipette

unread,
Jan 24, 2015, 6:39:25 PM1/24/15
to swift-l...@googlegroups.com
Thanks a lot Darrell ! It was the problem ! I didn't let a the right spaces between terms, so you are completly true, the right formule is :

var a:Int = 5
if a != 0 {
println("Hello world")
}

As you notice, a space between "a" and "!=" and also a space between "=" and "0"

Thanks again for the time you spent on my question and also thanks to other one for their answers !
By

Darrell Nicholas

unread,
Jan 26, 2015, 6:14:03 PM1/26/15
to swift-l...@googlegroups.com
No problem. Glad to be of help. I just learned that very thing a few days ago.
Reply all
Reply to author
Forward
0 new messages