Coupon Accepted Successfully!


The if Statement

Among the decision-making statements in C++, the if statement is the simplest one. C++ uses the keyword if to execute a set of command lines or a command line when the logical condition is true. It has only one option. The syntax for the simplest if statement is as shown in Figure.

The if statement

The if statement contains an expression. The expression is always enclosed within a pair of parentheses. The conditional statements should not be terminated with semicolons (;). The statements following the if statement are normally enclosed within curly braces. The curly braces indicate the scope of the if statement. The default scope is one statement. But it is a good practice to use curly braces even with a single statement.

The if keyword is followed by an expression in parentheses. The expression is evaluated. If the expression is true, it returns 1, otherwise 0. The value 1 or any non-zero value is considered as true and 0 as false. In C++, the values (1) true and (0) false are known bool data type. The bool data type occupies one byte in memory. If the given expression in the if statement is true, the following statement or block of statements are executed; otherwise, the statement that appears immediately after the if block (true block) is executed as given in Figure.

As shown in figure, the expression is always evaluated to true or false. When the expression is evaluated as true, the statements within the if block are executed and the program continues from the next statement. When the expression is false, the if block is skipped and statement 3 is executed.

The expression given in the if statement may not be always true or false. For example, if (1) or if (0). When such a statement is encountered, the compiler will displays a warning message “condition is always true” or “condition is always false”. Instead of the expression, we can also use the function that returns 0 or 1 return values. The following programs illustrates all the above discussed points.

Flowchart for if statement is as shown in Figure.


Example of a simple if statement.

5.1 Write a program to declare the price of the book with if statement and check its price. If its price is less than or equal to 600, print the output with some comment, else terminate.



Step 1: Start.

Step 2: Declare the price of a book with some variable.

Step 3: Check the condition with if statement i.e. price<=600.

Step 4: If ‘yes’ print the output with some comment, otherwise terminate.

Step 5: End.


Explanation: In the above program, the price of the book is entered through the keyboard. If entered price is less than or equal to 600, the message displayed is “Hurry up buy the book!!!!!”.

5.2 Write a program to check the equivalence of two numbers.


Explanation: The two numbers are entered. They are checked with if statement. If the difference of two numbers is zero, it prints the message “Two numbers are equal”.

5.3 Write a program to use library function strlen() and check for a string. If the string is non-zero, then display some message.


Explanation: In the above program, the character array nm[] is initialized with the string “Hello”. The strlen() function is used in the if statement. The strlen() function calculates the length of the string and returns it. If the string is empty, it returns (0) false: otherwise, it returns a non-zero value (string length). The non-zero value is considered as true. The strlen() function returns non-zero value (true). The if statement displays the message “The string is not empty.” Here, instead of an expression, a library function is used.

Test Your Skills Now!
Take a Quiz now
Reviewer Name