# Objectives Knows and understand about another kind of flowchart’s symbols (looping) Have ability to create structured solution on simple problem(s) Have.

## Presentation on theme: "Objectives Knows and understand about another kind of flowchart’s symbols (looping) Have ability to create structured solution on simple problem(s) Have."— Presentation transcript:

Algorithm and Programming IS-1113
Weeks – 02/1 Introduction to flowchart’s symbols for selection and looping process

Objectives Knows and understand about another kind of flowchart’s symbols (looping) Have ability to create structured solution on simple problem(s) Have ability to create structured solution using pseudo-code on simple problem(s) 1

Control Structure with flowchart
1. Simple sequence 2. Selection IF-THEN 3. Selcetion IF-THEN-ELSE Y N

4a. Nested IF 4b. CASE Structure conditioni ? Y N

No Yes Yes No 5. FOR structure 6. WHILE Structure For
7. REPEAT- UNTIL Structure No Yes

MAKING DECISIONS Most programs need to make decisions. There are several statements available in the Programming language for this. The IF statement is one of the them. The RELATIONAL OPERATORS, listed below, allow the programmer to test various variables against other variables or values. = Equal to > Greater than < Less than <> Not equal to <= Less than or equal to >= Greater than or equal to

Selection Control [1] (if)
IF Statement : if condition_is_true then execute_this_program_statement; Example-1 (Flowchart): true false grade >= 60 Output “Passed” Pseudocode : If student’s grade >= to 60 then Output “Passed”

Selection Control [2] (if)
condition true Statement Output will execute condition false If the condition is not true, then the program statement following the keyword then will be ignored. Statement Output will not execute then next statement Pascal Statements: if ( grade >= 60 ) then Writeln( ‘Passed’ );

Selection Control [3] (if)
Example-2 (PASCAL) program IF_DEMO; {Program demonstrating IF THEN statement} var number, guess : integer; begin number := 2; writeln('Guess a number between 1 and 10'); readln( guess ); if number = guess then writeln('You guessed correctly. Good on you!'); if number <> guess then writeln('Sorry, you guessed wrong.') end.

Selection Control [4] (if-then-else)
Statement if-then-else: The IF statement can also include an ELSE statement, which specifies the statement (or block or group of statements) to be executed when the condition associated with the IF statement is false. Example in pseudocode: If student’s grade is greater than or equal to then Output “Passed” else Output “Failed” EndIF Example ( Flowchart) true false output“Failed” output “Passed” grade >= 60

Selection Control [5] (if…else)
Pascal : if ( grade >= 60 ) then Writeln( ‘Passed’) else Writeln(‘Failed’);

Selection Control [6] (if…else)
Example-2 : { Program example demonstrating IF THEN ELSE statement } program IF_ELSE_DEMO; var number, guess : integer; begin number := 2; writeln('Guess a number between 1 and 10'); readln( guess ); if number = guess then writeln('You guessed correctly. Good on you!') else writeln('Sorry, you guessed wrong.') end.

Example-3 There are times when you want to execute more than one statement when a condition is true (or false for that matter). Pascal makes provison for this by allowing you to group blocks of code together by the use of the begin and end keywords. Consider the following portion of code, if number = guess then begin writeln('You guessed correctly. Good on you!'); writeln('It may have been a lucky guess though') end {no semi-colon if followed by an else } else writeln('Sorry, you guessed wrong.'); writeln('Better luck next time') end; {semi-colon depends on next keyword }

Nested if : Example (pseudocode):
The then and else block of an if statement can contain any valid statements, including other if statements. An if statement containing another if statement is called a nested-if statement. Example (pseudocode): If student’s grade is greater than or equal to 90 then output “A” else If student’s grade is greater than or equal to then output “B” else If student’s grade is greater than or equal to then output “C” else If student’s grade is greater than or equal to then output “D” else output “F”

Flowchart Nested IF

BOOLEAN OPERATORS A boolean operator takes boolean values as its operands and returns a boolean value. The three boolean operators are X Y X AND Y T F X AND Y NOT X X NOT X T F X Y X OR Y T F X OR Y

Case Study Pemimpin sebuah perusahaan otomotif perlu menentukan besarnya bonus yang akan diberikan kepada para pegawainya yang bekerja sebagai account executive. Jika terdapat pegawai yang dalam bulan ini telah menjual mobil lebih dari dua unit, maka akan mendapatkan bonus sebesar Rp ,- kemudian pegawai yang bisa menjual mobil tepat dua buah maka, akan mendapatkan bonus Rp ,- namun jika pegawai yang dalam bulan ini penjualannya kurang dari dua unit maka, pegawai tersebut tidak mendapatkan bonus. Create flowchart

EXERCISE-1 Which of the following is an invalid Pascal relational operator a. == b. <> c. < d. > Write a Pascal statement which compares the integer variable sum to the constant value 10, and if it is the same prints the string "Good guess"

EXERCISE-2 Write a Pascal statement which compares the character variable letter to the character variable chinput, and if it is not the same, prints the value of letter Write a Pascal statement to compare the character variable letter to the character constant 'A', and if less, prints the text string "Too low", otherwise print the text string "Too high"

Exercise-3 Write a Pascal statement to display the text string "Valve open", if the variable waterflow is equal to 1, AND the variable outputvalue is equal to 0 Write a Pascal statement which declares a constant called MAXSIZE with a value of 80 Write a Pascal statement which will display the value of the real variable degrees using a fieldwidth of 5 with three decimal places

Exercise-4 Write all of above exercise 2 to 6 in pseudocode
What is the output of X and Y : IF (X < 10) AND (Y>0) THEN X  X*(X+1) ELSE X  X*X-1 ENDIF Y Y+1+X Suppose the value of X=11 and Y = 4

Exercise-5 X  3+4*5+(73 mod 9) + (53 div 9)
What is the output of the following code : X  3+4*5+(73 mod 9) + (53 div 9) What is the value A now, when A=10 ? If A > 10 Then A A-5 EndIf A  A + 3

Answers-1 Which of the following is an invalid Pascal relational operator : == Write a Pascal statement which compares the integer variable sum to the constant value 10, and if it is the same prints the string "Good guess" if sum = 10 then writeln('Good guess');

Answers-2 Write a Pascal statement which compares the character variable letter to the character variable chinput, and if it is not the same, prints the value of letter if letter <> chinput then writeln( letter );

Answers-3 Write a Pascal statement to compare the character variable letter to the character constant 'A', and if less, prints the text string "Too low", otherwise print the text string "Too high" if letter < 'A' then writeln('Too low') else writeln('Too high'); Write a Pascal statement to display the text string "Valve open", if the variable waterflow is equal to 1, AND the variable outputvalue is equal to 0 if (waterflow = 1) AND (outputvalue = 0) then writeln('Valve open');

Answers-4 Write a Pascal statement which declares a constant called MAXSIZE with a value of 80 const MAXSIZE = 80; Write a Pascal statement which will display the value of the real variable degrees using a fieldwidth of 5 with three decimal places writeln('Degrees = ', degrees:5:3 );

Answer-5 No.8 Exercise 2 : Exercise 3 : Exercise 4 : if sum = 10
then Output('Good guess') Exercise 3 : if letter <> chinput then Output(letter) Exercise 4 : if letter < 'A' then output('Too low') else output('Too high');

Answer-6 No.8 MAXSIZE  80 No.9 Exercise 5 : Exercise 6 :
if (waterflow = 1) AND (outputvalue = 0) then Output(‘Valve open') Exercise 6 : MAXSIZE  80 No.9 IF (X < 10) AND (Y>0) False AND True  F THEN X  X*(X+1) ELSE X  X*X-1 X= 11*11-1=120 ENDIF Y= =125 Y Y+1+X Suppose the value of X=11 and Y = 4

Answer-7 X  3+4*5+(73 mod 9) + (53 div 9) : 3+20+1+8=32
If A > 10 Then A A-5 EndIf A  A + 3 Answer : A= 10  A> 10 is false so the value is A = 13

Similar presentations