Conditionals compared across 16 programming languages
Make decisions and control the flow with if/else or its equivalents.
PyPython
punkte = 73
if punkte >= 90:
print("very good")
elif punkte >= 70:
print("good")
else:
print("needs improvement")Indentation forms the block; elif stands for "else if".
JSJavaScript
const punkte = 73;
if (punkte >= 90) {
console.log("very good");
} else if (punkte >= 70) {
console.log("good");
} else {
console.log("needs improvement");
}Curly braces form the block; always compare with === instead of ==.
TSTypeScript
const punkte: number = 73;
if (punkte >= 90) {
console.log("very good");
} else if (punkte >= 70) {
console.log("good");
} else {
console.log("needs improvement");
}Branching is identical to JavaScript; TypeScript additionally narrows types inside the branches (narrowing).
JvJava
public class Main {
public static void main(String[] args) {
int punkte = 73;
if (punkte >= 90) {
System.out.println("very good");
} else if (punkte >= 70) {
System.out.println("good");
} else {
System.out.println("needs improvement");
}
}
}The condition goes in parentheses and must be a boolean; blocks are formed with curly braces.
CC
#include <stdio.h>
int main(void) {
int punkte = 73;
if (punkte >= 90) {
printf("very good\n");
} else if (punkte >= 70) {
printf("good\n");
} else {
printf("needs improvement\n");
}
return 0;
}Conditions go in parentheses, blocks in curly braces. Every value other than 0 counts as true.
C++C++
#include <iostream>
int main() {
int punkte = 73;
if (punkte >= 90) std::cout << "very good\n";
else if (punkte >= 70) std::cout << "good\n";
else std::cout << "needs improvement\n";
}The condition goes in parentheses. For a single statement, curly braces are optional – teams usually recommend them anyway.
C#C#
int punkte = 73;
if (punkte >= 90)
Console.WriteLine("excellent");
else if (punkte >= 70)
Console.WriteLine("good");
else
Console.WriteLine("needs improvement");The condition must be a bool and goes in parentheses; blocks with { } are optional for single statements.
GoGo
package main
import "fmt"
func main() {
punkte := 73
if punkte >= 90 {
fmt.Println("excellent")
} else if punkte >= 70 {
fmt.Println("good")
} else {
fmt.Println("needs improvement")
}
}No parentheses around the condition, but always curly braces around the block. Go has no ternary operator.
RsRust
fn main() {
let punkte = 73;
if punkte >= 90 {
println!("very good");
} else if punkte >= 70 {
println!("good");
} else {
println!("needs improvement");
}
}No parentheses around the condition, and it must be a bool. if is also an expression: let note = if ... { "a" } else { "b" };.
PHPPHP
<?php
$punkte = 73;
if ($punkte >= 90) {
echo "excellent\n";
} elseif ($punkte >= 70) {
echo "good\n";
} else {
echo "needs improvement\n";
}The condition goes in parentheses, the block in curly braces. For mapping values, match is often the more elegant alternative.
RbRuby
punkte = 73
if punkte >= 90
puts "excellent"
elsif punkte >= 70
puts "good"
else
puts "needs improvement"
endNo parentheses around the condition, the block ends with end. The further branch is called elsif. Only nil and false count as falsy.
SwSwift
let punkte = 73
if punkte >= 90 {
print("excellent")
} else if punkte >= 70 {
print("good")
} else {
print("needs improvement")
}No parentheses around the condition, but braces around the block are mandatory. For many cases, switch with ranges (90...100) is clearer.
KtKotlin
fun main() {
val punkte = 73
val note = when {
punkte >= 90 -> "very good"
punkte >= 70 -> "good"
else -> "needs improvement"
}
println(note)
}when without an argument is the Kotlin counterpart to if / else if; since it is an expression, it directly yields the value for note.
SQLSQL
SELECT name, preis,
CASE
WHEN preis >= 500 THEN 'premium'
WHEN preis >= 50 THEN 'medium'
ELSE 'budget'
END AS klasse
FROM produkte
ORDER BY preis DESC;CASE WHEN is the if/elif/else of SQL – as an expression, not a statement. The conditions are checked in order, and the first match wins.
shBash
punkte=73
if [[ $punkte -ge 90 ]]; then
echo "very good"
elif [[ $punkte -ge 70 ]]; then
echo "good"
else
echo "needs improvement"
fi[[ ... ]] is the Bash test construct; you compare numbers with -ge, -lt, -eq … (text with ==, !=). Every block ends with fi.
</>HTML & CSS
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.box { padding: 14px; background: #ddd; margin-bottom: 8px; }
.box::after { content: " – schmaler Bildschirm"; }
@media (min-width: 420px) { .box { background: #cfe8ff; } .box::after { content: " – breiter Bildschirm"; } }
@supports (display: grid) { .raster { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; } }
@supports not (display: grid) { .raster { color: red; } }
.raster div { background: #fde; padding: 8px; }
.karte { border: 2px solid #ccc; padding: 8px; }
.karte:has(img, svg) { border-color: seagreen; }
</style>
<div class="box">@media (min-width: 420px)</div>
<div class="raster"><div>@supports grid</div><div>Cell 2</div></div>
<p class="karte">Card without an image (gray border)</p>
<p class="karte"><svg width="16" height="16"><circle cx="8" cy="8" r="7" fill="green"/></svg> Card with an image: :has() colors the border</p>CSS has no if, but it has conditions: @media checks environment/device, @supports checks feature support, and :has() selects elements depending on their content or state. Change the width of the preview to see @media.