COMP1103 Week 3 (1)
COMP1103 Week 3 (1)
INTRODUCTION TO
PROGRAMMING
Spring 2025 – Week 3
Tuğba Erkoç
Announcements
◦ Lab Assignment
◦ Lab assignments will start next week.
◦ To do for this week:
◦ Create your MATLAB account
◦ Install Zoom on your phone
Exercise - 4
◦ Write a MATLAB program which asks the user to enter their name.
Then display a welcome message to the user with their name.
Exercise - 5
◦ Write a MATLAB program which asks the user to enter three floating
point numbers in [-1, 2]. Calculate the average of the numbers. Display
the result with scientific notation.
◦ Example:
◦ If the user enters 0.0025, 1.03 and -0.004 result should be displayed as
3.4283e-01.
Relational Operators
false false false (age <= 18) && (weight < 140) is false, because both
false true false (age < 18) && (weight == 140) is false, because (age
<18) is false.
true false false (age > 18) && (weight > 140) is false, because (weight >
140) is false.
true true true (age > 18) && (weight >= 140) is true, because both
false false false (age == 20) || (weight > 150) is false, because both
false true true (age > 34) || (weight <= 140) is true, because (age > 34) is
true false true (age > 14) || (weight >= 150) is false, because (age > 14) is
true.
true true true (age >= 24) || (weight == 140) is true, because both
Selection Statements
◦ Sequential execution of statements
◦ What if we want to execute only some statements depending on some
conditions/choices?
◦ MATLAB has two basic statements that allow us to make choices based
on conditions:
◦ if statement
◦ switch statement
if Statement
◦ The if statement chooses whether another statement, or group of
statements, is executed or not.
◦ If statement structure looks like
if condition
action
end
Action Condition
if-else Statement
◦ The if statement chooses whether or not an action is executed.
◦ Choosing between two actions, or choosing from among several actions, is
accomplished using if-else
◦ If-else statement structure looks like:
if condition
action1
else
action2
end
if-else Statement
if condition
action1
else
action2
end
◦ Write a MATLAB program to get the weight and height of the user. Calculate
the BMI and then display the interpretation according to the table.
Exercise - 6
◦ Write a program that prompts the user to enter a year and displays the
animal for the year. Note that there are 12 animals associated with
years.
◦ Example: User enters 2012
◦ 2012 maps to 8 so 2012 is the
dragon year
Exercise - 7
◦ Write a program that prompts the user to enter a year as an integer
value and checks if it is a leap year.
◦ A year is a leap year if it is divisible by 4 but not by 100, or it is divisible
by 400.
◦ Tell the user whether the year is leap or not.