[WEBINAR] The New Normal: Dealing with the Reality of an Unsecure World

If Statement

Definition - What does If Statement mean?

An if statement, in C#, is a programming construct in C# used to selectively execute code statements based on the result of evaluating a boolean expression. The boolean expression must return either a true or false value.

The if statement is used as a control statement to branch to different sections of code depending on the result of the boolean conditional expression. The expression is stated within parentheses and evaluated during execution. If the expression results in a true value, the code following the if statement is executed. Otherwise, the code following an optional "else" statement is executed. If there is no else statement, execution continues with the code after the if block.

The if statement provides a decision-making capability by which one piece of code is executed instead of the other based on one or more specified conditions.

Techopedia explains If Statement

Multiple conditions can be combined in the boolean expression to be evaluated within the if statement by using the OR operator (||) and/or the AND operator (&&). The other set of corresponding logical operators are | and &. The logical operators && and || produce more efficient code than the other regular ones because the entire expression does not always need to be evaluated. As a result, they are called short-circuit operators.

For example, an if statement can be used to check if a character input to the program is an alphabetical, numeric, or special character.

In contrast to languages like C or C++, where a result of zero is considered to be a false value and a number other than zero is considered to be true, C# expects results as boolean values (true or false) only. Hence, for numeric conditions such as variable not equal to zero, the expression has to be explicitly written for that condition and not just evaluated with the number itself placed within parentheses.

Although the switch construct forms an alternative to the if statement and can provide better performance and clearer code, it can only be used to test expressions against constant values. The if statement can be replaced with a ternary operator when a condition is evaluated. The result of the evaluation is considered for assigning a variable to a value.

This definition was written in the context of C#

Techopedia Deals

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
"Techopedia" on Twitter

Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Free Whitepaper: The Path to Hybrid Cloud
Free Whitepaper: The Path to Hybrid Cloud:
The Path to Hybrid Cloud: Intelligent Bursting To Amazon Web Services & Microsoft Azure
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.