| IF conditional instruction With this instruction we can check if a variable has a certain value.
General form:
* - Fixed value if (variable == value) * - If no value given if (variable! = value) * - If it has a value greater or equal at least: if (variable> = value) * - If it has a value less or equal at least: if (variable <= value)
This instruction may have a second part, which is optional. if (condition)
(
)
else
(
) Between the () "else" is executed only if the condition is false / not take place.
Examples # include
void main ()
(
int a;
cost << "Give value:";
CIN>> a;
if (a> = 0)
cost << "You entered a positive number";
else
cost << "You entered a negative number";
) There is another possibility: # include
void main ()
(
int a;
cost << "Give value:";
CIN>> a;
if (a> 0)
cost << "You entered a positive number";
else if (a <0)
cost << "You entered a negative number";
else
cost << "You entered the void;
) And the "else if" continued .... # include
void main ()
(
int a, b;
cost << "a =";
CIN>> a;
cost << "b =";
CIN>> b;
if (a> b)
cost < # include
void main ()
(
int a;
cost << "Enter a different value of 5;
CIN>> a;
if (a! = 5)
cost << "Correctly";
else
cost << "false";
) Everything is different from 0 is considered true if the condition, and that 0 is false.
Extra
* Operator & & - "si"
- General form if (conditie1 & conditie2) - The condition is considered true only if both conditions are true
- Example:
1. int a = 5, b = 3;
if (a == 5 & & b == 3) cost << "truth"; 2. int a = 5, b = 4;
if (a == 5 & & b == 3)
cost << "false"; So with this operator we can find out if a variable is located in a desirable: # include
void main ()
(
int a;
cost << "a =";
CIN>> a;
if (a> = 1 & & a <= 10)
cost < Or read from the keyboard: # include
void main ()
(
int a, x, y;
cost << "top range";
CIN>> x;
cost << "End of interval:";
CIN>> y;
cost << "a =";
CIN>> a;
if (a> = x & & a <= y)
cost < * The operator | | - or
- General form if (conditie1 | | conditie2) - The condition is considered true if at least one of the conditions is true
- Examples: int a = 5;
if (a == 5 | | a == 3) cost << "truth"; int a = 5;
if (a == 4 | | a == 3) cost << "false"; Examples: # include
void main ()
(
int a;
cost << "a =";
CIN>> a;
if (a> 0 | | a <0)
cost < # include
void main ()
(
int a;
cost << "a =";
CIN>> a;
if (a == 3 | | a <0)
cost < So much about IF.
|