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

Inf 154 Inf154 2012 - Lecture 03 - Theory

Uploaded by

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

Inf 154 Inf154 2012 - Lecture 03 - Theory

Uploaded by

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

INF154

Lecture 03 - Theory
Decisions 1

1
Administration
• Elect class representatives
• Class representatives must register
themselves on-line at
http://web.up.ac.za/classrep or by clicking
on this

• banner under Student Affairs on the UP Web


or in the UP Student Portal:
2
Administration
• Homework from next week ONLY on ClickUP
for everybody
• No excuses for late entry, don’t even try!
• Due date and time: Everybody before 10:30
on Monday 5/3. No hand in at class.
• Please note: Create you program in the
following format: HxxS99999999, where xx is
the homework number and S9999999 is your
student number. For example, H03S27883465

3
Lecture overview
1. Program design
2. Decisions
3. Comparison operators
4. General issues

4
1. Program design

5
Program design
Every program
starts at one
point

• Programs need to be Mix all the wet ingredients Every step of the
process is
designed represented by an
activity.
• Programs can be seen as Mix all the dry ingredients
Only one step at a
recipes to perform some time can be
executed.
specific task Mix all the ingredients
Changing the
• For example: to bake a order of steps can
be a problem.
cake Place in pan

The order of the


Bake the cake steps are shown
by the arrows

Ice the cake

Every program
has one end
point
6
Program design
• Take as example the following program
– Take value1 and value2 as input, process by swapping them around
and placing three dashes (-) between them, output the result

Declare variable

Get first value


{
Get second value

{
Swap values

{ {
Place dashes in the middle

Display result

7
Program design
• Program structure:
– You will find that all programs can be done using 4
design structures:
• Sequence: steps just follow one another (all our programs so
far are done like this)
• Decision: sometimes the steps to follow depends on a
decision (this is what we will do in today’s lecture)
• Repetition: sometimes we want to do steps more than once
(we will do this next week)
• Methods: sometimes we will take steps, group them
together, give them a name and then execute the whole
group as if it is one step in our program (later in the course)

8
2. Decisions

9
Decisions
• In many cases the next steps are determined by a decision
• For instance, if a passenger on a flight is an adult (older than 12),
he/she pays R2,000, if a passenger is a child, they pay R1,000.
(see code snippets 3)
int PassengerAge;
double Price;

Get passenger age Console.WriteLine("Enter passenger's age");


PassengerAge = Convert.ToInt32(Console.ReadLine());

[Passenger is adult] [Passenger is child]


if (PassengerAge > 12)
Price = 2000;
else
Pay R2,000 Pay R1,000 Price = 1000;

Console.WriteLine("Price is R" + Price);

Console.Read();
10
Decisions
• An if statement consists of a number of parts
The condition in brackets. This condition can
only have one of two values: true or false (in
other words Boolean). The passenger’s age is
either greater than 12 or less than or equal to 12

The true action. If the


if (PassengerAge > 12) condition is true this
Price = 2000; action will be taken
else
Price = 1000; The optional false
action. If the condition is
false this action will be
taken
11
Decisions
• If more than one action is executed use curly
brackets to group them
if(Number < 5)
{
Number < 5 [true]
Console.WriteLine(“C”);
Console.WriteLine(“D”);
}
Write "C"

[false]

Write "D"

12
2. Comparison operators

13
Comparison operators
Assume: A = 5, B = 3
Operation Standard C# Example Result
notation notation
Is equal to = == A == B False
Is not equal to  != A != B True
Is greater than > > A>B True
Is less than < < A<B False
Is greater than or equal to > >= A >= B True
Is less than or equal to < <= A <= B False

14
The pesky curly brackets and
semicolons – 1 of 3
• One of the most confusing things in C# for
beginners is the curly brackets and semicolons
when doing if statements
• Rule 1: never, ever, ever, place a semicolon on
the if statement line following the closing
bracket.
if(Number < 5) ;

• The above statement means that if Number


is less than 5 the program will do everything
between the ) and the ; which is NOTHING. 15
The pesky curly brackets and
semicolons – 2 of 3
• Rule 2: if the if statement or the else statement
only has one line, you don’t need curly brackets
if(Number < 5)
Value = “Low”;
Else
Value = “High” ;

• BUT if you add them it is no problem


if(Number < 5)
{
Value = “Low”;
}
Else
{
Value = “High” ; 16
}
The pesky curly brackets and
semicolons – 3 of 3
• Rule 3: if the if statement or the else statement
has more than one line, you must enclose them
with curly brackets
if (Number < 5) if (Number < 5) if (Number < 5)
{ Value = “Low”; {
Value = “Low”; else Value = “Low”;
Sum = Sum + 1; { Sum = Sum + 1;
} Value = “High” ; }
else Sum = Sum – 1; else
Value = “High” ; } {
Value = “High” ;
Sum = Sum – 1;
}

17
Class exercise 1
Give the output of the following segments.
Assume: int a = 3, b = 4
If (a == b) If (a == b)
Console.WriteLine(“Black”); {
Console.WriteLine(“White”); Console.WriteLine(“Black”);
Console.WriteLine(“White”);
}
If (a != b) If (a > b)
Console.WriteLine(“Black”); Console.WriteLine(“Black”);
Console.WriteLine(“White”); else
Console.WriteLine(“White”);
If (a < b)
{
Console.WriteLine(“Black”);
Console.WriteLine(“White”); 18
}
3. General issues

19
Formatting output lines
• You can format the WriteLine as follows
– Define a fixed string with the constant text that you want
– For every variable place a placeholder in the fixed string
– A placeholder is a number in curly brackets, e.g. {0}, {1}, {2}, …
– For every placeholder in the string place a variable after the
fixed string – the first variable corresponds to placeholder {0},
the second variable to placeholder {1}, etc.
Console.WriteLine(“The money is {0} and my age is {1}”, someMoney, myAge);

The money is 500 and my age is 20

Console.WriteLine(“I am {0} years old. My age is {0} years”, myAge);

I am 20 years old. My age is 20 years 20


Total and count fields
• Many times you need to get the totals or
counts of certain things
• For instance:
– The number of students in a class – count
– The combined marks for all students in a class –
total
• Total and count fields must always be
initialised before you use them, mostly to 0
• An easy way to add to a total or count field is
as follows: EmployeeCount += 1
TotalSalary += EmployeeSalary
21
Comments
• Writing comments enables you to explain your C#
statements to other humans reading your program
(also you, yourself after a few months)
• C# supports three styles (we discuss only two) of
comments.
• A multiline comment
– Begins with /* and end with */.
– Can extend over many lines
– Anything between these two comment symbols is ignored by
the compiler.
• A single-line comment
– Begins with // and ends at the end of the line
– Only affects one line
– That line or line part is ignored by the compiler
22
Class exercise 2
• Take as input a student’s semester mark
• If his mark is less than 30% display that he/she
does not have exam entrance
• Otherwise display that he/she has exam
entrance

Solution: see code snippet 1


23
Class exercise 3
• Create a simple quiz program
• Ask questions, if person has correct answer let him/her know
• For every correct answer add 1 to the total
• Display the number of correct answers
• Note: always initialise total fields
• Solution: see code snippet 4 (for a complete quiz program see
code snippet 2 – correct the small errors and make it more user
friendly for exercise)

24

You might also like