The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. |
![]() |
Home ![]() ![]() |
||
See also: Chained conditionals, Nested Conditionals, Alternative Execution | ||
![]() ![]() ![]() ![]() ![]() ![]() |
||
Conditional Execution
cout << "x is positive" << endl; } The expression in parentheses is called the condition. If it is true, then the statements in brackets get executed. If the condition is not true, nothing happens. The condition can contain any of the comparison operators: x == y // x equals yx != y // x is not equal to y x > y // x is greater than y x < y // x is less than y x >= y // x is greater than or equal to y x <= y // x is less than or equal to y Although these operations are probably familiar to you, the syntax C++ uses is a little different from mathematical symbols like =, neq and le. A common error is to use a single = instead of a double ==. Remember that = is the assignment operator, and == is a comparison operator. Also, there is no such thing as =< or =>. The two sides of a condition operator have to be the same type. You can only compare ints to ints and doubles to doubles. Unfortunately, at this point you can't compare Strings at all! There is a way to compare Strings, but we won't get to it for a couple of chapters.
|
||
Home ![]() ![]() |