+ 2

Please tell me where am I wrong with this go prog

package main import "fmt" func main() { var f int fmt.Scanln(&f) //your code goes here Switch f { case f<0: fmt.Println("Wrong Input") case f>=0 && f<20: fmt.Println("Infrasound") case f>=20 && f<=20000: fmt.Println("Audible") default: fmt.Println("Ultrasound") } }

23rd Jul 2021, 5:52 PM
Pratyush Anshuman
Pratyush Anshuman - avatar
10 odpowiedzi
+ 1
Pratyush Anshuman That's why because when you write a variable with switch then its mean the type of that variable and the type of each case should be same. Since you have written f with switch which is a type of int but in each cases the type is boolean (f < 0 returns boolean) so in this case type mismatch so you can't write a variable with switch.
23rd Jul 2021, 6:50 PM
A͢J
A͢J - avatar
+ 4
If a variable needs to be compared with something, (==), then the entry is made together with the variable, (switch variable {}), each case statement includes a value for comparison. If you need to write some condition in the "case", (<, >), then you need to use switch without a variable, since the variable is written in the "case". 👇 👇 switch { case f<0: 👈 fmt.Println("Wrong Input") case f>=0 && f<20: fmt.Println("Infrasound") case f>=20 && f<=20000: fmt.Println("Audible") default: fmt.Println("Ultrasound") }
23rd Jul 2021, 6:17 PM
Solo
Solo - avatar
+ 2
Pratyush Anshuman There is only "switch" not "switch f"
23rd Jul 2021, 6:01 PM
A͢J
A͢J - avatar
+ 1
switch with capital S ("Switch") is not a keyword
23rd Jul 2021, 6:06 PM
minirkk
minirkk - avatar
+ 1
Thank you everyone. Sometimes switch is written with the variable and sometimes we use just switch. When should we use switch with variable and when alone
23rd Jul 2021, 6:41 PM
Pratyush Anshuman
Pratyush Anshuman - avatar
+ 1
Pratyush Anshuman When you don't have condition in cases then you have write a variable like this: switch num { case 1: //Statement case 2: // Statement } In this case you have to write a variable with switch.
23rd Jul 2021, 6:45 PM
A͢J
A͢J - avatar
+ 1
Ok I got it. Thank you AJ.
23rd Jul 2021, 6:47 PM
Pratyush Anshuman
Pratyush Anshuman - avatar
+ 1
Thanks again. Another thing is how r u able to tag me everytime you answer. I am searching for it but cant find how
23rd Jul 2021, 6:52 PM
Pratyush Anshuman
Pratyush Anshuman - avatar
+ 1
Thanks A͢J
23rd Jul 2021, 7:00 PM
Pratyush Anshuman
Pratyush Anshuman - avatar
0
Use @ symbol before name. List of names will be open (list of your followers, following and also the name of the person who has given answer) . Now you can select any name.
23rd Jul 2021, 6:59 PM
A͢J
A͢J - avatar