Topic 2 Programming Coding Concepts Basics Computer Science

Topic 2: Programming Coding Concepts (Basics) Computer Science

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 possible errors Using coding constructs • We also need to. Fixing learn the following: Using correct data types/structures Creating sub-programs 2 Writing descriptive comments Validating user input 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 3 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 4 Programming: Coding Concepts (Basics)

Developing Code Python C# Java 5 Programming: Coding Concepts (Basics)

Coding Constructs • All procedural programs are built off three important ‘pillars’ a. Sequencing b. Branching c. 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) 6 Programming: Coding Concepts (Basics)

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 7 Programming: Coding Concepts (Basics)

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 8 C#/Java Programming: Coding Concepts (Basics)

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-If statements Switch statements • If, Else, and Else-If can be used together 9 Need at least one if-statement Can have as many else-if statements as we want Programming: Coding Concepts (Basics) Can only have one else-statement at the end

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

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 11 Programming: Coding Concepts (Basics)

Coding Constructs: Branching • Here’s the same example from earlier (using a switch-statement this time) C# 12 Programming: Coding Concepts (Basics)

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 13 Programming: Coding Concepts (Basics)

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

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# 15 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 16 Programming: Coding Concepts (Basics)

• Create the following programs in your programming language of choice 1. Given an array of positive integers, find the smallest positive integer that does not appear in the array 2. 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 17 Programming: Coding Concepts (Basics)

- Slides: 18