Inf 154 Inf154 2012 - Lecture 03 - Theory
Inf 154 Inf154 2012 - Lecture 03 - Theory
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
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
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
{
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;
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
[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) ;
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);
24