Coding & programming
Installing CodeBlocs compiler
C language
C++ language
© The scientific sentence. 1998-2016
| Â |
|
Programming with C/C++ Language
Introduction to the C++ Language
Conditional if statements
1. conditional if statement
The if statement is a conditional statement, that allows us to control
a situation. That is to select an action based upon a given condition is true
or false.
A TRUE statement is one that evaluates to a nonzero number.
A FALSE statement evaluates to zero.
When you perform comparison with the relational operators, the operator will return
1 if the comparison is true, or 0 if the comparison is false.
Here are the relational operators, as they are known, along with examples:
>   greater than      Â
12 > - 4 is TRUE
≥   greater than or equal      Â
7 ≥ 7 is TRUE
<   less than      Â
- 3 < 2 is TRUE
≤   less than or equal      Â
3 ≤ 4 is TRUE
==   equal to      Â
8 == 8 is TRUE
!=   not equal to      Â
2 != - 6 is TRUE
2. If Statement Syntax
The structure of an if statement is as follows:
if ( TRUE )
Execute the next statement ..
Example:
int x ;
if ( x > 30 )
cout<< " The month is not February";
For more than one statement to execute after an if statement
that evaluates to true, use braces, like we did with the body of a function.
Anything inside braces is called a compound statement, or a block.
Example:
if ( TRUE )
Execute all statements inside the braces..
3. Else and Else if
The else statement gives an alternative. It is executed
when the if statement is FALSE.
It can look like this:
if ( TRUE ) {
// Execute these statements if TRUE
}
else {
// Execute these statements if FALSE
}
Another use of else is when there are multiple conditional
statements that may all evaluate to true.
We can use an else if statement following an if statement and its body;
that way, if the first statement is true, the "else if" will be ignored, but if
the if statement is false, it will then check the condition for the else if statement.
If the if statement was true the else statement will not be checked. It is possible
to use numerous else if statements to ensure that only one block of code is executed.
if ( <condition> ) {
// Execute these statements if <condition> is TRUE
}
else if ( <another condition > ) {
// Execute these statements if <another condition > is TRUE and
// <condition> is FALSE
}
Let's see an example:
#include <iostream>
using namespace std;
int main() // main function of the program
{
int nbr; // setting a variable ...
cout <<"Please guess a number between 1 and 10: "; // Asks for a number
cin>> nbr; // The input is put in nbr
cin.ignore(); // Throw away enter
if ( nbr < 10 ) { // If the nbr is less than 30
cout<<"You have followed the instructions!\n"; // Say somethings if it's true ..
}
else if ( nbr == 10 ) {
cout<<" The largest !\n";
}
else {
cout<<"You didn't follow the instructions!\n"; // Executed if no other statement is
}
cin.get(); //Keep the window from closing
}
Using CodeBlocs:
4. conditional statements and boolean operators
Boolean operators OR, AND, and NOT are also used in conditional
statements.
When using if statements, you will often need to check multiple different conditions.
The boolean operators function in a similar way to the comparison operators:
each returns 0 if evaluates to FALSE or 1 if it evaluates to TRUE.
• Note that the NOT operator accepts one input. If that input is TRUE,
it returns FALSE, and if that input is FALSE, it returns TRUE.
In C and C++ NOT is written as !. NOT is evaluated prior to both
AND and OR.
• AND returns TRUE if both inputs are TRUE.
The AND operator is evaluated before the OR operator.
• If either (or both) of the two values the OR operator checks are TRUE then
it returns TRUE. The OR is written as || in C++. Those are the pipe characters.
The OR will be evaluated after AND.
It is possible to combine several boolean operators in a single
statementinvolving complex expressions .
|
  |