120 likes | 136 Views
CS001 Introduction to Programming Day 3. Sujana Jyothi ( sujana @cs.nuim.ie ). What have we learnt so far?. Program – sequence of steps Terms learnt : source code, compiler, statement, data-type, variable, constant. Sequence, Decision. Resources. Resources
E N D
CS001Introduction to ProgrammingDay 3 Sujana Jyothi (sujana@cs.nuim.ie)
What have we learnt so far? • Program – sequence of steps • Terms learnt: source code, compiler, statement, data-type, variable, constant. • Sequence, Decision
Resources • Resources • Use the material from my website. http://www.cs.may.ie/~Sujana/CS001.html • Save your work • on your flash drive • or email it to yourself
ALGORITHM An algorithm is a systematic list of instructions for accomplishing some task, and the task can be anything that has a recognizable end-point (or result). Often some of the specific steps in the procedure are to be repeated until the task is done. Imagine you have a random, unsorted list of numbers. Our goal is to find the highest number in this list. 1. Pretend the first number in the list is the largest number. 2. Look at the next number, and compare it with this largest number 3. Only if this next number is larger, then keep that as the new largest number 4. Repeat steps 2 and 3 until you have gone through the whole list.
ALGORITHM EXAMPLE A farmer has to take three items, a dog, a goat and a cabbage, across a river. He has a boat but it can take two objects at once, in other words, the farmer and one other item only. If the dog and the goat are left together, the dog will bite the goat. If the goat and the cabbage are left together the goat will eat the cabbage. WRITE AN ALGORITHM TO TELL THE FARMER HOW TO GET THE THREE ITEMS ACROSS THE RIVER.
Learn Programming Through Scratch Scratch -> "new programming language that lets you create your own animations, games, and interactive art."
Statement in Scratch Called BLOCKS in SPRITE Tells the computer to do something Instructs a sprite to say something Instructs a sprite to go to some location
Logic Structures • Sequence • Decision / Selection if statement • Looping / Iteration/ Repetition for statement while statement
Boolean Expression • Boolean expression is an expression that is either true or false. TRUE when the mouse is down FALSE when the mouse is not down TRUE when ??????? FALSE when ????????
Decision if statement Instruct a sprite to say hello only if, the user has depressed the mouse button
Decision If - else statement Instruct a sprite to say hello or goodbye, depending on whether the user has pressed the mouse button or not.