"if" Statements

This section describes 'if' statements - logical conditions to determine whether embedded statements should be executed or not.

The most commonly used conditional statement is the "if" statement, which has the following syntax:

if statement:
   if (logical_expression) {
      embeded_statements
   }

When an "if" statement is encountered in the execution flow, the logical expression will be evaluated first. If the resulting value is true, the embedded statements that are enclosed in braces immediately after the logical expression will be executed. If the resulting value is false, those embedded statements will be skipped.

An "if" statement can also have an "else" clause, which will contain a block of embedded statements to be executed only when the logical expression is evaluated to false. Here is the syntax of "if else" statement:

if statement:
   if (logical_expression) {
      embeded_statements
   } else {
      embeded_statements
   }

In the "else" clause, we could have another logical condition to separate the execution in two more ways:

if statement:
   if (logical_expression) {
      embeded_statements
   } else if (another_logical_expression) {
      embeded_statements
   } else {
      embeded_statements
   }

Last update: 2010.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

Logical Expressions and Conditional Statements

 Logical Expressions

"if" Statements

 "if" Statements - Example

 "switch" Statements

 "switch" Statements - Example

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version