[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppIf
The if-statement can be used for debugging by checking certain values. If you do so, check the use of assert, which is an if-statement that can be 'removed' from you code by the preprocessor.
.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppIf
(C++) if
Keyword that checks a condition. If a condition is true, then it performs the statement(s) after if, else it performs the statement(s) after else.int main() { //A coin-toss if (rand()%2==0) { std::cout << "You threw head." << std::endl; } else { std::cout << "You threw tail." << std::endl; } return 0; }
- include <iostream>
The if-statement can be used for debugging by checking certain values. If you do so, check the use of assert, which is an if-statement that can be 'removed' from you code by the preprocessor.
Syntax
This program shows the syntaxes of if and if..else. It also puts wise words on the screenint main() { //Multiple statements using accolades '{' and '}' if (1==2) { for (int i=0; i!=10; ++i) { std::cout << "Life is bad." << std::endl; } } else { for (int i=0; i!=10; ++i) { std::cout << "Life is good." << std::endl; } } //You can skip accolades for a single statement if (1==2) std::cout << "Life is bad." << std::endl; //One statement else std::cout << "Life is good." << std::endl; //One statement //The else condition is not obligatory if (1==2) std::cout << "Life is bad." << std::endl; //One statement std::cout << "Today you should go to the pub." << std::endl; return 0; }
- include <iostream>
if in debugging
When you use if's for debugging, prefer using either try and catch or assert statements. These both have their own strengths, so see them both.Code links
'if' links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
