+ 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") } }
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.
+ 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")
}
+ 2
Pratyush Anshuman
There is only "switch" not "switch f"
+ 1
switch with capital S ("Switch") is not a keyword
+ 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
+ 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.
+ 1
Ok I got it. Thank you AJ.
+ 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
+ 1
Thanks A͢J
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.