Switch in Java
In this post, you will learn how to work with switch so that you can use that same knowledge and thus evolve.
- the switch is another way of working with decisions and can only check an equality relationship, ie it cannot use comparison operators;
- the case compares what was defined in itself with the content of the argument that was defined in the switch;
- the default is executed when the content of the argument that was defined in the switch is not found in any case.
package com.caffeinealgorithm.programminginjava;
public class Switch {
public void Run() {
char _case = 'D';
switch (_case) {
case 'A':
System.out.println("The case A exists.");
break;
case 'B':
System.out.println("The case B exists.");
break;
case 'C':
System.out.println("The case C exists.");
break;
default:
System.out.printf("The case %c does not exist.", _case);
break;
}
}
}
// The case D does not exist.
Don’t forget to watch the video and you can always read this post in Portuguese.