6 Control Flow
6.1 If
<iostream>
include
int main(int argc, char const *argv[])
{
int i;
std::cin >> i;
if (i % 2 == 0) std::cout << i << " is even" << std::endl;
else std::cout << i << " is odd" << std::endl;
return 0;
}
6.2 Switch
<iostream>
include
enum Color {RED, BLUE, GREEN};
int main(int argc, char const *argv[])
{
int i;
= RED;
Color c
std::cin >> i;
switch(i) {
case 0:
= RED;
c break;
case 1 :
= BLUE;
c break;
case 2 :
= GREEN;
c break;
default :
std::cout << "error: invalid color" << std::endl;
}
std::cout << c << std::endl;
return 0;
}