intro to programming part of chapter 5. algorithms an algorithm is an ordered set of executable...

13
Intro to Intro to Programming Programming Part of Chapter 5 Part of Chapter 5

Post on 21-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Intro to Intro to ProgrammingProgramming

Part of Chapter 5Part of Chapter 5

Page 2: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

AlgorithmsAlgorithms

An algorithm is an An algorithm is an orderedordered set of set of executableexecutable steps that defines a steps that defines a terminatingterminating process. process.

Procedure to solve the problemProcedure to solve the problem

Page 3: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

TerminologyTerminology

Procedure & FunctionProcedure & Function Syntax (a programming language Syntax (a programming language

standard)standard) BASIC: PRINT “Hello!”BASIC: PRINT “Hello!” C: printf(“Hello!”);C: printf(“Hello!”); C++: cout << “Hello!”;C++: cout << “Hello!”; JAVA: System.out.print(“Hello!”);JAVA: System.out.print(“Hello!”);

Variable & ConstantVariable & Constant C:C: int Var = 0;int Var = 0;

Var = Var++;Var = Var++;

Page 4: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Conditional statementConditional statement & & LoopLoop

Conditional statement Conditional statement if if ConditionCondition then then ActionAction

Page 5: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Conditional statementConditional statement & & LoopLoop

Loop (Recursice Structure)Loop (Recursice Structure) while while ConditionCondition do do ActionAction C: C: while( Var >= 100)while( Var >= 100)

printf(“TRUE”);printf(“TRUE”); For loop (Iterative Structure)For loop (Iterative Structure)

for for Iterative statementIterative statement do do ActionAction C:C: for( Var = 1; Var <= 3; Var ++)for( Var = 1; Var <= 3; Var ++)

printf(“Hello!”);printf(“Hello!”);

Page 6: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Conditional statementConditional statement & & LoopLoop

While loopWhile loop

Page 7: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Conditional statementConditional statement & & LoopLoop

For loopFor loop

Page 8: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Conditional statementConditional statement & & LoopLoop

Select & Case Select & Case BASIC: BASIC: SELECT CASE Name$ SELECT CASE Name$

CASE "Ted" CASE "Ted" PRINT “Greetings." PRINT “Greetings."

CASE "Mike" CASE "Mike" PRINT "Go away!" PRINT "Go away!"

CASE ELSE CASE ELSE PRINT "Hello, "; PRINT "Hello, ";

Name$ Name$ END SELECT END SELECT

Page 9: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Conditional statementConditional statement & & LoopLoop

Select & CaseSelect & Case

Page 10: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Practice 1Practice 1

Create a program with following Create a program with following specifications.specifications.

SpecificationSpecification First prompts the user with First prompts the user with

““Do you want to exit? (Y/N)”Do you want to exit? (Y/N)” Exit program only if the input is “Y” Exit program only if the input is “Y”

otherwise, keep running the prompt otherwise, keep running the prompt messagemessage

““Y” or “y” Y” or “y” Exit the programExit the program ““N” or “n” N” or “n” Keep loopingKeep looping

Page 11: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Practice 2Practice 2

SpecificationSpecification Prompt message “Enter you name: ”Prompt message “Enter you name: ” ““EK” EK” “Hello, instructor EK.” “Hello, instructor EK.” Exit Exit

programprogram ““Peter” Peter” “Want to exit program? (Y/N)”“Want to exit program? (Y/N)”

Y Y Exit programExit program

N N “Whatever, I’m terminating”“Whatever, I’m terminating”

Exit programExit program Any Names elseAny Names else “Hello, “Hello, NameName”” Exit Exit

programprogram

Page 12: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Practice 3Practice 3

SpecificationSpecification Prompt “How many times you want me Prompt “How many times you want me

to write?”to write?” Get an input (Get an input (NN)) NN has to be more than or equal to 10 has to be more than or equal to 10

If not If not PrintPrint “Input has to be >= 10” “Input has to be >= 10”

Prompt the MSG and get Prompt the MSG and get new N new N

If yes If yes ContinueContinue

Page 13: Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm

Practice 3 (Cont.)Practice 3 (Cont.) PrintPrint

““11. I will not bully anyone in class again.”. I will not bully anyone in class again.”““22. I will not bully anyone in class again.”. I will not bully anyone in class again.”““33. I will not bully anyone in class again.”. I will not bully anyone in class again.”……… … ““(N)(N).. I will not bully anyone in class I will not bully anyone in class

again.”again.”

Try: Try: Write the new msg on a new line and on the Write the new msg on a new line and on the

same linesame line