...

Week 3: Conditional Statements Introduction to Programming Lecturer: Qun Liu

by user

on
Category: Documents
27

views

Report

Comments

Transcript

Week 3: Conditional Statements Introduction to Programming Lecturer: Qun Liu
CA146/CA247A
Introduction to Programming
Week 3: Conditional Statements
Lecturer: Qun Liu
Lab Tutor: Dasha Bogdanova
2nd Semester, 2014-2015 Academic Year
http://computing.dcu.ie/~qliu/CA146-CA247A
Content
Decisions
Combining logical tests
Negation
if…else
switch
18 March 2015
2
if statement
We can use an if statement to test whether something is true.
The thing we are testing is called an expression and is surrounded
by round brackets: ( ).
If the expression
evaluates to true
then the instructions
between the following
set of curly brackets { }
are executed.
18 March 2015
3
if statement
This program prompts
the user to enter a
number and
• if that number is
greater than zero the
program prints out a
message saying so.
• If the number
entered fails the test
(i.e. the expression
evaluates to false)
then the program
just exits.
18 March 2015
4
logical operator
The symbol > is a logical operator.
Here are some others:
>= greater than or equal to
< less than
<= less than or equal to
== equal to
! = not equal to
18 March 2015
5
logical operator
Note we use double equals == to test for equality (remember
we use a single equals = to assign a value).
For example this statement tests whether num
(i.e. the value currently assigned to num) is equal to zero:
18 March 2015
6
Content
Decisions
Combining logical tests
Negation
if…else
switch
18 March 2015
7
Combining logical tests
• This program waits for the user to input a number and then
tests whether it lies between 0 and 10.
• •An expression which
uses the logical
operator && will only
be true if both parts of
the expression are
true.
18 March 2015
8
Combining logical tests
• An expression which uses the logical operator | | will be true
if either part of the expression is true.
• For example this statement returns true if 𝑛𝑢𝑚 is less than
zero or greater than 10:
18 March 2015
9
Content
Decisions
Combining logical tests
Negation
if…else
switch
18 March 2015
10
Negation
• The logical operator ! negates the expression following it.
• For example:
is true if 𝑛𝑢𝑚 is not greater than zero.
It should be read “if it is false that 𝑛𝑢𝑚 is greater than zero”.
18 March 2015
11
Integer variables
as logical variables
• Integer variables can be regarded as logical variables:
is true if 𝑛𝑢𝑚 is not zero and:
is true if 𝑛𝑢𝑚 is equal to zero.
18 March 2015
12
Content
Decisions
Combining logical tests
Negation
if…else
switch
18 March 2015
13
if …else
This program prompts
the user to enter a
number and then
performs one of two
different actions
depending on the
value of the number.
18 March 2015
14
if …else
We can chain together
multiple if … else statements
to test against a succession of
possible conditions as follows:
As soon as one condition
evaluates to true the
corresponding actions are
executed and the program
exits the if … else. If none of
the conditions evaluate to true
then the statements in the
final else block are executed.
18 March 2015
15
Content
Decisions
Combining logical tests
Negation
if…else
switch
18 March 2015
16
switch
Sometimes it is handier to
use a switch statement
rather than a succession
of if … else statements:
This program prompts
the user to enter an
integer between 1 and 3
and stores it in a variable
called 𝑐ℎ𝑜𝑖𝑐𝑒.
It then carries out a
different action for each
possible value of 𝑐ℎ𝑜𝑖𝑐𝑒.
18 March 2015
17
switch
The different possible
values of 𝑐ℎ𝑜𝑖𝑐𝑒 appear
after the word case.
Each action set is
terminated by the word
break which causes the
program to exit the
switch statement.
18 March 2015
18
switch
A default action is
usually supplied which is
carried out if none of the
previous case conditions
are applicable.
The switch statement is
often used in simple
menu driven programs.
18 March 2015
19
Exercises
•
•
•
•
•
•
•
•
•
•
18 March 2015
Ordering numbers
Minimum of three numbers
Water
Digits
Days in a month
The elevator
Grades
Even and odd numbers
Triangles
Leap years
20
Ordering numbers
• Write a program that reads in two numbers.
The program should:
• Print “bigger” if the second number is bigger
than the first
• Print “smaller” if the second number is
smaller than the first
• Print “equal” if the numbers are equal
18 March 2015
21
Minimum of three numbers
• Write a program that reads in three floating
point numbers and prints out the minimum.
• If there is no unique minimum value the
program should print nothing.
18 March 2015
22
Water
• Write a program that reads in a temperature
(in Celsius).
• The program should print out the state water
occupies at that temperature i.e. solid, liquid
or gas.
18 March 2015
23
Digits
• Write a program that reads in a positive
number and prints out the number of digits in
the number.
• If the number is greater than 9999 print a
message to say that numbers greater than
9999 are not handled.
18 March 2015
24
Days in a month
• Write a program that reads in a month
number (1 for January, 2 for February etc.)
and prints out the number of days in that
month.
• Do not use a separate if statement for each
month but combine all months that have the
same number of days into a single check.
18 March 2015
25
The elevator
• A building has 19 floors numbered 1 through 20
(13 is excluded because it is too unlucky).
• Write a program that reads in a floor number and
checks its validity. The program should print out an
error message (and exit) if any of the following are
true:
– 13 is entered
– Zero or a negative number is entered
– A number greater than 20 is entered
18 March 2015
26
Grades
• Write a program that reads in an exam mark
and prints out the corresponding grade. The
rules are as follows:
–
–
–
–
–
0-39 is an F
40-54 is a D
55-69 is a C
70-84 is a B
85-100 is an A
18 March 2015
27
Even and odd numbers
• Write a program that reads in a number. The
program should:
– Print “even” if the number is even
– Print “odd” if the number is odd
• Hint: Make use of the modulus operator % in
your solution.
18 March 2015
28
Triangles
• Write a program that reads in the lengths of each of
the three sides of a triangle. The program should:
– Check if any of the numbers are less than or equal to zero
and exit if so
– Confirm that the length of each side is less than the sum of
the other two. If this is not the case a triangle cannot be
formed and the program should exit.
– Check whether the lengths form an equilateral triangle
(all sides equal).
– Check whether the lengths form an isosceles triangle (two
sides equal).
18 March 2015
29
Leap years
• Write a program that checks whether it is a
leap year. It is a leap year if:
– The year is divisible by 4 (e.g. 1980), but,
– it is not a leap year if divisible by 100 (e.g. 1900),
unless,
– the year is divisible by 400 (e.g. 2000)
18 March 2015
30
Discussion
18 March 2015
31
Acknowledgement
• Most of the content of this lecture is based on
previous lecture given by Gerard Marks.
– http://student.computing.dcu.ie/~ca146lab/html/index.html
18 March 2015
32
Fly UP