all right welcome back to program logic and design we will use discussing the three basic structures remember we don't want our code to be all tangled up with our one flow lines crossing each other that is where spaghetti comes in mind so we don't want spaghetti code so we want structured programming member spaghetti code is our unstructured programs so let's see structure is your basic units of programming logic you get three tabs you get your sequence structure which performs actions in order doom doom doom straight underneath each other there's no branching or skipping any tasks then you get your selection structures which is your decision structures remember your decision symbols which is your diamond shape and you ask a question and then you're going to take one of two actions a yes or no you get your D alternative ifs or your single turnit effects which we're going to be discussing later on then you get your loop structure you repeat actions while a condition remains true so we'll discuss the loop structure in momentarily now as well now the mathematicians in the mid 1960s proved that any program no matter how complicated can be constructed using one of more of these three types of structures our sequence our selection and our loop now these three structures alone you can diagram any task from doubling another to performing a brain surgery just from these three so I'm going to explain the sequences in the next slide this is your sequence structure it's doom doom doom right on top of each other you'll see we stacking a box on top of the box on top of the box so sequence structures performs actions in order see there's no branching and no skipping it's just doom-doom doom-doom and they continue step by step until the sequence ends like driving directions you go down this road then you turn right then you turn left and you turn one again and on your left-hand side if you look there as the house it's number signs oh all right so I've added this the sequence structure to your template in your PowerPoint for you to use later on then we get our dual alternative it's these are our selections now our dual alternative is contained two alternatives so other one yes or other no dual alternative ifs or selections is our if-then-else structure so if some condition is true like if traffic is backed up on Cape Road if that is true then we're going to do one process are we going to continue one block on Hood Street then turn right on Fifth Avenue else we're going to do another process as we're going to turn right on Cape braked and then whenever we open an F we always close the if okay so this is our if-then-else structure this is how our do alternative if then our structure looks like we have if yes we do this if no we do this if yes we're going to turn down Cape Road and turn it into hurt Street if it's not too much backed up then we're going to turn right it was not backed up on cap right okay so those are our two alternatives so with a selection structure which are diamond-shape you've got to ask a question and take one of two actions so that's I'll do alternatives so a flowchart that describes a selection structure must begin with this decision symbol and the branches which is these flow lines the branches of decision join at the bottom of the structure so our flow lines join at the bottom remember it's got to happen in sequence so this is one structure so think of this selection structure we're popping in this big old box and that's going to happen first and then it's going to move on to our next box which might have a loop in or another sequence so our selection structures must ask a question that can be answered only with a yes or a No so they both have an alternative to them to do our single alternative ifs it looks like this you've got yes and you'll just have one alternative to doom this will be like if employee belongs to a dental plan then deduct 40 from employee grasp a and Al's Clause is not required because there's only going to be one alternative one thing that needs to happen so your pseudocode will be if it is raining then take an umbrella and if done that's it so a null case on the other hand will a null branch situation if it is not raining then an umbrella does not need to be taken so the case basically a null case is when a situation where nothing is done so we don't need to take an umbrella so that will be a null case so a flowchart that describes the selection structure with a single alternative must begin with the decision symbol like a dual alternative and the branches opt decision join at the bottom of the structure so the selection structure must ask a question that can be answered only with a yes or a no no will basically just take you carry you'll just carry on with the program so that nothing will have actually really happen but if it's yes if that question which is in our decision here is answered yes then you're going to have to do something because it says we're going to have to process something somethings needs to be done so that will be our single alternative our loop structure now third basic structure is a loop loop structure is repeats a set of actions while the condition remains true you've seen that sometimes we need to repeat something to happen make it happen over and over and over and over and over again so instead of having ten million lines of code it's repeating the same lines over and over and over over gamma we put it in a loop body and the loop body the action or actions that occur within the loop is going to go here in our loop body now the loop structures provide the ability to perform Ribit of processing on a very large scale looping is often referred to as repetition or iteration so if you get that in your tester exam what is a loop structure its repetition or iteration pseudocode uses while do or while loop alright we'll chat shortly about these two loops later but a condition is tested first in the most common form of the loop which is our while loop so for our loop structure here we will enter our loop our decision this will be our condition which is first tested this is our condition here which needs to be tested and if the condition is true or in other words yes it is true yes say it nobody is entered in zzzzz to exit the program if we asking for a whole lot of people's names to be entered in then we're going to do some processing and then we're going to loop back to the beginning of the decision so we can carry on and live back to the beginning and carry on and back carry on and carry on and carry on until this decision here equates to false so it will be known as the answer and then we carry on with the program so this is in another set of boxes now the reason I'm saying set of boxes is I want you to think everything in a box because we're going to stack them just now always begin the decision symbol that has the branches that return to the prior spot to the decision so always to the prizefight very important you execute the loop body over and over again while the condition is still true only executing the loop when the decision is false so we have our while test condition continues to be true then do some process so you start with a while and you end with an N structure statement end while so while you continue to be hungry take another bite of food the turn if you're still hungry end while while test condition continues to be true we're going to be doing something as I said before is that we need to stack things so all logic problems can be solved using only sequence selection and loop in an and number of ways so that's where we get two hours stacking structure we attach structures end to end we put a box down and we put another box on top we are we stack stack stack stack or stack to the side etc so it's very important that structures may only be stacked end to end sometimes students commonly make the mistake of interweaving structures instead of actually stacking them but as you can see you can't with our contents of our box if you have a look at our contents of a box here you can't interweave things going in here this is one action this is one structure you can't interweave now a sequence in here because this is one action it's got to first end for in order for us to stack it on top of or put something a sequence in there n structure statements indicates the end of a structure that is your end if statements ends an if then our structure and the end while statement ends a loop structure or if you open up an if you closed there if you open up a while you close the one very important figure 3 6 is a structured flowchart and pseudocode with three stacked structures can you fancy the structures yes we've got our sequence we got our selection and we've got our loop remember I said that it can be stacked in an infinite number of ways so we could have loop first and then sequence and then selection or selection first then sequence and then loop it doesn't really matter where it goes as long as long as it is stacked on top of each other and not lines not crossing each other very important please note that the yes and no or true and false is in either flow chart or pseudocode but in this course I want you guys to have yes and no reserved only for flow times and true and false only for pseudocode ok we've learned now about stacking our structures now I want to show you that you can actually miss your structures now any individual tasks will step in a structure can be replaced by structures so we call it nesting structures placing one structure within another so we indent the nested structures statements and then we get a block which is a group of statements that execute as a single unit so some languages have block delineation symbols for example Java and C++ use curly braces to identify blocks there's an example of a nested abstraction so we've got our condition here our decision structure and within our decision structure we have a sequence so we're nesting our sequence within our selection so step JK and our constitute as a block which is a group of statements that execute as one single unit now as we got to the end structure statements how do these statements help to outline a block of code that involves the structure all we need to basically do is placed J K and al you are nesting the structures in other words you are indenting pseudocode so as you can see if condition H is true then we indent our pseudocode stick jiaying step K step L all in our if statement here's another example is that any sequence or selection loop can be contained in other sequences selections and loops so as you can see here we've got our step J which is our sequence and from our sequence we go into a loop which is in a while loop and from our while loop will go back into the rest of our sequence so we would have had like a sequence here we've just replaced this sequence in the middle here we've replaced it with a loop now so step n is now within a loop there this is our a single selection decision because we are they have no or yes now notice if an ENDIF are vertically aligned so they're on the same level can you notice any other alignments yeah step J and step out are aligned and while and end while are aligned so they're all vertically aligned so structures nested cannot over lamp example end well comes b4 and if so it's nested within these so and while is with within the if structure here's another example with for a selection within a loop within a sequence within a selection so we have a decision we have a sequence and within the sequence is a loop the decision within the loop there is no limits the number of levels you can create when you nest and stack structures if you have a look at step and yeah step in has been replaced with a selection to make the logic more complicated structures that performs a step P and step Q based on the outcome of the condition and this is basically a nested within the loop controlled by condition M so please note this is a generic structure for generic conditions like condition M which can stand for anything we will apply to this to real life programs the summary is on page 94 it's very important to know as well as figure 3.11 you've got your sequences your selection and your loop structure programs have the following characteristics include only combinations of three basic structures which which discusses your sequences your selections and your loop you each structure has a single entry point and a single exit points single entry single exit single entry single exit single entry single exit and the exit handles is where you can physically pick up any of these restructure or replace the section here with the structure or replace this section here with structural this section here with a structure etc these are all spots you can actually connect one structure to another so the entry and exit is where you can connect or one structure to another structures can be stacked or connected to one another only at their in exit points as we've discussed and any structure can be nested within another structure please join me in the next video in which I will be discussing priming input to structure a program