0% found this document useful (0 votes)
99 views

03 - Selection Statements (If, Switch)

This document summarizes selection statements in C++ including if, if/else, else if, nested if, and switch statements. It provides examples of each statement type and how they are used for decision making based on conditional expressions. Key points covered include the basic syntax and structure of each statement, using blocks for grouping statements, and nesting statements for multi-level decisions. Examples provided demonstrate how to read input values and print outputs based on conditional logic.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

03 - Selection Statements (If, Switch)

This document summarizes selection statements in C++ including if, if/else, else if, nested if, and switch statements. It provides examples of each statement type and how they are used for decision making based on conditional expressions. Key points covered include the basic syntax and structure of each statement, using blocks for grouping statements, and nesting statements for multi-level decisions. Examples provided demonstrate how to read input values and print outputs based on conditional logic.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

LECTURE 6

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.

2. The Single If Statement Structure:


The IF statement is used to express conditional expression. If the given
condition is true then it will execute the statements; otherwise it will execute
the optional statements.

General Form of single-selection If statement:


if ( expression or condition ) statement1 ;

condition statement1

Example 1: if ( avrg >= 3.5 )


cout << “good”;

Example 2: if ( x > 0.0 )


sum += x;

Example 3: cin >> num;


if ( num == 0 )
zcount = zcount + 1;
Example 1
Write a C++ program to read any two numbers and
print the largest value of it:
#include<iostream.h>
void main( )
{
Float x,y;
Cout<<”Enter any two numbers\n”;
Cin>>x>>y;
If (x>y)
Cout << “largest value is”<<x<<endl;
}

3. The Single Block If Statement Structure :


The block IF statement are enclosed in ({) and (}) to group declaration and
statements into a compound statement or a block. These blocks are always
considered as a single statement. The structure is:

General Form of single block selection If statement:


if ( expression or condition )
{
statement1 ;
statement2 ;
statement3 ;
}

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}

4. The If/else Statement Structure:


The IF structure is

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 1: cin >> value;


if ( value >= 0 )
cout << “positive”;
else
cout << “negative”;
Example 2: cin >> num1 >> num2;
if ( num1 > num2 )
cout << num1;
else
cout << num2;

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:

General Form of else if statement:

if ( expression or condition 1 ) statement1 ;


else if ( expression or condition 2 ) statement2 ;
else if ( expression or condition 3 ) statement3 ;
:
else if ( expression or condition n ) statement-n ;
else statement-e ;

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:

Z={ x+5 :x<0


cos(x) + 4 : x = 0
√x :x>0
#include<iostream.h>
void main( )
{
int Z, x;
cout << "Enter X value \n";
cin >> x;
if ( x < 0 ) Z= x + 5;
else if ( x == 0 ) Z= cos(x) + 4;
else Z= sqrt(x);
cout << "Z is " << Z;
}

6. Nested If Statements:
Some of the samples of NESTED if-else constructions are shown below:

If (exp.) { Statements } If (exp.) { If (exp.) {


Else { Statements} If (exp.) {Statements} If (exp.) {Statements}
Else { Statements} } Else { Statements} }
Else {Statements} Else {If (exp)
{Statements}
Else {Statement}
}
Example 7
Write C++ program to find a largest value among three numbers:
#include<iostream.h>
void main( )
{
#include<iostream.h>
void main( )
{
Float x,y,z;
Cout<<”Enter any two numbers\n”;
Cin>>x>>y,z;
If (x>y) {
If (x>z)
Cout << “largest value is”<<x<<endl;
Else
Cout << “largest value is”<<z<<endl;
}
Else If (y>z)
Cout << “largest value is”<<y<<endl;
Else
Cout << “largest value is”<<z<<endl;
}
LECTURE 7

1. The Switch Selection Statement (Selector):


The switch statement is a special multi way decision maker that tests
whether an expression matches one of the number of constant values,
and braces accordingly.

General Form of Switch Selection statement:

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 1: switch (value)


{
case 0: cout << “grade is A”;
break;
case 1: coucout << “grade is B”;
break;
case 2: coucout << “grade is C”;
break;
default: cout << “grade is X”;
break;
}
Example 1
Write C++ program to read integer number, and print the name of
the day in a week:
#include<iostream.h>
void main( )
{
int day;
cout << “Enter the number of the day \n”;
cin >> day;
switch (day)
{
case 1: cout << “Sunday”; break;
case 2: cout << “Monday”; break;
case 3: cout << “Tuesday”; break;
case 4: cout << “Wednesday”; break;
case 5: cout << “Thursday”; break;
case 6: cout << “Friday”; break;
case 7: cout << “Saturday”; break;
default: cout << “Invalid day number”; 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;

cout << “+ for addition \n”;


cout << “- for subtraction \n”;
cout << “* for multiplication \n”;
cout << “/ for division \n”;
cout << “enter your choice \n”;
cin >> x;

switch ( x )
{
case ‘+’: cout << a + b;
break;
case ‘-’: cout << a - b;
break;
case ‘*’: cout << a * b;
break;
case ‘/’: cout << a / b;
break;
default: break;
}
}

2. Nested Switch Selection Statement:


General Form of Nested Switch Selection statement:
switch ( selector1 )
{
case label1 : statement1 ; break;
case label2 : statement2 ; break;
case label3 : switch ( selector2 )
{
case label1 : statement1 ; break;
case label2 : statement2 ; break;
:
}
case label-n : statement-n ; break;
default : statement-e ; 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:

General Form of Conditional statement:

( condition ? True : False )

Example 1: cin >> value;


cout << (value >= 0 ? “positive” : “negative” );

Example 2: cin >> x >> y;


cout << ( x < y ? -1 : (x == y ? 0 : 1) );

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

Q4: Write C++ program to represent a simple nested case (selector).

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.

You might also like