if Conditional selection statement

if Conditional selection statement

if runs one statement when a scalar condition is nonzero. Braces group code; else and else if handle alternatives. Use ==, not =; parentheses are required.

CategoryControl flow
KindKeyword / Statement
C standardC89 / C90

📝 Syntax

if (expr) statement; else statement;

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

positive

📚 Related Tutorials

❓ FAQ

QMust an if condition be written as a comparison?
ANo. Any scalar expression can be a condition: zero is false and nonzero is true. Pointers can be compared with a null pointer constant and integers can be tested directly, although an explicit comparison is often clearer.
QWhy doesn't if (x = 1) test whether x equals 1?
A= is assignment, so the expression first writes 1 to x and then uses the assignment result, 1, as a true condition. Use == for equality; compiler warnings can catch many accidental assignments.
QHow many statements does if control without braces?
AIt directly controls only the single statement immediately following it. Later statements run regardless of indentation. Use a compound statement, { ... }, to control multiple statements.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏