11 Programing concepts

Nov 10, 2019

Chapter Overview

  • Basic data types
    • Integer
    • Real number
    • Char
    • String
    • Boolean
  • Programming
    • Rules in naming variable
    • Sequence
    • Selection
    • Iteration
    • Totalling
    • Counting

Note: All codes in this article are written in Python


11.1 Basic data types

INTEGER
A positive or negative whole number that can be used with mathematical operators.

REAL
number with a fractional part, that can be used with mathematical operators.
Not all programming languages distinguish between real numbers and integers. For example, JavaScript makes no distinction but Python does.

Integer = int(25) // Declare an integer with the value of 25 
Real = float(25) // Declare a real number with the value of 25.0 

CHAR
single character.

Char = 'A' 
Char = "A"

STRING
A string variable has several characters in length. String vary in length and may even have no characters. The characters can be lettersand/or digits and/or any other printable symbols. For example:

exam_name = 'CIE'
subject_code = '0478'
entry_password = '@!&*%^$#'

BOOLEAN
A boolean variable can have only two values: TRUE or FALSE

pass_CIE_exam_with_help_of_SCIEprogrammer = TRUE

11.2 Programming

RULES IN NAMING VARIABLES

  • Case-sensitive (number1 is different from Number1)
  • Can contain numbers, letters and underscore
  • CAN NOT start with a number
  • CAN NOT be the same as Python Reserved words
    E.g. max, min, sum
  • USE DESCRIPTIVE NAMES and ADD COMMENTS
    Otherwise, you’ll lose mark in CIE EXAM !

SEQUENCE
Statements that runs one by one in order.

first_num = int(input('Enter the first whole number:'))
second_num = int(input('Enter the second whole number:'))
total = first_num + second_num
print('The sum equals', total)

SELECTION
Running the following statements depends on whether the criteria given is satisfied or not. In Python we use IF statement:

if grade == 100:
    print('Excellent! You got TOP IN THE WORLD in computer science.')
elif grade > 80:
    print('Great! You can improve by spending more effort.')
else:
    print('Oops! You need more hard work.')

Difference between single equal sign (=) and double equal signs (==)

  • Single Equal Sign means to assign value or content or variable on right to the variable on the left
    • It means ‘
Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.