The if Statement in C++
By: Priya
Normally, your program flows along line by line in the order in which it appears in your source code. The if statement enables you to test for a condition (such as whether two variables are equal) and branch to different parts of your code, depending on the result.
The simplest form of an if statement is this:
if (expression) statement;
The expression in the parentheses can be any expression at all, but it usually contains one of the relational expressions. If the expression has the value 0, it is considered false, and the statement is skipped. If it has any nonzero value, it is considered true, and the statement is executed. Consider the following example:
if (bigNumber > smallNumber) bigNumber = smallNumber;
This code compares bigNumber and smallNumber. If bigNumber is larger, the second line sets its value to the value of smallNumber.
Because a block of statements surrounded by braces is exactly equivalent to a single statement, the following type of branch can be quite large and powerful:
if (expression) { statement1; statement2; statement3; }
Here's a simple example of this usage:
if (bigNumber > smallNumber) { bigNumber = smallNumber; cout << "bigNumber: " << bigNumber << "\n"; cout << "smallNumber: " << smallNumber << "\n"; }
This time, if bigNumber is larger than smallNumber, not only is it set to the value of smallNumber, but an informational message is printed. Listing below shows a more detailed example of branching based on relational operators.
A demonstration of branching based on relational operators.
1: // Listing 4.4 - demonstrates if statement 2: // used with relational operators 3: #include <iostream.h> 4: int main() 5: { 6: int RedSoxScore, YankeesScore; 7: cout << "Enter the score for the Red Sox: "; 8: cin >> RedSoxScore; 9: 10: cout << "\nEnter the score for the Yankees: "; 11: cin >> YankeesScore; 12: 13: cout << "\n"; 14: 15: if (RedSoxScore > YankeesScore) 16: cout << "Go Sox!\n"; 17: 18: if (RedSoxScore < YankeesScore) 19: { 20: cout << "Go Yankees!\n"; 21: cout << "Happy days in New York!\n"; 22: } 23: 24: if (RedSoxScore == YankeesScore) 25: { 26: cout << "A tie? Naah, can't be.\n"; 27: cout << "Give me the real score for the Yanks: "; 28: cin >> YankeesScore; 29: 30: if (RedSoxScore > YankeesScore) 31: cout << "Knew it! Go Sox!"; 32: 33: if (YankeesScore > RedSoxScore) 34: cout << "Knew it! Go Yanks!"; 35: 36: if (YankeesScore == RedSoxScore) 37: cout << "Wow, it really was a tie!"; 38: } 39: 40: cout << "\nThanks for telling me.\n"; 41: return 0; 42: } Output: Enter the score for the Red Sox: 10 Enter the score for the Yankees: 10 A tie? Naah, can't be Give me the real score for the Yanks: 8 Knew it! Go Sox! Thanks for telling me.
Analysis: This program asks for
user input of scores for two baseball teams, which are stored in integer
variables. The variables are compared in the if statement on lines 15,
18, and 24.
If one score is higher than the other, an informational message is printed. If
the scores are equal, the block of code that begins on line 24 and ends on line
38 is entered. The second score is requested again, and then the scores are
compared again.
Note that if the initial Yankees score was higher than the Red Sox score, the if statement on line 15 would evaluate as FALSE, and line 16 would not be invoked. The test on line 18 would evaluate as true, and the statements on lines 20 and 21 would be invoked. Then the if statement on line 24 would be tested, and this would be false (if line 18 was true). Thus, the program would skip the entire block, falling through to line 39.
In this example, getting a true result in one if statement does not stop other if statements from being tested.
Archived Comments
1. it is very good website for IT students
View Tutorial By: jeecee at 2013-03-21 12:23:46
2. cool.
View Tutorial By: Fahad Palomo at 2011-08-03 06:19:56
3. # include <iostream.h>
int main ()
{
int x,y
cout<<"ent
View Tutorial By: nazarine at 2011-07-06 06:17:10
4. #include<iostream>
#include<conio.h>
using namespace std;
v
View Tutorial By: Zohaib Farooq at 2011-05-23 12:47:54
5. #include<iostream>
#include<conio.h>
using namespace std;
v
View Tutorial By: Zohaib Farooq at 2011-05-23 07:58:44
6. #include<iostream>
#include<conio.h>
using namespace std;
v
View Tutorial By: Zohaib Farooq at 2011-05-23 07:57:56
7. #include<iostream>
#include<conio.h>
using namespace std;
m
View Tutorial By: XavioR at 2011-01-31 00:53:11
8. #include<iostream>
#include<conio.h>
using namespace std;
m
View Tutorial By: XavioR at 2011-01-31 00:52:48
9. Can we use the if sttmt l;ike this::
if (a==b==c==d==e)
i.e., multiple equality opera
View Tutorial By: Anubhav at 2010-11-14 08:15:04
10. elow guys...what a brilliant explanation about the if statement in c++...but i'm sorry to tell you
View Tutorial By: gelo abuel at 2009-08-03 08:21:44
11. elow guys...what a brilliant explanation about the if statement in c++...but i'm sorry to tell you
View Tutorial By: gelo abuel at 2009-08-03 08:21:27
12. w0w amazing
View Tutorial By: elvin at 2009-01-05 01:00:13
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++