[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CIf
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]
CIf
(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) { printf("You threw head."); } else { printf("You threw tail."); } return 0; }
- include <stdlib.h>
- include <stdio.h>
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) { printf("Life is bad."); } } else { for (int i=0; i!=10; ++i) { printf("Life is good."); } } //You can skip accolades for a single statement if (1==2) printf("Life is bad."); //One statement else printf("Life is good."); //One statement //The else condition is not obligatory if (1==2) printf("Life is bad."); //One statement printf("Today you should go to the pub."); return 0; }
- include <iostream>
if in ?debugging
When you use if's for ?debugging, prefer using assert statements.Code links
'if' links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
