Saturday 18 February 2012

The if Statement in C and C++

While it’s possible to write code without ever using the if statement, you’ll probably use it all the time. Below is a summary of its use in C and C++.

1. If Statement—One Line No Else

The simplest form of the if statement is one line and executes when the condition is true.

if (condition) statement[,statement...];

If is a C++ keyword and must be in lowercase. C++ is case sensitive. The code after (condition) to the semicolon will execute if the condition is true. Note, the word THEN is not used.

Sample Code:

The sample code was created in MS Visual C++ as a Windows Form Application. It contains a form (Form1) that displays when the program runs. The sample code, contained in the Form Load Event, executes and adds text to a RichTextBox named rtOut.

// Declare Boolean variable.
bool bCondition;
// Set to true.
bCondition = true;
// Add text to text property of RichtextBox rtOut.
if (bCondition) rtOut->Text += "bCondition = true;\n";

Since bCondition is true, the code will add the string "bCondition = true;\n" to the text property of rtOut.

Output:

bCondition = true;

While you can write a one-line if statement with several C++ statements after the condition, it’s preferable to use a block statement as discussed below. Block statements are easier to read.

Declaring the Boolean variable and assigning an initial value at the same time also works.

bool bCondition = true;
if (bCondition) rtOut->Text += "bCondition = true;\n";


2. If Statement—One Line With Else

You can use the keyword else to execute code when the condition is false.

if (condition) statement[,statement...];
else statement[,statement...];

Sample:

// Set to false.
bCondition = false;
// Add text to text property of RichtextBox rtOut.
if (bCondition) rtOut->Text += "bCondition = true;\n";
else  rtOut->Text += "bCondition = false;\n";

Since bCondition is false, "bCondition = false;\n" is added to rtOut;

Output:

bCondition = false;


3. If Statement—Block Statements

If you want to execute many lines of code instead of one. You do that by creating a block statement. Sometimes referred to as a compound statement or compound statements. A block statement starts with a curly opening bracket { and ends with the closing bracket }. Some people refer to them as braces. (See wikipedia for more.)

if (condition)
{
   statement;
   statement;
}
else
{
   statement;
   statement;
}

An example with true.

// Set to true.
bCondition = true;
if (bCondition)
{
  // Text added to rtOut.
  rtOut->Text += "bCondition = true;\n";
  rtOut->Text += "It really is true.\n";
}

An example with false.

// Set to false.
bCondition = false;
if (bCondition)
{
  // Not executed.
  rtOut->Text += "bCondition = true;\n";
}
else
{
  // Text added to rtOut.
  rtOut->Text += "bCondition = false;\n";
  rtOut->Text += "Honest. It's false.\n";
}

Output:

bCondition = true;
It really is true.
bCondition = false;
Honest. It's false.

Placing the curly brackets on separate lines is not a requirement. It’s a convention to make the code more readable for humans, not the computer. To the compiler, the source code is one long series of characters.

4. If…Else If…Else Statement

The if…else statement allows for a binary execution of code, true or false. The use of the if…else if…else allows for more than two conditions. It’s structured as follows:

if (condition) statement[,statement...];
else if (condition) statement [,statement...];
else statement [,statement...];

Or with block statements.

if (condition)
{
   statement;
   statement;
}
else if (condition)
{
   statement;
   statement;
}
else (condition)
{
   statement;
   statement;
}

You can have more than one else if lines.

This sample code acts on the value of an integer variable being positive, zero or negative.

// Declare integer variable.
int iCondition;

// Positive.
iCondition = 10;
rtOut->Text += "iCondition set to 10\n";
if (iCondition > 0 ) rtOut->Text += "iCondition > 0;\n";
else if (iCondition < 0 ) rtOut->Text += "iCondition < 0;\n";
else  rtOut->Text += "iCondition = 0;\n";

// Negative.
iCondition = -10;
rtOut->Text += "iCondition set to -10\n";
if (iCondition > 0 ) rtOut->Text += "iCondition > 0;\n";
else if (iCondition < 0 ) rtOut->Text += "iCondition < 0;\n";
else  rtOut->Text += "iCondition = 0;\n";

// Zero.
iCondition = 0;
rtOut->Text += "iCondition set to 0\n";
if (iCondition > 0 ) rtOut->Text += "iCondition > 0;\n";
else if (iCondition < 0 ) rtOut->Text += "iCondition < 0;\n";
else  rtOut->Text += "iCondition = 0;\n";

Output:

iCondition set to 10
iCondition > 0;
iCondition set to -10
iCondition < 0;
iCondition set to 0
iCondition = 0;


5. Condition & Data Types

Any valid C++ expression can be used to control the if statement.  The type of conditional expression need not be restricted to only those involving the relational and logical operators or to the operands of the type bool. All that is required is that the controlling expression evaluate to either a true or false. A value of zero is automatically converted into false and all non-zero values are converted into true. Thus any expression that can be converted into a number can be used as the control for an if statement.

Here are some data types that can be used as a condition to control an if statement.

bool bCondition = true | false;

int iCondition = non-zero | zero;

float fCondition = non-zero | zero;

char cCondition = non-zero | zero;

While you would normally use the keywords true or false with a Boolean data type, C++ won’t object to assigning a numeric value to a Boolean variable. The condition will equate zero as false and non-zero as true.

// Declare Boolean variable.
bool bCondition;

// Set to positive value.
bCondition = 10;
if (bCondition) rtOut->Text += "bCondition = 10;\n";

// Set to negative value.
bCondition = -10;
if (bCondition) rtOut->Text += "bCondition = -10;\n";

// Set to zero.
bCondition = 0;
if (bCondition) rtOut->Text += "bCondition != 0;\n";
else  rtOut->Text += "bCondition = 0;\n";

Output:

bCondition = 10;
bCondition = -10;
bCondition = 0;

A character variable can act as a condition expression for an if statement.

// Declare character variable.
char cBol;

// Non-zero value.
cBol = 'A';
if (cBol) rtOut->Text += "cBol = 'A';\n";

// Null or zero value.
cBol = '\0';
if (cBol) rtOut->Text += "cBol != '\\0';\n";
else rtOut->Text += "cBol = '\\0';\n";

Output:

cBol = 'A';
cBol = '\0';


As well for floating-point variables.

// Declare a floating point variable.
float fBol;

fBol = 123.456;
if (fBol) rtOut->Text += "fBol = 123.456;\n";

fBol = 0;
if (fBol) rtOut->Text += "fBol != 0;\n";
else rtOut->Text += "fBol = 0;\n";

Output:

fBol = 123.456;
fBol = 0;


No comments:

Post a Comment