Skip to content

Case Statements

Another way to check conditions

  • Lesson
  • 1 min read
  • 12 of 15 in this chapter

What are case statements?

Case statements are basically condensed if/else if statements. The code will check the value of a data type. Depending on certain "cases" it will execute code. For example...

Java
int checkingInt = 5;
switch(checkingInt){
	//switch(data type that you are checking)
	case 1:System.out.println("the number 1");break;
	//case "value": code;break; is the syntax
	//in case the value is one it prints "the number 1"
	case 2:System.out.println("the number 2");break;
	case 3:System.out.println("the number 3");break;
	case 4:System.out.println("the number 4");break;
	case 5:System.out.println("the number 5");break;
	default:System.out.println("any other number");break;
	//if it doesn't find any, its default is set here
}
//the output is "the number 5"