Table of Contents
Back Button
Back to Chapter
C++ Setting Up
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.

C++ Selection Statements

Selection statements are used to decide the course of a program. Selection statements basically execute some lines of code if one condition is met and a different set of lines if the condition is not met.

To understand selection statements better consider a problem statement

Write code for a gaming competition where if you score 900 to 1000 points (1000 being the maximum) you win Rs.5000 and anything lesser than 900 points earns you nothing.

The condition to earn Rs.5000 in this example is points greater than 900.

Lets see how we can write this in C++ using selection statements.

C++ provides us with 2 ways in which we can select which lines of code are to be executed based on the condition.

if, else if and else statements

In the above example, we have the condition:-points greater than 900 if this is true then earn 5000 else you earn nothing.

Syntax of if/else statements:

if(condition is true)// { //execute these lines of code } else { //execute these lines of code }

Syntax of if/else statements:

#include using namespace std; main() { int Prize=5000;int score; cout<<"How much did you score:"; cin>>score; if(score>=900) { cout<<"you've won Rs."<
Output 1: How much did you score:900 you've won Rs.5000 Output 2: How much did you score:700 not eligible better luck next time!

But only having one Rs.5000 prize is not interesting, let's add some more conditions if your score is less than 900 but greater than 800 you win a prize of Rs.2000 and scoring less than 800 but greater than 700 will earn you Rs.1000. Any score lesser than 700 does not win anything.

Syntax

if(condition1 is true) { ..... } else if(condition2 is true) { ...... } else if(condition3 is true) { ...... } else{ ..... }

Now, write a program for the same.

#include using namespace std; main() { int Prize=5000;int score; cout<<"enter your score:"; cin>>score; if(score>900) { cout<<"you win Rs.5000"<=800) { cout<<"you win Rs.2000"; } else if(score>=700) { cout<<"you win Rs.1000"<

Let us try to write a code for the above conditions.

Output 1 : enter your score:700 you win Rs.1000 Output 2 : enter your score:880 you win Rs.2000

Nested if/else

Consider example problem

Airplane tickets If you have a membership then, adult ticket is Rs.70,000, children under 14 years ticket are Rs.40,000 if you do not possess the membership then the cost for 1 adult ticket is Rs.85000 and for children, it is Rs.60,000

Write a program to ask the user if he has the membership or not and based on the ticket type display the bill.

#include using namespace std; main() { int membership=0; int age; cout<<"Do you have a membership: 1. yes 2. no "; cin>>membership; cout<<"enter age:"; cin>>age; if(membership==1) { if(age>14) cout<<"have a safe journey"<14) cout<<"have a safe journey"<
Output 1: Do you have a membership: 1. yes 2. no 1 enter age:14 have a safe journey ticket fare is Rs.40000 Output 2: Do you have a membership: 1. yes 2. no 2 enter age:14 have a safe journey ticket fare is Rs.60000

Using if-else statements, again and again, is annoying so C++ offers another alternative Switch statements.

Switch

The s***witch*** is used when we have more than 2 paths in which the program can be executed. It usually contains multiple switch cases and a default case which is executed only if neither of the above case condition is met. The variable in the bracket is compared with the test conditions and if the condition is met then the statement is executed. Each switch statement ends with a |break|. This is done to prevent statements following from being executed.

Syntax

switch(Character)//character decides which statement is to be executed { case 1:......;break;// if character=1 execute this statement case 2:......;break; case 3:.......;break; default:......; }

Lets create a simple shooting game using switch statements.

#include using namespace std; main() { int ch; cout<<"enter your choice 1.shoot 2.crouch 3.jump 4.punch"; cin>>ch; switch(ch) { case 1: cout<<"you killed the enemy";break; case 2: cout<<"you dodged a bullet";break; case 3:cout<<"you were shot by the enemy:";break; case 4:cout<<"you got knocked out.";break; default:cout<<"play loser";break; } }
Output: enter your choice 1.shoot 2.crouch 3.jump 4.punch:10 play loser enter your choice 1.shoot 2.crouch 3.jump 4.punch:2 you dodged a bullet

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?