25
loading...
This website collects cookies to deliver better user experience
#include <iostream>
using namespace std;
int main() {
int http = 200;
switch (http) {
case 200:
cout<<"OK";
break;
case 201:
cout<<"CREATED";
break;
case 202:
cout<<"ACCEPTED";
break;
case 400:
cout<<"BAD REQUEST";
break;
case 404:
cout<<"NOT FOUND";
break;
case 503:
cout<<"SERVICE UNAVAILABLE";
break;
default:
cout << "Error! HTTP Code is not valid";
break;
}
return 0;
}
ok
if..else
and if...elif...else
statements to write conditional statements, but the all new Python Structural Pattern Matching provides a more elegant and legible way to write some conditional statement.match subject:
case pattern1:
#case 1 code block
.....
....
...
case patternN:
#case N code block
match
is the keyword here.subject
is the value that need to be match.case
is also a keywordpattern
is the value that will be match with subject
if any case pattern matches the match
subject
that case code block will be executed.http = 200
match http:
case 200:
print("OK")
case 201:
print("CREATED")
case 202:
print("ACCEPTED")
case 400:
print("BAD REQUEST")
case 404:
print("NOT FOUND")
case 503:
print("SERVICE UNAVAILABLE")
case _:
print("Error! HTTP Code is not valid")
OK