1
Algorithms
• Computing problems
– Solved by executing a series of actions in a specific order
• Algorithm a procedure determining
– Actions to be executed
– Order to be executed
– Example: recipe
• Program control
– Specifies the order in which statements are executed
2003 Prentice Hall, Inc. All rights reserved.
3
Pseudocode
• Pseudocode
– Artificial, informal language used to develop algorithms
– Similar to everyday English
• Not executed on computers
– Used to think out program before coding
• Easy to convert into C++ program
– Only executable statements
• No need to declare variables
– Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”
2003 Prentice Hall, Inc. All rights reserved.
4
Pseudocode
2003 Prentice Hall, Inc. All rights reserved.
6
Flowcha rt
Copyright © 2013 Brian Sandall, University of Nebraska-Omaha
2003 Prentice Hall, Inc. All rights reserved.
7
false true
grade >= 60
print “Failed” print “Passed”
2003 Prentice Hall, Inc. All rights reserved.
12
Control Structures
• Sequential execution
– Statements executed in order
• Transfer of control
– Next statement executed not next one in sequence
• 3 control structures (Bohm and Jacopini)
– Sequence structure
• Programs executed sequentially by default
– Selection structures
• if, if/else, switch
• Single-selection statement (if)
• Double-selection statement (if...else…)
• Multiple-selection statement (switch)
– Repetition structures
• while, do/while, for
2003 Prentice Hall, Inc. All rights reserved.
13
if Selection Structure
• Make decision based on truth or falsity of condition If condition met, body
executed
• Choose among alternative courses of action
– Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”
– If the condition is true
• Print statement executed, program continues to next statement
– If the condition is false
• Print statement ignored, program continues
– C++ code
if ( grade >= 60 )
cout << "Passed";
• Indenting makes programs easier to read
• C++ ignores whitespace characters (tabs, spaces, etc.) but necessary so that programs
easier to read
2003 Prentice Hall, Inc. All rights reserved.
14
if Selection Structure
• Flowchart of pseudocode statement
true
g ra d e >= 60 p rint “ Pa ssed ”
fa lse
2003 Prentice Hall, Inc. All rights reserved.
15
Decision Ma king: Equa lity a nd Rela tiona l
Opera tors
Sta nd a rd a lg eb ra ic C++ eq ua lity Exa m p le Mea ning of
eq ua lity op era tor or or rela tiona l of C++ C++ c ond ition
rela tiona l op era tor op era tor c ond ition
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
>= x >= y x is greater than or equal to y
<= x <= y x is less than or equal to y
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
2003 Prentice Hall, Inc. All rights reserved.
16
Decision Ma king: Equa lity a nd Rela tiona l
Opera tors
2003 Prentice Hall, Inc. All rights reserved.
17
if/else Selection Structure
• if
– Performs action if condition true
• if/else
– Different actions if conditions true or false
• Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
2003 Prentice Hall, Inc. All rights reserved.
18
if/else Selection Structure
• Ternary conditional operator (?:)
– Three arguments (condition, value if true, value if false)
• Code could be written:
cout << ( grade >= 60 ? “Passed” : “Failed” );
Condition Value if true Value if false
false true
grade >= 60
print “Failed” print “Passed”
2003 Prentice Hall, Inc. All rights reserved.
19
if/else Selection Structure
• if…else if… structures
– One inside another, test for multiple cases
– Once condition met, other statements skipped
if student’s grade is greater than or equal to 90
Print “A”
else if student’s grade is greater than or equal to 80
Print “B”
else if student’s grade is greater than or equal to 70
Print “C”
else if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
2003 Prentice Hall, Inc. All rights reserved.
20
if/else Selection Structure
• Example
if ( grade >= 90 ) // 90 and above
cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";
2003 Prentice Hall, Inc. All rights reserved.
21
“if…”
vs.
“if…else if…”
2003 Prentice Hall, Inc. All rights reserved.
22
if vs. if..else if..
if ( num1 == num2 ) if ( num1 == num2 )
cout << num1 << " is equal to " << num2 << endl; cout << num1 << " is equal to " << num2 << endl;
if ( num1 != num2 ) else if ( num1 != num2 )
cout << num1 << " is not equal to " << num2 << endl; cout << num1 << " is not equal to " << num2 << endl;
if ( num1 < num2 ) else if ( num1 < num2 )
cout << num1 << " is less than " << num2 << endl; cout << num1 << " is less than " << num2 << endl;
if ( num1 > num2 ) else if ( num1 > num2 )
cout << num1 << " is greater than " << num2 << endl; cout << num1 << " is greater than " << num2 << endl;
if ( num1 <= num2 ) else if ( num1 <= num2 )
cout << num1 << " is less than or equal to "<< num2 cout << num1 << " is less than or equal to "<< num2
<< endl; << endl;
if ( num1 >= num2 ) else
cout << num1 << " is greater than or equal to "<< cout << num1 << " is greater than or equal to "<< num2 << endl;
num2 << endl;
num1=22
num2=12
output?
2003 Prentice Hall, Inc. All rights reserved.
23
1 // Fig. 1.14: fig01_14.cpp
using statements eliminate Outline
2 // Using if statements, relational
3 // operators, and equality operators.
need for std:: prefix.
4 #include <iostream>
fig01_14.cpp
5 Declare variables.(1 of 2)
6 using std::cout; // program uses cout
7 using std::cin; // program uses cin
8 using std::endl; // program uses endl Can write cout and cin
9 without std:: prefix.
10 // function main begins program execution
11 int main() if structure compares values
12 { of num1 and num2 to test for
13 int num1; // first number to be read from user equality.
14 int num2; // second number to be read from user
15
If condition is true (i.e.,
16 cout << "Enter two integers, and I will tell you\n"
17 << "the relationships they satisfy: ";
values are equal), execute this
18 cin >> num1 >> num2; // read two integers statement.
19
20 if ( num1 == num2 )
21 cout << num1 << " is equal to " << num2 << endl; if structure compares values
22 of num1 and num2 to test for
23 if ( num1 != num2 ) inequality.
24 cout << num1 << " is not equal to " << num2 << endl;
25
If condition is true (i.e.,
values are not equal), execute
2003 Prentice Hall, Inc.
this statement. All rights reserved.
24
26 if ( num1 < num2 )
27 cout << num1 << " is less than " << num2 << endl;
Outline
28
29 if ( num1 > num2 )
fig01_14.cpp
30 cout << num1 << " is greater than " << num2 << endl;
Statements may
(2 of 2) be split over
31
32 if ( num1 <= num2 )
several lines.
33 cout << num1 << " is less than or equal to " fig01_14.cpp
34 << num2 << endl; output (1 of 2)
35
36 if ( num1 >= num2 )
37 cout << num1 << " is greater than or equal to "
38 << num2 << endl;
39
40 return 0; // indicate that program ended successfully
41
42 } // end function main
Enter two integers, and I will tell you
the relationships they satisfy: 22 12
22 is not equal to 12
22 is greater than 12
22 is greater than or equal to 12
2003 Prentice Hall, Inc.
All rights reserved.
25
1 // Fig. 1.14: fig01_14.cpp
2 // Using if statements, relational
Outline
3 // operators, and equality operators.
4 #include <iostream>
fig01_14.cpp
5
6 using std::cout; // program uses cout
(1 of 2)
7 using std::cin; // program uses cin
8 using std::endl; // program uses endl
9
10 // function main begins program execution
11 int main()
12 {
13 int num1; // first number to be read from user
14 int num2; // second number to be read from user
15
16 cout << "Enter two integers, and I will tell you\n"
17 << "the relationships they satisfy: ";
18 cin >> num1 >> num2; // read two integers
19
20 if ( num1 == num2 )
21 cout << num1 << " is equal to " << num2 << endl;
22
23 else if ( num1 != num2 )
24 cout << num1 << " is not equal to " << num2 << endl;
25
2003 Prentice Hall, Inc.
All rights reserved.
26
26 else if ( num1 < num2 )
27 cout << num1 << " is less than " << num2 << endl;
Outline
28
29 else if ( num1 > num2 )
fig01_14.cpp
30 cout << num1 << " is greater than " << num2 << endl;
31
(2 of 2)
32 else if ( num1 <= num2 )
33 cout << num1 << " is less than or equal to " fig01_14.cpp
34 << num2 << endl; output (1 of 2)
35
36 else if ( num1 >= num2 )
37 cout << num1 << " is greater than or equal to "
38 << num2 << endl;
39 else cout << “Error” << endl;
40 return 0; // indicate that program ended successfully
41
42 } // end function main
Enter two integers, and I will tell you
the relationships they satisfy: 22 12
22 is not equal to 12
2003 Prentice Hall, Inc.
All rights reserved.
27
Pra ctice
• Odd and Even number
– User input a number
– Your program inform the user whether the number is an odd
or even number
2003 Prentice Hall, Inc. All rights reserved.
28
if/else Selection Structure
if ( grade >= 60 )
cout << "Passed.\n";
else
{
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
if ( grade >= 60 )
cout << "Passed.\n";
else
cout << "Failed.\n";
cout << "You must take this course again.\n";
2003 Prentice Hall, Inc. All rights reserved.
29
if/else Selection Structure
• Compound statement
– Set of statements within a pair of braces
if ( grade >= 60 )
cout << "Passed.\n";
else
{
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
– Without braces,
cout << "You must take this course again.\n";
always executed
• Block
– Set of statements within braces
2003 Prentice Hall, Inc. All rights reserved.
30
Confusing Equa lity (==) a nd Assignment (=)
Opera tors
• Common error
– Does not typically cause syntax errors
• Example
o if ( payCode == 4 )
o if ( payCode = 4 )
2003 Prentice Hall, Inc. All rights reserved.
31
Confusing Equa lity (==) a nd Assignment (=)
Opera tors
• Example
if ( payCode == 4 )
cout << "You get a bonus!" << endl;
– If paycode is 4, bonus given
• If == was replaced with =
if ( payCode = 4 )
cout << "You get a bonus!" << endl;
– Paycode set to 4 (no matter what it was before)
– Statement is true (since 4 is non-zero)
– Bonus given in every case
2003 Prentice Hall, Inc. All rights reserved.
32
Equa lity Opera tors
• ! (logical NOT, logical negation)
– Returns true when its condition is false, & vice versa
if ( !( grade == sentinelValue ) )
cout << "The next grade is " << grade << endl;
Alternative:
if ( grade != sentinelValue )
cout << "The next grade is " << grade << endl;
2003 Prentice Hall, Inc. All rights reserved.
33
Logica l Opera tors
• Used as conditions in loops, if statements
• && (logical AND)
– true if both conditions are true
if ( gender == 1 && age >= 65 )
++seniorFemales;
• || (logical OR)
– true if either of condition is true
if ( semesterAverage >= 90 || finalExam >= 90 )
cout << "Student grade is A" << endl;
nb:
Dengan logical operator, fungsi “if else “ dapat memiliki lebih dari 1
syarat
2003 Prentice Hall, Inc. All rights reserved.
34
Pra ctice
• Seleksi masuk ruangan (Gender + Umur)
– Female & umur ≤ 50 room A
– Female & umur > 50 room B
– Male & umur ≤ 50 room C
– Male & umur > 50 room D
– Female user input ‘F’ atau ‘f’
– Male user input ‘M’ atau ‘m’
2003 Prentice Hall, Inc. All rights reserved.
35
Pra ctice
• Seleksi penerimaan jurusan Univ.
– Masukkan nilai Matematika, Fisika, Kimia, Biologi, Inggris
– Masukkan jurusan yang diminati (Elektro, Pangan, Industri,
Matematika)
– Memenuhi syarat atau Tidak memenuhi syarat
• Elektro: Mat > 70 dan Fisika > 80
• Pangan: Inggris > 75, Kimia > 80 atau Biologi > 80
• Industri: Mat > 75 atau Fisika > 75, Inggris >70
• Matematika: Mat > 80 atau Fisika >70
2003 Prentice Hall, Inc. All rights reserved.