Kamis, 02 Juli 2015

Conditional Execution in C++

Nama: Putri wanda ottafia(15211673)

Kelas : 4EA11

Conditional Execution in C++

The if Statement

Quite often we need to control the flow of execution of a program; in particular, to decide if a certain instruction or block of instructions should be executed, depending on a given condition. For example, in a more advanced version of our invoice program, we may want to take into account the fact that some products are taxable and some are not. The program could ask the user whether the product is taxable or not, and depending on what the user indicates, calculate things accordingly.
The most fundamental statement related to these types of situations is the if statement. The syntax for the if statement is as follows:
if (condition)
{
    statement(s);
}
A simple concrete example is shown below:
if (number < 0)
{
    cout << "Error! You must enter a positive number!" << endl;
}
This works as follows: the value of the variable number is retrieved, and compared against 0. If it is less than 0, then thecout statement (plus any other statements enclosed between the curly braces) is executed, and then execution continues at the statement following the closing curly brace (not shown in the example above, but presumably there are additional statements). If it is not less than 0, then the cout statement is skipped, and execution continues at the statement following the closing curly brace.
The round brackets are not optional. The curly braces are optional only if it is a single statement what executes conditionally. If more than one statement is associated to the if, then you must enclose the statements between curly braces.
In the case of a single statement, you may still enclose the statement in curly braces; this is a matter of style, but it is my opinion and my advice that one should always and unconditionally use curly braces to enclose the statement or statements associated to an if statement.
The reason is quite simple: if you do not use them, and in the future need to add an additional statement (it could even be acout statement for debugging purposes, in a situation where you suspect that there may be some problem around the ifor the condition), you could easily end up making the following mistake:
Original code:
if (number < 0)
    cout << "Error! You must enter a positive number!" << endl;

And you decide that your program should fix the condition and warn the user, instead of refusing to do anything and print an error message:
if (number < 0)
    cout << "Negative number entered -- adjusting sign" << endl;
    number = -number;
Even though we may be misled by the indentation (the nice alignment to the right for the whole block presumably associated to the if), the compiler is notremember that the compiler ignores extra spaces and newlines between tokens or statements; and because there are no curly braces, the rule says that only the statement immediately following the if and the condition is the one executed conditionally. The statement number = -number; is not part of the if, and is simply the next statement to be executedunconditionally!
The above can not happen if we always use curly braces, even when it is a single statement.
Important: the if statement must NOT have a semicolon at the end of the line (i.e., after the closing bracket for the condition). This is one of the common mistakes that can give you a really hard time before identifying it and fixing it!
This is how it works. If you code the following:
if (number < 0);
{
    cout << "Negative number entered -- adjusting sign" << endl;
    number = -number;
}
The compiler sees that there is a statement after the closing bracket for the condition. It is a special case of statement: anull statement; one that has no effect. But it is a statement that finishes at the semicolon. Therefore, the entire block that follows is simply a normal block of code that executes after the if statementit executes unconditionally and independently of the if statement.

Conditions

Conditions are expressions that evaluate to a boolean valuea true or false value (true and false are C++ keywords, representing the two possible values of a boolean expression or variable). Simple conditions involve two operands, each of which can be a variable or a literal value, and an operator, typically a comparison operator.