// page information $page_type = "t"; $page_title = "C++ Conditional Statements"; $page_keywords = "c++, c, c plus plus, conditional statements, relational operators, if statements, if else statement, switch statements, else, if, switch, statements"; $page_description = "C++ conditional statements tutorial. Find more tutorials and scripts at TheScripts.com, a programming and software development resource, directory and community."; $page_articletitle = "C++ Conditional Statements"; $page_next_url = ""; $page_next_anchor = ""; $page_prev_url = ""; $page_prev_anchor = ""; $page_author = "John Bourbonniere"; $page_byline = "Developer, PlayZion.com"; // site header include ($_SERVER["DOCUMENT_ROOT"]."/header.php"); // begin html ?>
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.
equal: ==
not equal: !=
less than: <
greater than: >
less than or equal to: <=
greater than or equal to: >=
not: !
and: &&
or: ||
if (<conditional expression>) {
<one or more statements>
}
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."
}
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