What is the difference Switch and if | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the difference Switch and if

5th Jan 2020, 12:00 PM
Solestebano
2 Answers
+ 3
With switch statement can only check value's equality and run certain code block based on it. But with if statement you can do more complicated operations, with swift it would be harder to for example check whether x < 12. But using swift you can run the rest statements even though they did not match, until break is accessed, example: https://code.sololearn.com/c3wC71wvuznm/?ref=app
5th Jan 2020, 12:12 PM
Seb TheS
Seb TheS - avatar
0
if statement's check condition of two variable true, /false and for example if(a == b) { System.out.print (" true " ) ; else System.out.println("false ") ; A switch statement tests a variable for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case . For example int color = 2; Switch (color ) { case 1 : System.out.println ("red") ; break ; case 2 : System.out.println ("black ") ; break ; case 3 : System.out.println ("green ") ; break ; default : System.out.println ("doesn't found your choice result ") ; break ; }
5th Jan 2020, 1:33 PM
Vikram Singh
Vikram Singh - avatar