I am trying to learn the break
function in java , for example this following code:
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int num = in.nextInt();
switch (num) { // switching values for num
case 1: // if num is 1
System.out.println("1");
break; // stop here and don't continue.
case 2: // if num is 2
System.out.println("F");
break; // stop here and don't continue
default: // if all cases are wrong
System.out.println("R");
break; //finish
}
So my question is for example, if I am deleting the break
after case 1
, why does it print "F"? also if there is no break
there, it's still supposed to check if num=2
in the line case 2:
, so, why without break
in case 1:
, it skips to case 2
without checking if it is true and doing what is inside of it?