Download presentation
Presentation is loading. Please wait.
1
Coding Concepts (Data- Types)
Topic 2: Programming Coding Concepts (Data- Types)
2
Programming: Coding Concepts (Data-Types)
This presentation is all about data The different forms we can see in a program The different structures we can use to store it In every programming language, data comes in different types Although data is just binary, these types help the interpreter/compiler understand what the data is used for Is it text? Then we can concatenate it Is it numerical? Then we can add/subtract/multiply/divide it Programming: Coding Concepts (Data-Types)
3
Numerical Logical Text
Data Types Programming languages usually have their own way of saying whether something is a number or text Some languages don’t care Before looking at specific languages, let’s cover the main data-types we can come across They’re split into three categories Numerical Logical Text Programming: Coding Concepts (Data-Types)
4
Programming: Coding Concepts (Data-Types)
Data Types: Numerical Numerical types are split into two sub-types Whole Numbers (Integer) Decimal Numbers (Real) Integers are positive or negative numbers that do not have a fractional part to them Reals are positive or negative numbers that can have a fractional part Though they don’t need one Programming: Coding Concepts (Data-Types)
5
Programming: Coding Concepts (Data-Types)
Data Types: Numerical Integers are typically represented using the int keyword int meaning Integer In technical terms, an int is any whole number that fits within 32 bits Most significant bit used for sign (think Two’s Complement) Imagine converting a denary number to a binary one If it fits within these digits, then there’s no problem If the number is too large, we’ll get an overflow The number will go back to a really small one Programming: Coding Concepts (Data-Types)
6
Programming: Coding Concepts (Data-Types)
Data Types: Numerical Reals can be represented in two ways 32-bit floating-point numbers: float 64-bit floating-point numbers: double The double is the most commonly seen, and what we’ll use (if the programming language needs it) These numbers can have a decimal point with digits after it Programming: Coding Concepts (Data-Types)
7
Data-Types: Numerical
Python doesn’t care about data-types when we program in it It is referred to as a loosely-typed language The data-type of a variable doesn’t really matter As long as our program makes sense C# and Java, however, need to know what data- types to use They are strictly-typed Programming: Coding Concepts (Data-Types)
8
Data-Types: Numerical
Here’s how we can use these types in the different programming languages Including conversion Python Java C# Programming: Coding Concepts (Data-Types)
9
Programming: Coding Concepts (Data-Types)
Data-Types: Logical The Logical type is represented in one way Using a Boolean Booleans refer to any value that is true or false Or 1/0 As before, Python doesn’t care However, C# uses bool and Java uses Boolean They both have the values as true and false Python, however, has the values as True and False Those capitals are important! Programming: Coding Concepts (Data-Types)
10
Programming: Coding Concepts (Data-Types)
Data-Types: Logical Here’s how we can use the Boolean type in each of the languages Including conversion Note that we don’t convert directly to a Boolean We take the text input And compare it against another result This gives us the Boolean value Java C# Python Programming: Coding Concepts (Data-Types)
11
Programming: Coding Concepts (Data-Types)
Data Types: Text This final type has two sub-types as well Individual characters (Character) A series of characters in a row (String) These are represented using two keywords (in C# and Java) Characters use char Strings use string (in C#) and String (in Java) Python, as usual, doesn’t really care Programming: Coding Concepts (Data-Types)
12
Programming: Coding Concepts (Data-Types)
Data Types: Text There are also two different ways of representing values of these sub-types String values often use speech marks (“”) Character values often use quotation marks (‘’) Python doesn’t care what you use, and actually doesn’t differentiate between strings and characters This does matter for C# and Java, as it thinks of characters as numbers As they come from the ASCII table Programming: Coding Concepts (Data-Types)
13
Programming: Coding Concepts (Data-Types)
Data-Types: Text Here’s how we can use these text types in different programming languages C# Java Python Programming: Coding Concepts (Data-Types)
14
Programming: Coding Concepts (Data-Types)
Manipulating Text The number and logical data-types have their own operators Numerical Operators: * / % Logical Operators: ||/or &&/and Relational Operators: > < == >= <= Equality Operators: == !/not Text also has its own features Not really operators per se But functions which that text value can use Programming: Coding Concepts (Data-Types)
15
Manipulating Text: Cases
First up is the ability to take any text value and make it all upper-case or lower-case Running this function typically returns a new text value That is the upper/lower case version We would need to store this in a variable This is a useful thing to do, especially with user input Programming: Coding Concepts (Data-Types)
16
Manipulating Text: Cases
For example, we want to compare a user-input value against a choice A simple one is yes/no, or y/n The characters Y and y are actually different Because they have different ASCII values We should accept both Y and y from the user if they enter it in Since, from the user’s standpoint, they are the same letter Programming: Coding Concepts (Data-Types)
17
Manipulating Text: Cases
Here’s how we can handle input without introducing a case bug C# Java Python Java has to compare Strings using equals() (because it is an object). Java also has equalsIgnoreCase() for Strings, which removes the need for changing the case. Programming: Coding Concepts (Data-Types)
18
Programming: Coding Concepts (Data-Types)
Create the following program: Generate a random number (between 0 and 1000) Ask the user if they would like to see another number Give the option of y/n If they do, then generate another random number If they do not, then end the program Programming: Coding Concepts (Data-Types)
19
Manipulating Text: Substring
Strings are technically strings of characters Which means there is more than one character value If we have ‘more than one’ of a value in a single variable, well that’s an array We can treat string values as arrays Which means getting individual characters (as we saw before) Or getting substrings (groups of characters in the string) Programming: Coding Concepts (Data-Types)
20
Manipulating Text: Substring
Let’s say we’re creating a registration system that needs user information It creates a username for a user based off A unique identifier at the beginning Their first-name’s initial 4 letters from their surname If their surname has more than 4 letters, then we only want to get the first 4 For example, for Jane Doeson, their username could be: 0192JDOES Programming: Coding Concepts (Data-Types)
21
Manipulating Text: Substring
If we’re making this program, then we would Generate a number for the beginning of the username (could be randomly) Ask for the user’s first name Ask for the user’s surname From this we would need to create the username That means Getting the first letter of the first name Getting the first 4 letters of the surname (or the whole surname if less than, or equal to, 4 letters) Concatenating this all together and making it upper case Programming: Coding Concepts (Data-Types)
22
Manipulating Text: Substring
Here is how we can do this in the different languages C# Python Java Python stores Strings in arrays, meaning we can use array slicing. That’s the [0:4] bit. We’re getting all the values from index 0 to index 4 (exclusive). Programming: Coding Concepts (Data-Types)
23
Programming: Coding Concepts (Data-Types)
Create the following program Get a word from the user Add a hyphen to the end of the word: - Take the first letter of the word and move it to the end Add “ay” to the end of the word Output the word Programming: Coding Concepts (Data-Types)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.