Weeks – 02/ 1 Introduction to flowcharts symbols for selection and looping process.
-
Upload
melissa-reynolds -
Category
Documents
-
view
220 -
download
0
Transcript of Weeks – 02/ 1 Introduction to flowcharts symbols for selection and looping process.
1
Weeks – 02/1
Introduction to flowchart’s Introduction to flowchart’s symbols for selection and symbols for selection and
looping processlooping process
2
ObjectivesKnows 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)
3
Control Structure with flowchartControl Structure with flowchart1. Simple sequence
2. Selection IF-THEN
3. Selcetion IF-THEN-ELSE
Y
N
N
Y
4
4a. Nested IF
Y
N
NY
N
Y
4b. CASE Structure
conditioni ?
5
5. FOR structure
For
6. WHILE Structure
Yes
No
7. REPEAT- UNTIL Structure
No
Yes
6
MAKING DECISIONSMost 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
Selection Control [1] (if)IF Statement :
if condition_is_true thenexecute_this_program_statement;
Example-1 (Flowchart):
true
false
grade >= 60 Output “Passed”
Pseudocode :If student’s grade >= to 60 then Output “Passed”
8
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
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
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 60
then Output “Passed”else Output “Failed”
EndIF Example ( Flowchart)
truefalse
output“Failed” output “Passed”
grade >= 60
11
Selection Control [5] (if…else)Pascal :
if ( grade >= 60 ) then Writeln( ‘Passed’)else Writeln(‘Failed’);
12
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
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
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):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 80 then output “B”else If student’s grade is greater than or equal to 70 then output “C” else If student’s grade is greater than or equal to 60 then output “D” else output “F”
15
Flowchart Nested IF
16
BOOLEAN OPERATORS
X AND Y
XY
X AND
Y
T T T
T F F
F T F
F F F
X OR Y
XY
X OR Y
T T T
T F T
F T T
F F F
NOT X
XNOT
X
T F
F T
• A boolean operator takes boolean values as its operands and returns a boolean value.
• The three boolean operators are
17
Case StudyPemimpin 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 1.000.000,- kemudian pegawai yang bisa menjual mobil tepat dua buah maka, akan mendapatkan bonus Rp 500.000,- namun jika pegawai yang dalam bulan ini penjualannya kurang dari dua unit maka, pegawai tersebut tidak mendapatkan bonus.
Create flowchart
18
START
END
Jumlah penjualan > 2
unit ?
Input jumlah
penjualan
Jumlah penjualan = 2
unit ?
Bonus = 1.000.000
Bonus = 500.000
Bonus = 0
Ya
Tidak
Ya
Tidak
Output Bonus
Answer to flowchart
19
EXERCISE-11. 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
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
Exercise-35. 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 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
22
Exercise-48. Write all of above exercise 2 to 6 in
pseudocode9. 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+XSuppose the value of X=11 and Y = 4
23
Exercise-510.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 EndIfA A + 3
24
Answers-11. 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
Answers-23. 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
Answers-34. 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
Answers-46. 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
Answer-5No.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
Answer-6No.8
Exercise 5 : if (waterflow = 1) AND (outputvalue = 0)
then Output(‘Valve open')Exercise 6 :
MAXSIZE 80No.9
IF (X < 10) AND (Y>0) False AND True FTHEN X X*(X+1)
ELSE X X*X-1 X= 11*11-1=120 ENDIF Y=4+1+120=125Y Y+1+X
Suppose the value of X=11 and Y = 4
30
Answer-7a. X 3+4*5+(73 mod 9) + (53 div 9) :
3+20+1+8=32
b. If A > 10 Then A A-5 EndIfA A + 3
Answer : A= 10 A> 10 is false so the value is A = 13