<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 5 Conditional Processing, Part 1 - If-Else Statements and Relational Operators_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 5: Conditional Processing, Part 1
If/Else Statements and Relational Operators
 More of this Feature
• Conditional Processing, Part 1, Page 2
• Summary of Relational Operators
 Related Resources
• Use of Braces in C/C++
• All C++ Tutorials
• Tutorials on Compiling
 
 From Other Guides
• Unix Filesystems and Links
• Top Five Unix Flavors for Home Computers

by John Kopp

Welcome to About.com's C++ tutorial. This lesson introduces conditional processing. In previous tutorials, all the code in the examples executed, that is, from the first line of the program to the last, every statement was executed in the order it appeared in the source code. This may be correct for some programs, but others need a way to choose which statements will be executed or run. Conditional processing extends the usefulness of programs by allowing the use of simple logic or tests to determine which blocks of code are executed. In this lesson, a simple guessing game will be developed to illustrate the use of conditional execution.

The if statement is used to conditionally execute a block of code based on whether a test condition is true. If the condition is true the block of code is executed, otherwise it is skipped.

#include <iostream>
using namespace std;

int main()
{

    int number = 5;
    int guess;

    cout << "I am thinking of a number between 1 and 10" << endl;
    cout << "Enter your guess, please " << endl;
    cin >> guess;
    if (guess == number)
    {
        cout << "Incredible, you are correct" << endl;
    }

    return 0;
}

Please try compiling and executing the above. The "==" is called a relational operator. Relational operators, ==, !=, >, >=, <, and <=, are used to compare two operands. The program works, but it needs some improvements. If the user enters 5 as a choice, he gets back a nice message, "Incredible, you are correct". But what happens if the user puts in an incorrect choice? Nothing. No message, no suggestions, nothing. Luckily, for our program user, C++ has a solution.

The else statement provides a way to execute one block of code if a condition is true, another if it is false.

#include <iostream>
using namespace std;

int main()
{

    int number = 5;
    int guess;

    cout << "I am thinking of a number between 1 and 10" << endl;
    cout << "Enter your guess, please ";
    cin >> guess;
    if (guess == number)
    {
        cout << "Incredible, you are correct" << endl;
    }
   else
    {
        cout << "Sorry, try again" << endl;
    }

    return 0;
}

Next page > Page 2 of Tutorial > Page 1, 2, 3, 4




Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.