Presentation is loading. Please wait.

Presentation is loading. Please wait.

Coding Concepts (Basics)

Similar presentations


Presentation on theme: "Coding Concepts (Basics)"— Presentation transcript:

1 Coding Concepts (Basics)
Topic 2: Programming Coding Concepts (Basics)

2 Developing Code We’ve seen how to both analyse problems and evaluate solutions Both of which can be done before we put code to file If we want to implement a solution, we’ll eventually have to program something For this course, knowing how to program isn’t the only thing we need to know We also need to learn the following: Fixing possible errors Using coding constructs Using correct data types/structures Writing descriptive comments Creating sub-programs Validating user input Programming: Coding Concepts (Basics)

3 Programming: Coding Concepts (Basics)
Developing Code The first thing to note is that you have a choice of programming language Out of: C#, Java, and Python You are free to pick any of these three Coding examples will be given for all languages Choose the one that you are most comfortable with The following topics are used in all three Programming: Coding Concepts (Basics)

4 Programming: Coding Concepts (Basics)
Developing Code The second thing to note is how we define a program in the different languages Java and C# does this easily Create project/solution acts as a single program A file can be added (usually automatically) that acts as the starting point of the program Inside of which is a main() function Python is a bit more freeform The file itself acts as the entry point No need for a main() function But one can be added Programming: Coding Concepts (Basics)

5 Programming: Coding Concepts (Basics)
Developing Code Python C# Java In Python, we don’t need to create a main function. However, if we were to create larger Python programs later on (which may require multiple Python files), having floating code (instructions not in any scope) is actually a bad thing. When importing a Python file for use in another file, floating code is actually run as well (not just ‘included’). This means we could accidentally run a different program when importing a Python file. Including the if __name__ == ‘__main__’ check means that we only run that function if and only if the script itself is being run, not when it is imported into another file. Programming: Coding Concepts (Basics)

6 Programming: Coding Concepts (Basics)
Coding Constructs All procedural programs are built off three important ‘pillars’ Sequencing Branching Iteration These are known as constructs They each describe a certain action (or series of actions) They can be implemented in different ways (with different techniques) Programming: Coding Concepts (Basics)

7 Coding Constructs: Sequencing
This is the act of running instructions one after the other Running them “in a sequence” This is the simplest construct to use All we have to do is do things in the program Branching and Iteration are used via Sequencing Programming: Coding Concepts (Basics)

8 Coding Constructs: Sequencing
This simple example includes creating variables and storing values in them Sequencing is as simple as running one instruction after another Python C#/Java Programming: Coding Concepts (Basics)

9 Coding Constructs: Branching
This is the act of choosing specific instructions to run (sequences) Based on a condition (a boolean expression) Branches can be introduced via four different techniques If statements Else statements Else-If statements Switch statements If, Else, and Else-If can be used together Need at least one if-statement Can have as many else-if statements as we want Can only have one else-statement at the end Programming: Coding Concepts (Basics)

10 Coding Constructs: Branching
This example checks to see if the user entered yes or no to a question Python Java C# Programming: Coding Concepts (Basics)

11 Coding Constructs: Branching
Switch-statements are used on their own Not part of any other technique They can take in any numerical value And then check possibilities of that value (known as cases) Note three important things Switch-statements only check for equality Cases in a switch-statement need to be broken out of (to stop trickle down) Switch-statements are not in Python Programming: Coding Concepts (Basics)

12 Coding Constructs: Branching
Here’s the same example from earlier (using a switch-statement this time) C# It’s the same in C# as it is in Java (except for the differences in how output work). Programming: Coding Concepts (Basics)

13 Coding Constructs: Iteration
This is the act of repeating sequences based off of a condition Iteration is used when we want to repeat a sequence A set number of times (within a specific range of values) Indefinitely (until a condition returns false) We have two techniques for using iteration For-loops While-loops For-loops have more control (we use them when we know how many times to repeat) While-loops are for more indefinite loops Programming: Coding Concepts (Basics)

14 Coding Constructs: Iteration
Here’s an example of using a for-loop to search for a value in an array Python C# Programming: Coding Concepts (Basics)

15 Coding Constructs: Iteration
Here’s an example of using a while-loop to keep a program running until the user wants to stop Python C# Programming: Coding Concepts (Basics)

16 Programming: Coding Concepts (Basics)
Coding Constructs Those are all the constructs that a program can use to solve different problems Knowing which technique to use at a certain point is fundamental to creating good programs For example, should we use a for-loop instead of a while-loop? We also have other things, like data-structures and sub-programs to look at Will come later Programming: Coding Concepts (Basics)

17 Programming: Coding Concepts (Basics)
Create the following programs in your programming language of choice Given an array of positive integers, find the smallest positive integer that does not appear in the array Given an array of integers and an integer, output “Yes” or “No” depending on if two integers in the array add up to the given integer For exercise 1: Imagine having the array [1, 6, 3, 8, 9, 5]. The program should say 2 (because it’s the first positive number, starting from 1, that is not in the array). For exercise 2: Imagine having the array [1, 4, 8, 2, 9, 10], and the number 6. The program should say “Yes”, because the array contains 4 and 2, which add up to 6. Programming: Coding Concepts (Basics)

18


Download ppt "Coding Concepts (Basics)"

Similar presentations


Ads by Google