Docsity

Programming Fundamentals: Data Types, Operators, and Control Structures - Prof. Akerman, Slides of Computer Science

Partial preview of the text

Download Programming Fundamentals: Data Types, Operators, and Control Structures - Prof. Akerman and more Slides Computer Science in PDF only on Docsity!

integer division remainder division

Topic: PROGRAMMING Title: Programming Fundamentals – 1c Challenge Date: 27 June 2025 Objectives :  (^) Understand and use data types: integer, real, Boolean, character and string  (^) Declare and use constants and variables  (^) Use input, output and assignment statements  (^) Use arithmetic operators including MOD and DIV  (^) Use string handling and conversion functions

Good programming habits :  (^) Document your data! (#)  (^) Use naming conventions for variables (e.g. camel case, meaningful etc.)*  Constants in capitals  (^) Indenting*

  • Generally, consistency is more important than specific rules.

Constant const PI = 3. Inputs firstName = input("What is your name? ") Integer division weeks = 31 DIV 7 Remainder division daysLeft = 31 MOD 7

String handling functions Function Example Result str.length word = "Algorithm" print(word.length) 9 str.substring(start, end) print(word.substring(3,6)) "orit" str.left(n) Print(word.left(3)) "Alg" str.right(n) Print(word.right(4)) "ithm" Remember : strings and arrays start their index at 0

Sequence The statements are executed one by one in the order they are written: mark1 = 78 mark2 = 67 total = mark1 + mark average = total / 2 print(average)

Comparison expressions

  • (^) The condition average >= 80 is a Boolean expression The outcome will always evaluate to TRUE or FALSE
  • (^) Comparison operators include == equal to != not equal to

    greater than < less than

  • (^) What are two other comparison operators? Why is = not used to mean equal to?

Boolean expressions

Boolean expressions evaluate to being either true or false. A single = is used for assignment - e.g. age = 14 Comparison operators Meaning Pseudocode example Result Notes == Equal to 5 == 5 True Some languages use a single = != Not equal to 5 != 5 False Visual Basic uses <>

Greater than 5 > 5 False = Greater than or equal to 5 >= 5 True < Less than 5 < 5 False <= Less than or equal to 5 <= 5 True

Nested if statements

  • (^) If statements may be nested: if member == "child" then if day == "Saturday" then swimPrice = 2. else swimPrice = 2. endif else swimPrice = 4. endif
  • (^) What is the price for an adult on Saturday?
    • (^) What is the price for a child on Sunday?

Complex Boolean expressions

  • (^) Boolean expressions can include the Boolean operators AND, OR and NOT
  • (^) For example: if (mark < 0) OR (mark > 100) then … Operator Description AND Returns TRUE if both conditions are TRUE OR Returns TRUE if either of the conditions are TRUE NOT A TRUE expression becomes FALSE and vice versa

True or False?

Mark1 Mark2 Condition True or False? 80 67 (mark1 >= 80) AND (mark2 >= 80) False 82 80 (mark1 >= 80) OR (mark2 >= 80) True 35 (mark1 > 30) OR (mark1 < 50) True 65 (mark1 < 30) OR (mark1 > 80) False 0 75 NOT(mark1 > 50) AND (mark2 > 50) True 65 85 NOT(mark1 < 60) AND NOT (mark2 <

True

  • (^) Complete the table:

The switch/case statement This statement may be used when a selection is to be made from several alternatives, for example when choosing from a menu switch menuChoice: case "1": print("You selected 1") case "2": print("You selected 2") case "3": print("You selected 3") default: print("This is not a valid choice") endswitch

Random numbers How could you simulate the throw of a die?

  • (^) Pseudocode: die = random(0, 100) print(die) To create a random real number between -100.0 and 100.0, you would use: die = random(-100.0, 100.0)
  • (^) Python: import random die = random.randint(0, 100) print(die)
  • Worksheet 1 and