If statement in c++



If statement


The if section structure is used to select or determine a course of action that depends on the values of program variables. A very simple example of the need for such a structure would be a program to find the roots of quadratic equation based on the values of coefficients of the equation. Depending on the value of a, b, and c in the quadratic equation there are either 0, 1,0r 2 roots of the equation. The procedure followed is to evaluate the discriminate and based on its value we would assign 0, 1, or 2 to the variable indicating the number of roots of the equation.
The simple if statement is used when a course of action consists of (a) executing a statement or block of statements, or (b) NOT executing the statement or block of statements.

The syntax of If statement is:

If (logical expression) statement 1;
next_statement

If the logical expression is TRUE, statement is executed and then next_statement is executed. If the logical expression is FALSE, statement 1 is ignored and next_statement is executed.
Similarly, a block of statements following:

If (logical expression)
{
statement a;
statement b;
statement c;
}
next_ statement 

If logical expression is TRUE, the block of statements inside the braces are evaluated and then next_ statement is executed. If logical expression is FALSE, the block of statements is ignored and the program proceeds to execute next_ statement.
Example 1: 
In this program is the sequence of numbers is tracked and the smallest and largest values are stored as it goes through the sequence. There are 2 ways of doing it as shown bellow.

If (thisValue>maxValue) maxValue=thisValue;
If (thisValue>minValue) minValue=thisValue;
Example 2:
In this Example 2, the minimum value is not only stored, it is also printed as the sequence is traversed, and a block of statements, enclosed in braces, is necessary.

If (thisValue<minValue)
{
minValue=thisValue;
cout<<”The minimum value so far is:”<<minValue<<endl;
}




Comments

Popular posts from this blog

Rumors of women’s hair cutting in India

President of India

Java interview question answer