0% found this document useful (0 votes)
10 views24 pages

05 Selection

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

05 Selection

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Selections

Department of Computer
Science
Exercise 5.1
• Write a C++ program to read an examination mark
from keyboard and print “pass” if mark >= 40.
C++ Selection (if Statement)
• One-Way (if) Selection

Syntax
if (expression)
{
statement(s)
}

• Statement executed if value of expression true


C++ Selection (if Statement)
• Two-Way (if...else)
if (expression)
{
statement1;
}
else
{
statement2;
}
• If expression true, statement1 executed, otherwise
statement2 executed
if-then-else Statement
• Syntax
if (Condition) {
statement(s)
}
else {
statement(s)
}

• Example
Exercise 5.1
Write a C++ program
to print the biggest
number of the two
given numbers
Exercise 5.2
Write a C++ program
to print the biggest
number of the three
given numbers
Exercise 5.3
• Write a C++ program to print the grade for a given
mark.

0 40 60 75 100
Solution 1
Nested if and if...else Statements
• Nesting: one control statement in another
• Syntax of nested if...else statements:

if (condition1)
statement1;
else if (condition2)
statement2;
...
else if (condition-n)
statement-n;
else statement-z;
Exercise 5.4
Write a C++ program
to print the grade for a
given marks
Debug
Debugging is a methodical process of finding and
reducing the number of bugs, or defects
Debug
• Programming errors are called bugs
• Programming errors may be
– Compiler errors
– Runtime errors
– Logical errors
• Going through the code, examining it and looking for
something wrong in the implementation (bugs) and
correcting them is called debugging
Debugging Options
• Make break points
– Right click on statement and add break point

• Add watches to see the values of the variables


– Click Debug – Debugging window - Watches
Debugging cont.…
• Start debugging (Press F8)

• Debug Commands
– F8 – Start / Continue
– F7 – Next Line
– Shift+F8 Stop
Exercise 5.5
• Write a C++ program that reads month as an integer
and print the name of the month
• Draw a flowchart to describe your solution
• Debug your code and find execution path for the
following
– Month = 1;
– Month = 12;
– Month = 6;
Switch
Run with number of possible execution paths
A switch works with the byte, short, char, and
int
primitive data types
Switch
• switch structure: alternate to if...else

• Example 1:
switch(x) {
case x1: statements1;
break;

case x2: statements2;


break;

default: statements4;
break;
}
Exercise 5.6
• Write a C++ program that reads month as an integer
and print the name of a month.
(Use Switch)
Exercise 5.7
• Draw a flowchart to above program
Exercise 5.7
• Write a C++ program that reads day as an integer
(1-7)and print the name
– Use only if-else statement and implement your
solution
– Use switch statement and implement your
solution
• Draw flowcharts to above two programs
• Compare performance of the two methods (if and
Switch)
– Use debug and find the execution path
Exercise 5.8
following formats.

• Write a C++ program to display a given date as the

Your program should read date as the three inputs


(day, month and year) and generates the output
forms.
– Option 1: 21.05.2001
– Option 2: 21.05.01
– Option 3: 21 st May 2001
Solution
1. Print a day
2. Print (st/nd/rd/th)
3. Print month
4. Print year

Budditha Hettige ([email protected])


Exercise 5.9
• Write a C++ program to print the bill for an item
bought by a customer from a shop.
– The program should ask unit price and quantity of
an item and calculate the total cost
– If item quantity greater than 10 give one item free
– Add 3.5 % discount for the total if total cost grater
than 2500.
– The bill should contain all the above information
and amount of money tendered and the correct
amount of change.

You might also like