Presentation is loading. Please wait.

Presentation is loading. Please wait.

Weeks – 02/ 1 Introduction to flowcharts symbols for selection and looping process.

Similar presentations


Presentation on theme: "Weeks – 02/ 1 Introduction to flowcharts symbols for selection and looping process."— Presentation transcript:

1 Weeks – 02/ 1 Introduction to flowcharts symbols for selection and looping process

2 1 Objectives Knows and understand about another kind of flowcharts 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)

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

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

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

6 5 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

7 6 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 students grade >= to 60 then Output Passed

8 7 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 );

9 8 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.

10 9 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 students grade is greater than or equal to 60 then Output Passed else Output Failed EndIF Example ( Flowchart) truefalse outputFailedoutput Passed grade >= 60

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

12 11 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.

13 12 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 begin writeln('Sorry, you guessed wrong.'); writeln('Better luck next time') end; {semi-colon depends on next keyword }

14 13 Nested if : 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): I f students grade is greater than or equal to 90 then output A else If students grade is greater than or equal to 80 then output B else If students grade is greater than or equal to 70 then output C else If students grade is greater than or equal to 60 then output D else output F

15 14 Flowchart Nested IF

16 15 BOOLEAN OPERATORS X AND Y X Y TTT TFF FTF FFF X OR Y X Y TTT TFT FTT FFF NOT X X TF FT A boolean operator takes boolean values as its operands and returns a boolean value. The three boolean operators are

17 16 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

18 17 Answer to flowchart

19 18 EXERCISE-1 1. Which of the following is an invalid Pascal relational operator a. == b. <> c. < d. > 2. 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"

20 19 EXERCISE-2 3. 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 4. 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"

21 20 Exercise-3 5. 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 6. Write a Pascal statement which declares a constant called MAXSIZE with a value of Write a Pascal statement which will display the value of the real variable degrees using a fieldwidth of 5 with three decimal places

22 21 Exercise-4 8. Write all of above exercise 2 to 6 in pseudocode 9. What is the output of X and Y : IF (X 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

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

24 23 Answers-1 1. Which of the following is an invalid Pascal relational operator : == 2. 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');

25 24 Answers-2 3. 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 );

26 25 Answers-3 4. 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'); 5. 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');

27 26 Answers-4 6. Write a Pascal statement which declares a constant called MAXSIZE with a value of 80 const MAXSIZE = 80; 7. 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 );

28 27 Answer-5 No.8 Exercise 2 : 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');

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

30 29 Answer-7 a. X 3+4*5+(73 mod 9) + (53 div 9) : =32 b. If A > 10 Then A A-5 EndIf A A + 3 Answer : A= 10 A> 10 is false so the value is A = 13


Download ppt "Weeks – 02/ 1 Introduction to flowcharts symbols for selection and looping process."

Similar presentations


Ads by Google