Saturday, June 12, 2021 20:48

Table of contents >> Introduction > Testing a condition with If

Testing a condition with If

As previously stated in our lessons, as your programs will become more and more complex, they will perform a set of instructions when a condition is True, and another one when is False. When your program executes a conditional processing, you will be testing a condition with If. The template of this instruction is:

The condition to be evaluated by the If instruction must be between parenthesis, and must produce an either True or False result. Whenever the condition will evaluate as True, the program will execute the instruction that follows after the If statement. When the condition will evaluate as False, the program will ignore the instruction that follows.

The following example tests if a variable named age is greater than or equal to 32. If the condition is True, the program will execute the Console.WriteLine() instruction. If the condition will evaluate as False, the program will ignore the instruction that follows, and will continue the execution starting with the first instruction that follows after Console.WriteLine():

There are two kinds of instructions, when using an If conditional statement: simple and composed. A simple instruction is a single instruction, like the following:

On the other side, a composed instruction means two or more instructions following after the condition, like in the following example:

You can notice that we are grouping the instructions in a block, having curly brackets as delimiters. You should NEVER forget placing curly brackets when using more than one instruction after a conditional check. Consider the following example:

As explained in a previous lesson, whenever the condition is evaluated as False, it will ignore the expression that follows, and resume the execution starting from the first instruction that follows. If you omit the curly brackets, whenever the above code will have the condition return False, the program will ignore instruction1, BUT WILL RESUME the execution starting from instruction2! If you want the program to ignore a set of instructions when the condition is False, group the instructions in a block of code by using curly brackets.

Some programmers consider that it is a good practice to place curly brackets even around simple instructions, like in the following example:

However, other programmers (including me 😉 ) consider that as a waste of lines, which makes a pretty important difference in programs with a huge number of lines and/or conditional statements. Since both ways are valid, you can chose whichever you want. However, I would advice beginners to use curly brackets in all places, at least until they are comfortable enough with programming and using conditional statements right.

The concepts explained in this lesson are also shown visually as part of the following video:

 

EXERCISES
1. Write an if statement that takes two integer variables and exchanges their values if the first one is greater than the second one.

Solution


Guidelines: Read about if-statements.

Tags: ,

Leave a Reply



Follow the white rabbit