Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Decision Making Statements

By Sripriya R | on September 1, 2007 |
C++ Tutorials

In this C++ tutorial, you will learn about decision making statements if statement, if..else statement, switch statement, conditional operator along with syntax and examples.

Decision-making is an important concept in any programming language and to accomplish this, C++ uses the following decision making statements:

  • if statement
  • if..else statement
  • switch statement
  • conditional operator

Explanation of decision-making statements in C++ programming language:

if statement:

  1. if (condition)
  2. {
  3.    statement 1;
  4.    statement 2;
  5.    ...
  6. }

If the condition returns true value then all the statements inside the braces of if block are executed. Otherwise, if the condition returns false, the statements from inside the block are ignored, and the rest of the code is executed. If the block contains only one statement, it can be written without the braces.

For Example:

The programmer desires to retrieve the input from the user, check the number value and wishes to print the output only if it is greater than 50. Thus, this can be performed as follows:

  1.  #include <iostream>
  2. using namespace std;
  3. void main()
  4. {
  5.    int a;
  6.    cout << "Input the number: "; 
  7.    cin >> a;
  8.    if (a>50)
  9.    cout << a;
  10. }

The output of the above program is

There is only one statement to execute if the if statement returns true value, therefore, the braces are not placed.

Suppose, in the above program, if the condition returns true value. The programmer wants to print the value of a first and then add 10 to a and print. Braces are then used for the if block as follows:

  1.  #include <iostream>
  2. using namespace std;
  3. void main()
  4. {
  5.    int a;
  6.    cout << "Input the number: "; 
  7.    cin >> a;
  8.    if (a>50)
  9.    { 
  10.       cout << a << endl;
  11.       a=a+10;
  12.       cout << a;
  13.    }
  14. }

The output of the above program is

if..else statement:

If has several levels of condition checking. When the if statement condition1 evaluates to false, then another condition in the false block must be placed by using an else if statement. If the condition1 evaluates to true, then all the other else if blocks are ignored.

If condition1 is false, then condition2 is checked, and in case of false, the next condition is checked and continued. If all statements are false when the programmer places an else statement, the statements in this block are executed. This is referred to as a nested if..else statement.

  1. if (condition1)
  2. {
  3.    statement 1;
  4.    statement 2;
  5.    ...
  6. }
  7. else if (condition2)
  8. {
  9.    statement 1;
  10.    statement 2;
  11.    ...
  12. }
  13.    ...
  14.    ...
  15. else
  16. {
  17.    statement 1;
  18.    statement 2;
  19.    ...
  20. }

for example:

  1.  #include <iostream>
  2. using namespace std; 
  3. void main() 
  4. {
  5.    int grade;
  6.    cout << "Input the Training Grade Level: ";
  7.    cin >> grade; 
  8.    if ( grade > 100 ) 
  9.    { 
  10.       cout << "Best Level "; 
  11.       cout << grade; 
  12.    }
  13.    else if ( grade == 100 ) 
  14.    { 
  15.       cout << "Good Level "; 
  16.       cout << grade; 
  17.    }
  18.    else 
  19.    {
  20.       cout << "Needs Improvement"; 
  21.    }
  22. }

The output of the above program is

Since the input was greater than 100 the, if block got executed.

Suppose the input is 90 the output would be because here the if and the else if condition both failed and therefore statement in else block got executed. The output of the above program is

}

switch statement:

If there are a number of decision making conditions instead of making an if..else construct, the programmer can use switch statement.

The general syntax is:

  1. switch (expression)
  2. {
  3.    case constant1:
  4.    group of statements 1;
  5.    break;
  6.    case constant2:
  7.    group of statements 2;
  8.    break;
  9.    .
  10.    .
  11.    .
  12.    default:
  13.    default group of statements
  14. }

In the above example, the expression is first evaluated and the value id checked with the constant1 in the case statement. If this is evaluated with the same value, then the group of statements is executed. When a break statement is encountered, the control is switched to the end of the switch statement. If the value of the expression is not equal to constant1, it is checked with constant2. If it evaluates the same value, then the group of statements (in the case of constant2) is executed. When a break statement is encountered, the control is then switched to the end of the switch statement. This proceeds until the value of expressions equal one of the constant values given. If the value of the expression is not equal to any of the constant values given, then, by default, the statements present are executed.

For example:

  1.  #include <iostream>
  2. using namespace std;
  3. void main()
  4. {
  5.    int x;
  6.    cout<<"Enter Input Value: ";
  7.    cin>>x;
  8.    switch(x)
  9.    {
  10.       case 10:
  11.       cout<< "Value is 10";
  12.       break;
  13.       case 20:
  14.       cout<<"Value is 20";
  15.       break;
  16.       default:
  17.       cout<<"Invalid Input";
  18.    }
  19. }
The output of the above program is

In the above example, the output produces:

  • Enter Input Value:10
  • Value is 10

Since the entered input is 10, which is equal to the first case value, the statement in the first case block is executed.

  • Enter Input Value:50
  • Invalid Input

Since the entered input is 50, which is not equal to any of the case values, the default statement is executed.

Suppose the programmer wants the same block of statements to be executed for a number of case values. This is performed by removing the break statement. If a break statement is removed, then the control inside the end of a switch statement will move to the next case statement and proceed until it finds a break statement.

Important care must be taken not to place variables in case values. Only constants can be placed and expressions of the switch statement can be compared only with constants in case statements.

conditional operator:

As we have seen before in the earlier chapter covering operators in C++, the conditional operator actually is a form of if…else statement.

  1. if(a < b)
  2.    m=a;
  3. else
  4.    m=b;

could be written using conditional operators as

  1. m=(a < b)? a:b;
« « Building Better Supply Chain
Which Supply Chain Management Software is Right for your Business? » »

Author Description

Avatar

Free Training

RSSSubscribe 391 Followers
  • Popular
  • Recent
  • C++ Static Functions

    February 12, 2007 - 0 Comment
  • C++ Standard Input Output Stream

    August 24, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Structure Part II

    August 19, 2007 - 0 Comment
  • C++ Abstraction

    April 26, 2007 - 0 Comment
  • C++ Operators Part I

    August 24, 2007 - 0 Comment
  • C++ Operator Overloading Part 1

    September 7, 2006 - 0 Comment
  • C++ Encapsulation

    August 16, 2007 - 0 Comment
  • C++ Operators Part II

    August 27, 2007 - 0 Comment
  • C++ Operator Overloading Part II

    September 9, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT