COMP1103 Week 5 (1)
COMP1103 Week 5 (1)
INTRODUCTION TO
PROGRAMMING
Spring 2025 – Week 5
Tuğba Erkoç
Exercise - 8
◦ In chemistry, the pH of an aqueous solution is a measure of its acidity.
The pH scale ranges from 0 to 14, inclusive. A solution with a pH of 7 is
said to be neutral, a solution with a pH greater than 7 is basic, and a
solution with a pH less than 7 is acidic.
◦ Write a script that will prompt the user for the pH of a solution, and
will print whether it is neutral, basic, or acidic. If the user enters an
invalid pH, an error message will be printed.
Exercise - 9
◦ In fluid dynamics, the Reynolds number Re is a dimensionless number used to
determine the nature of a fluid flow. For an internal flow (e.g., water flow
through a pipe), the flow can be categorized as follows:
◦ Write a script that will prompt the user for the Reynolds number of a flow and
will print the region the flow is in. Would it be a good idea to write the
selection statements using switch? Why or why not?
Loops
◦ Suppose that we need to display the same message on the screen 100
times.
◦ Would it be easy to repeat the same code 1000000 times?
◦ Example:
◦ disp(“There is an error!”);
◦ disp(“There is an error!”);
◦ disp(“There is an error!”);
◦…
◦ disp(“There is an error!”);
Loops
◦ We cannot repeat typing the same code for 100, 1000 or million times. It is
not feasible/practical.
◦ There is a solution to avoid re-typing the same codes to execute them
repeatedly.
◦ We call the structure that allows us to repeat the same code multiple times
as loop.
Loop Types
◦ There are two loop structures:
◦ for loop → counted loops
◦ Repeats statements a specified number of times.
◦ We know how many times the statements are to be repeated.
◦ Example: Repeat the same calculation 100 times