Top Banner
CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana @cs.nuim.ie )
12
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

CS001Introduction to Programming

Day 3

CS001Introduction to Programming

Day 3

Sujana Jyothi([email protected])

Page 2: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

2

What have we learnt so far?

• Program – sequence of steps

• Terms learnt: source code, compiler,

statement, data-type, variable, constant.

• Sequence, Decision

Page 3: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

3

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

Page 4: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

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.

Page 5: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

ALGORITHM EXAMPLEA 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.

Page 6: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

Learn Programming Through Scratch

Scratch -> "new programming language that lets you create your own animations, games, and interactive art."

Page 7: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

Statement in ScratchTells the computer to do something

Instructs a sprite to say something

Instructs a sprite to go to some location

Called BLOCKS in SPRITE

Page 8: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

Logic Structures• Sequence

• Decision / Selection if statement

• Looping / Iteration/ Repetition for statement while statement

Page 9: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

Boolean Expression

• Boolean expression is an expression that is either true or false.

TRUE when the mouse is downFALSE when the mouse is not down

TRUE when ???????FALSE when ????????

Page 10: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

Decision

if statement

Instruct a sprite to say hello only if, the user has depressed the mouse button

Page 11: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

Decision

If - else statement

Instruct a sprite to say hello or goodbye, depending on whether the user has pressed the mouse button or not.

Page 12: CS001 Introduction to Programming Day 3 Sujana Jyothi (sujana@cs.nuim.ie)sujana@cs.nuim.ie.

Looping / Iteration/ Repetition