03 - Selection Statements (If, Switch)
03 - Selection Statements (If, Switch)
1. Selection Statements:
Conditional expressions are mainly used for decision making. C++ provides
multiple selection structures: if, if/else, else if, nested if and switch.
condition statement1
Example 2
Write a C++ program to read a number and check if it’s positive,
if it’s so print it, add it to a total, and decrement it by 2:
#include<iostream.h>
void main( )
{
int num, total=0;
cin >> num;
if ( num >= 0 )
{ cout << num <<” is a positive”;
total += num; num = num – 2;
} }
General Form of If/else statement:
if ( expression) if ( expression)
statement1 ; {statements }
else statement2 ; else {statements}
true condition
false
Statement1 Statement2
In this case, either of the two statements are executed depending upon the
value of the expression. Note that there is a semicolon after each of the
statement but not after the IF expression. Note that the else statement without
braces leads to confusion so:
If (i>j) { If (a>b)
temp=a;
}
Else
temp=b;
Example 3
Write a C++ program to read a student degree, and check if it’s
degree greater than or equal to 50, then print pass, otherwise print fail:
#include<iostream.h>
void main( )
{
int degree;
cin >> degree;
if (degree >= 50 )
cout << ”pass”;
else
cout << “fail”;
}
Example 4
Write a C++ program to read a number, and check if it’s even or
odd:
#include<iostream.h>
void main( )
{
int num;
cin >> num;
if ( num % 2 == 0 )
cout << ”even”;
else
cout << “odd”;
}
5. Else if Statements:
Example 1:
if ( value == 0 ) cout << “grade is A”;
else if ( value == 1 ) cout << “grade is B”;
else if ( value == 2 ) cout << “grade is C”;
else cout << “grade is X”;
Example 5
Write a C++ program to read a number, and print the day of the
week:
#include<iostream.h>
void main( )
{
int day;
cin >> day;
if ( day == 1 ) cout << “Sunday”;
else if (day == 2 ) cout << “Monday”;
else if (day == 3 ) cout << “Tuesday”;
else if (day == 4 ) cout << “Wednesday”;
else if (day == 5 ) cout << “Thursday”;
else if (day == 6 ) cout << “Friday”;
else if (day == 7 ) cout << “Saturday”;
else cout << “Invalid day number”;
}
Example 6
Write C++ program to compute the value of Z according to the
following equations:
6. Nested If Statements:
Some of the samples of NESTED if-else constructions are shown below:
switch ( selector )
{
case label1 : statement1 ; break;
case label2 : statement2 ; break;
case label3 : statement3 ; break;
:
case label-n : statement-n ; break;
default : statement-e ; break;
}
Example 2
Write C++ program to read two integer numbers, and read the
operation to perform on these numbers:
#include<iostream.h>
void main( )
{
int a, b;
char x;
cout << “Enter two numbers \n”;
cin >> a >> b;
switch ( x )
{
case ‘+’: cout << a + b;
break;
case ‘-’: cout << a - b;
break;
case ‘*’: cout << a * b;
break;
case ‘/’: cout << a / b;
break;
default: break;
}
}
Example 3
Write C++ program to read integer number, and print the name of
the computerized department:
#include<iostream.h>
void main( )
{
int i,j;
cout << “Enter the number for the department name \n”;
cin >> i>>j;
switch (i)
{
case 1: cout << “Software Engineering Department”; break;
case 2: cout << “Control and computers Department”; break;
case 3: cout << “Computer Sciences Department”;
cout<<”Enter the no. of branch”;
switch(j)
{
case 1: cout << “Software”; break;
case 2: cout << “Information system”; break;
case 3: cout << “Security”;
case 4: cout << “AI”;
}
default: cout << “Invalid day number”; break;
}
}
3. Conditional Statement:
Example 4
Write C++ program to read integer number, and print if its even or
odd:
#include<iostream.h>
void main( )
{
int value;
cout << “Enter the number \n”;
cin >> value;
cout<<(value%2==0?”even”:”odd”);
}
WORK SHEET (3)
Selection Statements
Q1: Write C++ program to read two integer numbers then print “multiple”
or “not” if one number is a multiple to another number.
Q2: Write C++ program to read integer number and print the equivalent
string.
e.g:
0 Zero
1 One
2 Two
:
Q3: Write C++ program to read a score of student and print the estimation
to refer it.
e.g:
100 - 90 Exultant
89 - 80 Very good
79 - 70 Good
69 - 60 Middle
59 - 50 Accept
49 - 0 Fail
Q5: Write C++ program to compute the area of circle if the radius r=2.5.
Note: area of circle is r * r * pi ,
pi is 3.14
Q6: Write C++ program to read an integer number and check if it is positive
or negative, even or odd, and write a suitable messages in each case.
Q7: Write a program to read 3 numbers, and write the largest and smallest
numbers.
Q8: Write C++ program to read an integer from 1 to 12, and print out the
value of the corresponding month of the year.
Q9: Write C++ program to reads a character and print if it is digit (0..9),
capital letter (A,B, … ,Z), small letter (a, b, … ,z), special character ( +, !,
@, #, , {, >, … ).
Q10: Write C++ program to read x and compute the following:
Q11: Write C++ program to read 5 numbers and determine if the numbers
sorted ascending or not.
Q12: Write C++ program to read two integer numbers, and read the
operation to perform on these numbers.
Q13: Write a program to read X and print Sin X if X>0, square root X f X<0
and absolute X if X/2 is integer.