If Statements

Conditionals are a branching type of control structure, meaning they dictate the course of an application based on certain parameters. Of the branching control structures, the if conditional is the most common. Its syntax is straightforward:

if (condition) {
   statements;
}

The condition is placed within parentheses, followed by—within curly braces—the statement or statements to be executed. If the condition is true, the statements will be executed. If it is false, the statements will not be run.

In C, the simplest representation of false is the number 0 and true is everything else:

if (1) {
   printf ("This conditional is always
 true ");
   printf ("This is another
 statement.");
}

Any quantity or type of statements can be executed as the result of the conditional. If your conditional uses only one statement, you can get away without using the curly braces. For example:

if (1) printf ("This conditional is
 always true.");

To get you started programming conditionals, the following example demonstrates how the most basic condition is treated in C.

To create an if conditional

1.
Create a new, blank C project in your text editor or IDE.

2.
Add the initial comments and code (Script 4.1):

/* if.c - Script 4.1 */
#include <stdio.h>
int main (void) {

Script 4.1. This basic conditional checks if the test variable has a value other than 0.


3.
Declare and initialize an integer variable:

int test = 1;

The test variable is a simple integer (you could also make it a short unsigned int, if you want to be a minimalist). It will be used as the condition in this application's control structure.

4.
Add a message to be printed:

printf ("Testing the test
 variable.
");

This message establishes the context for any forthcoming text. It also guarantees that something will occur when the application is run, regardless of the conditional's truth (if the condition is false, no other text will be printed).

5.
Define the if conditional:

if (test) {
     printf ("*** Value of test is
      %d.
", test);
}

If the condition is true, some text will be printed along with the value of test. This will be the case if test has a value other than 0. If the condition is not true (because test does have a value of 0), the printf() statement will never be executed.

6.
Complete the main() function:

   getchar();
   return 0;
}

7.
Save the project as if.c.

8.
Compile and debug, as necessary.

9.
Run the executable application (Figure 4.1).

Figure 4.1. If the test variable has a value other than 0, the condition is true and the variable's value is printed.


10.
For comparison, change the value of test to 0, recompile, and rerun the application (Figure 4.2).

Figure 4.2. If the test variable's value is 0, the condition is false and only the print statement before the conditional is executed.


In this case, the condition is false, because 0 (test's value) is false in C.

✓ Tips

  • Creating single-statement conditionals without curly braces, while perfectly legal, can make your code harder to follow and possibly lead to errors if you make future changes to that conditional. That being said, you may like the terseness of this format or encounter it in other people's work. In this book, you'll see almost every conditional using curly braces.

  • The C99 standard includes a new data type called _Bool, short for boolean. Variables of this type can have a value of either 0 (representing false) or 1 (true). In fact, assigning any value other than 0 to it results in a value of 1. Note that version 4 of Dev-C++ does not support _Bool but version 5 does.

  • Available in the C99 standard is the stdbool.h file, which defines three related keywords: bool, which is an alias for the _Bool data type; true, which is an alias for 1; and false, which is an alias for 0. The purpose of this file and these aliases is to help make your C code more portable to C++, which uses those terms.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset