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

COMP1103 Week 3 (1)

This document outlines the curriculum for COMP1103 Introduction to Programming for Spring 2025, including lab assignments and exercises focused on MATLAB programming. It covers relational and logical operators, selection statements like if and if-else, and provides examples of programming tasks such as calculating BMI and determining leap years. Additionally, it includes exercises for students to practice their programming skills in MATLAB.

Uploaded by

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

COMP1103 Week 3 (1)

This document outlines the curriculum for COMP1103 Introduction to Programming for Spring 2025, including lab assignments and exercises focused on MATLAB programming. It covers relational and logical operators, selection statements like if and if-else, and provides examples of programming tasks such as calculating BMI and determining leap years. Additionally, it includes exercises for students to practice their programming skills in MATLAB.

Uploaded by

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

COMP1103

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

Relational Operator Operation


== Equality
>= greater than or equal to
> greater than
<= less than or equal to
< less than
~= inequality
Relational Operators
◦ Comparisons produce logical answers: true or false
◦ Equality
◦ x == y
◦ Inequality
◦ x ~= y
◦ Greater than
◦ age > 18
◦ Less than
◦ temperature < 20
◦ These comparisons can be used as conditions in our programs
Logical Operators

Logical Operator Operation


&& Logical AND
|| Logical OR
~ Logical NOT
Truth Table for AND operator
p1 p2 p1 && p2 Example (assume age = 24, weight = 140)

false false false (age <= 18) && (weight < 140) is false, because both

conditions are false.

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

(age > 18) and (weight >= 140) are true.


Truth Table for OR operator
p1 p2 p1 || p2 Example (assume age = 24, weight = 140)

false false false (age == 20) || (weight > 150) is false, because both

conditions are false.

false true true (age > 34) || (weight <= 140) is true, because (age > 34) is

false, but (weight <= 140) is true.

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

◦ A condition is a relational expression that is logically true or false.


◦ The action is a statement or a group of statements that is executed if
the condition is true. If not, the action will not be executed.
if Statement
if condition
action
end

◦ A condition is a relational expression that is logically true or false.


◦ The action is a statement or a group of statements that is executed if the
condition is true. If not, the action will not be executed.
◦ The action can be any number of statements until the reserved word end.
◦ The action is between the reserved words if and end.
if Statement - Example
◦ Remember the MATLAB program that calculates the area of a circle? What if
the user enters a negative value for radius?
◦ Rewrite the codes calculate the area of the circle only if the radius value is
nonnegative.

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

◦ As first step, the condition is evaluated.


◦ The second step is the selection of the set of statements to be executed according to the
condition’s result.
◦ If the condition is true: the set of statements shown as “action1” is executed. (If clause)
◦ If the condition is false: the set of statements shown as “action2” is executed. (Else
clause)
if-else Statement -Example
◦ Remember the MATLAB program that calculates the area of a circle? We no
longer perform area calculation when user enters a negative radius value.
◦ However, we do not tell the user why we did not calculate the area.
◦ Let’s rewrite the code to calculate the area if the user enters a nonnegative
value . If not (Else), tell the user that we are expecting a nonnegative value.
Nested if-else Statements
◦ The if-else statement is used to choose between two actions.
◦ If you have more than two actions the if-else statements can be nested to
choose from actions based on the conditions.
Nested if-else Statements - elseif
◦ Since you have more than two actions, you will use elseif to check the new conditions.
if condition1
action1
elseif condition2
action2
elseif condition3
action3

else
actionN
end
Nested if-else Statements - Example
◦ Consider the table below. Get the
score from the user. Then display
the letter grade.
Grade Letter Grade

Score >= 90 Grade is A

Score >= 80 Grade is B

Score >= 70 Grade is C

Score >= 60 Grade is D

Any score less Grade is F


than 60
Examples – Divisible?
◦ Write a program that checks whether a number is divisible by both 2 and 3.
Examples – Even two-digit number
◦ Write a MATLAB program that asks the user to enter a number. Your program
must check whether this number is an even two-digit number. If it is an
even two-digit number, display its digits to the user. Else, tell the user that it
is not an even two-digit number.
Examples – BMI calculator
◦ Body Mass Index (BMI) is a measure of health on weight. It can be calculated
by taking your weight in kilograms and dividing by the square of your height
in meters. The interpretation of BMI for people 16 years or older is as follows:
BMI Interpretation

BMI < 18.5 Underweight


18.5 <= BMI < 25.0 Normal
25.0 <= BMI < 30.0 Overweight
30.0 <= BMI Obese

◦ 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.

You might also like