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

Programming 1 Week 3

The document discusses week 3 of a Programming 1 course in C#. It covers common mistakes from week 2, an assignment on converting seconds to hours/minutes/seconds, selection types like if/else statements and truth tables, exercises on finding highest/lowest numbers and calculating raises, and using switch statements for multiple selection.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Programming 1 Week 3

The document discusses week 3 of a Programming 1 course in C#. It covers common mistakes from week 2, an assignment on converting seconds to hours/minutes/seconds, selection types like if/else statements and truth tables, exercises on finding highest/lowest numbers and calculating raises, and using switch statements for multiple selection.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Programming 1 (C#)

Week 3
Gerwin van Dijken ([email protected])
1
Program term 1.1 (Programming 1)
01 (wk 36) Introduction C# / Visual Studio 2022 (Community)
02 (wk 37) Sequence
03 (wk 38) Selection
04 (wk 39) Iteration
05 (wk 40) Arrays
06 (wk 41) Methods
07 (wk-42) no classes
08 (wk 43) Repetition / practice exam
09 (wk-44) exam (practical, computer)
10 (wk-45) -

2 Programming 1 - Week 3 22/23


Common mistakes – Week 2 (1)
‒ Bad naming, do use useful names!
int jeans; int numberOfJeans;

‒ Using (made-up) abbreviations


double avg; double average;

‒ Empty functions (event handlers) inside code (windows forms) Remove them

‒ There are better ways to format strings then (ab)using the + operator
Console.Write(hours.ToString("00") + ":" + minutes.ToString("00") + ":" + seconds.ToString("00"));
It’s better to use:
Console.Write($"{hours:00}:{minutes:00}:{seconds:00}");
3 Programming 1 - Week 3 22/23
Common mistakes – Week 2 (2)
‒ Don’t perform mathematical operations while printing output!
Console.WriteLine($"VAT: {input * VatRate:0.00}”);
It’s better to use:
double vat = input * VatRate;
Console.WriteLine($"VAT: {vat:0.00}");

4 Programming 1 - Week 3 22/23


Sequence assignment 3
Enter a number of seconds. Show this value as hours, minutes and seconds.
(hh:mm:ss, as shown on a digital alarm clock)

5 Programming 1 - Week 3 22/23


Sequence assignment 3

read seconds 7265


hours = seconds / 3600 2
seconds = seconds – hours * 3600 65
integer
minutes = seconds / 60 1
division! seconds = seconds – minutes * 60 5
display hours, minutes, seconds "02:01:05"

read seconds
hours = seconds / 3600
modulo
(remainder)
seconds = seconds % 3600
minutes = seconds / 60
seconds = seconds % 60
display hours, minutes, seconds
6 Programming 1 - Week 3 22/23
Selection types
Selection → a decision structure

2-way (boolean) selection


→selection depends on a boolean-value
→if-then selection
→if-then-else selection
→nested selection

Multiple (value) selection


→selection depends on an integer/char/string-value
7 Programming 1 - Week 3 22/23
‘if-then selection’

if <condition>
<statement(s)>

8 Programming 1 - Week 3 22/23


Example ‘if-then selection’
PassLimit = 55
read grade
if grade < PassLimit
display "Failed"

9 Programming 1 - Week 3 22/23


‘if-then-else selection’
if <condition>
<statement(s)>
else
<statement(s)>

10 Programming 1 - Week 3 22/23


Example ‘if-then-else selection’
PassLimit = 55
read grade
if grade < PassLimit
display "Failed"
else
display "Passed"

11 Programming 1 - Week 3 22/23


‘nested selection’
if <condition>
<statement(s)>
else
if <condition>
<statement(s)>
else
<statement(s)>

12 Programming 1 - Week 3 22/23


Example ‘nested selection’
PassLimit = 55
read grade
if grade < PassLimit
display "Failed"
else
if grade < 80
display "Passed, ok"
else
display "Passed, good"

13 Programming 1 - Week 3 22/23


Example ‘nested selection’ - alternative
PassLimit = 55
read grade
if grade < PassLimit
display "Failed"
else if grade < 80
display "Passed, ok"
else
display "Passed, good"

14 Programming 1 - Week 3 22/23


Exercise 1 – highest/lowest
Read two numbers. Show the highest value and the lowest value.

(highest value = …, lowest value = …)

15 Programming 1 - Week 3 22/23


Exercise 1 – pseudocode
read number1, number2
if number1 > number2
highest = number1
lowest = number2
else
highest = number2
lowest = number1
display "highest: " + highest
display "lowest: " + lowest

16 Programming 1 - Week 3 22/23


Exercise 2 – raise
A staff member receives a 5% raise on his/her monthly salary. The raise should
be at least €75.
Enter the current monthly salary. Calculate and print the raise and the new
monthly salary.

18 Programming 1 - Week 3 22/23


Exercise 2 – pseudocode
read salary
raise = salary * 0.05
if raise < 75
raise = 75
newSalary = salary + raise
display raise, newSalary

19 Programming 1 - Week 3 22/23


Selection with multiple conditions
PassLimit = 55
read grade
if grade >= PassLimit AND grade < 60
display "just passed..."

21 Programming 1 - Week 3 22/23


Truth table: AND
A B A AND B
(A ˄ B)
0 0 0
0 1 0
1 0 0
1 1 1

A-side B-side

22 Programming 1 - Week 3 22/23


Truth table: AND
A B A AND B
(A ˄ B)
0 0 0
0 1 0
1 0 0
1 1 1

A-side B-side
23 Programming 1 - Week 3 22/23
Truth table: OR
A B A OR B
(A ˅ B)
0 0 0
0 1 1
1 0 1
1 1 1

A-side B-side

24 Programming 1 - Week 3 22/23


Truth table: OR
A B A OR B
(A ˅ B)
0 0 0
0 1 1
1 0 1
1 1 1

A-side B-side
25 Programming 1 - Week 3 22/23
Exercise 3 – medal table
Determine whether the Netherlands are positioned above Germany in the
medal table (Olympic Games). Ranking depends on the number of gold, silver
and bronze medals.

26 Programming 1 - Week 3 22/23


Exercise 3 – pseudocode (1)
NLHigher = false
read goldNL, silverNL, bronzeNL
read goldGER, silverGER, bronzeGER

if goldNL > goldGER


NLHigher = true
else if goldNL = goldGER AND silverNL > silverGER
NLHigher = true
else if goldNL = goldGER AND silverNL = silverGER
AND bronzeNL > bronzeGER
NLHigher = true

display NLHigher

27 Programming 1 - Week 3 22/23


Exercise 3 – pseudocode (2)
NLHigher = false
read goldNL, silverNL, bronzeNL
read goldGER, silverGER, bronzeGER

if (goldNL > goldGER) OR


(goldNL = goldGER AND silverNL > silverGER) OR
(goldNL = goldGER AND silverNL = silverGER
AND bronzeNL > bronzeGER)
NLHigher = true

display NLHigher

28 Programming 1 - Week 3 22/23


‘multiple selection’
switch <expression>
case <option1>:
statement(s)
case <option2>:
statement(s)
case <option3>:
statement(s)
default:
statement(s)

29 Programming 1 - Week 3 22/23


Example ‘multiple selection’
read grade
switch grade
case 'A':
display "Excellent"
case 'B', 'C':
display "Well done"
case 'D':
display "Passed"
case 'F':
display "Failed"
default:
display "Invalid grade"

30 Programming 1 - Week 3 22/23


Homework
Read
- Yellow Book → Moodle

(practical class) Programming 1


- week 3 assignments → Moodle

31 Programming 1 - Week 3 22/23

You might also like