java_case

Java case Keyword

In Java, the case keyword is used within a switch statement to mark blocks of code that are executed when the switch's expression matches the case's value. The case statements within a switch block are tested sequentially until a match is found. Once matched, the block of code associated with that case is executed.

Code Example

<source lang=“java”> int number = 2; switch (number) {

   case 1:
       System.out.println("One");
       break;
   case 2:
       System.out.println("Two");
       break;
   default:
       System.out.println("Not One or Two");
} </source>

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

C

In C, the case keyword functions similarly to Java, being used inside a switch statement to execute a block of code based on the switch expression's value.

Code Example

<source lang=“c”> int number = 2; switch (number) {

   case 1:
       printf("One\n");
       break;
   case 2:
       printf("Two\n");
       break;
   default:
       printf("Not One or Two\n");
} </source>

https://en.cppreference.com/w/c/language/switch

C++

C++ also uses the case keyword within a switch statement in the same manner as C and Java, allowing conditional execution of code blocks.

Code Example

<source lang=“cpp”> int number = 2; switch (number) {

   case 1:
       std::cout << "One" << std::endl;
       break;
   case 2:
       std::cout << "Two" << std::endl;
       break;
   default:
       std::cout << "Not One or Two" << std::endl;
} </source>

https://en.cppreference.com/w/cpp/language/switch

Python

Python does not have a switch or case keyword. However, Python 3.10 introduced the match statement, which serves a similar purpose but with a different syntax and more capabilities, known as Structural Pattern Matching.

Code Example

<source lang=“python”> number = 2 match number:

   case 1:
       print("One")
   case 2:
       print("Two")
   case _:
       print("Not One or Two")
</source>

https://docs.python.org/3/tutorial/controlflow.html#match-statements

JavaScript

In JavaScript, the case keyword is used within a switch statement, similar to its usage in Java, C, and C++, to execute code based on the match of the switch expression.

Code Example

<source lang=“javascript”> let number = 2; switch (number) {

   case 1:
       console.log("One");
       break;
   case 2:
       console.log("Two");
       break;
   default:
       console.log("Not One or Two");
} </source>

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

PHP

PHP uses the case keyword inside a switch statement in a manner very similar to JavaScript, allowing execution of code blocks based on a matching case.

Code Example

<source lang=“php”> $number = 2; switch ($number) {

   case 1:
       echo "One";
       break;
   case 2:
       echo "Two";
       break;
   default:
       echo "Not One or Two";
} </source>

https://www.php.net/manual/en/control-structures.switch.php

Swift

Swift's case keyword is used within switch statements. Swift adds a level of pattern-matching capabilities to case statements, allowing for more powerful and flexible conditional expressions.

Code Example

<source lang=“swift”> let number = 2 switch number {

   case 1:
       print("One")
   case 2:
       print("Two")
   default:
       print("Not One or Two")
} </source>

https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html #ID127

Ruby

Ruby uses case in a slightly different manner. The Ruby case statement is more akin to a series of if-elsif statements and can include ranges and more complex conditions.

Code Example

<source lang=“ruby”> number = 2 case number when 1

   puts "One"
when 2
   puts "Two"
else
   puts "Not One or Two"
end </source>

https://ruby-doc.org/core-2.7.0/doc/syntax/control_expressions_rdoc.html#label-case+Expression

Go

Go does not use the case keyword in the same context as a switch statement directly. Instead, Go's switch statements can include initialization statements, and case expressions can be non-constants, making them more powerful and flexible.

Code Example

<source lang=“go”> package main

import “fmt”

func main() {

   number := 2
   switch number {
   case 1:
       fmt.Println("One")
   case 2:
       fmt.Println("Two")
   default:
       fmt.Println("Not One or Two")
   }
} </source>

https://golang.org/doc/effective_go#switch

Each language uses the case keyword or its functional equivalent within conditional structures, like switch statements or pattern matching constructs, to control the flow of execution. While the core idea remains similar—executing code based on matching conditions—the specifics of syntax, capabilities, and flexibility vary across different programming languages. The code examples provided illustrate the basic usage of case or its equivalent in each language, highlighting both the commonalities and unique features that each language brings to conditional logic and pattern matching. ```

This summary offers an insight into how different programming languages utilize the concept of `case` or its equivalents, showcasing the variations in syntax, usage, and functionality. For detailed usage and examples, the official documentation linked in each section is recommended.

java_case.txt · Last modified: 2024/05/01 03:52 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki