C++ Conditional Statements
Developer, PlayZion.com
A C++ Tutorial - Conditional statements
The ability to control the flow of your program, letting it make decisions on what code to execute, is valuable to the programmer. The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. Below you will find sample conditional statements and a list of relational operators to use with the statements.
Relational Operators:
equal: ==
not equal: !=
less than: <
greater than: >
less than or equal to: <=
greater than or equal to: >=
not: !
and: &&
or: ||
If statements:
if (<conditional expression>) {
<one or more statements>
}
if/else statement:
if (<conditional expression #1>) {
<one or more statements>
}
else if (<conditional expression #2>) {
<one or more statements>
}
else {
<one or more statements>
}
Note: If you're testing whether two things are equal or not, use ==, not =. Using = will set the first variable to the second.
For example,
if (Age == 20) {
cout << "I am twenty years old."
}
Switch Statements
If you want to test whether a variable takes one of a series of values, it's easier to use a switch statement than an if statement. Switch statements are very similar to 'Case' in VB or WINOOT.
switch (<variable>) {
case <value 1>:
<one or more statements>
break;
case <value 2>:
<one or more statements>
break;
default:
<one or more statements>
break;
} file://end switch
