Skip to content

Instantly share code, notes, and snippets.

@arkenidar
Created May 6, 2026 21:21
Show Gist options
  • Select an option

  • Save arkenidar/f16e22a3c49524c83d723e056915439e to your computer and use it in GitHub Desktop.

Select an option

Save arkenidar/f16e22a3c49524c83d723e056915439e to your computer and use it in GitHub Desktop.
[select] about conditional branching especially selection-branching
// c language : libraries
#include <stdio.h>
#include <string.h>
// handle cases : definitions of cases
enum Kind { Kind1, Kind2, Kind3, LastKindCount };
const char* const KindArray[LastKindCount] = { "Kind1", "Kind2", "Kind3"};
// c language : program entry-point
int main(){
printf("switch example\n");
// (safe) string input
char buffer[100];
printf("Enter a string: ");
if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
// Remove trailing newline if present
buffer[strcspn(buffer, "\n")] = '\0';
printf("You entered: %s\n", buffer);
}
// handle cases : search foundKind
int foundKind = -1;
for(int i=0; i<LastKindCount; i++){
if(0==strcmp(buffer,KindArray[i])){
foundKind=i; break;
}
}
// handle cases : switch(foundKind)
switch(foundKind){
case Kind1:
printf("switch -> Kind1\n"); break;
case Kind2:
printf("switch -> Kind2\n"); break;
case Kind3:
printf("switch -> Kind3\n"); break;
default:
printf("switch -> default\n");
}
}
// ✅ Strings
let lang = "Python";
switch(lang) {
case "JavaScript":
console.log("Web developer");
break;
case "Python":
console.log("Data Scientist");
break;
}
// ✅ Mixed types (strict comparison ===)
let value = "42";
switch(value) {
case 42: // ❌ Won't match (string !== number)
break;
case "42": // ✅ Will match
break;
}
// ✅ Templates (complex expressions)
let x = 10;
switch(true) { // Trick for ranges
case x < 5:
console.log("Less than 5");
break;
case x <= 10:
console.log("5 to 10");
break;
}
// addendum (see also php switch)
function fizzbuzz(num) {
switch (0) {
case num % 15 : return "FizzBuzz";
case num % 3 : return "Fizz";
case num % 5 : return "Buzz";
default : return num;
}
}
for (var i = 0; i <= 20; i++)
{
console.log( fizzbuzz(i) );
}
<?php
// ✅ Strings!
$lang = "Python";
switch($lang) {
case "JavaScript":
echo "Web developer\n";
break;
case "Python":
echo "Data Scientist\n";
break;
case "PHP":
echo "Backend developer\n";
break;
}
// ✅ Integers
$num = 42;
switch($num) {
case 42:
echo "Answer\n";
break;
}
// ✅ Mixed types! (loose comparison by default)
$value = "42";
switch($value) {
case 42: // Will match! (string "42" == int 42)
echo "Matches due to type juggling\n";
break;
}
// ✅ Strict comparison with switch
switch($value) {
case 42: // Won't match if using strict_types
break;
}
// ✅ Expressions (not just constants)
$threshold = 10;
switch($x) {
case $threshold: // OK in PHP
break;
case $threshold * 2:
break;
}
// ✅ Multiple values per case
switch($x) {
case 1:
case 2:
case 3:
echo "1, 2, or 3\n";
break;
}
// addendum (see also php match)
function fizzbuzz($num) {
switch (0) {
case $num % 15 : return "FizzBuzz";
case $num % 3 : return "Fizz";
case $num % 5 : return "Buzz";
default : return $num;
};
}
for ($i = 0; $i <= 20; $i++)
{
print( fizzbuzz($i) . PHP_EOL );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment