Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 3
Hung-Yu Wei
Department of Electrical Engineering
National Taiwan University
10/03/2006
HW#1
Deadline 10/17 12:00 (noon)
Assignment:
Pick any topic you like
Use whatever you have learned
Input/output
Arithmetic
If statement
Submission:
Email to: ntu.cpp.hw@gmail.com
Subject: [HW#1] b95xxxxxx_Name
Attach C++ source codes
b95xxxxxx_name.cpp (only 1 file)
1-2 pages of descriptions/results (Word or txt file)
Write your 姓名學號 in description file and source codes
Make sure your source codes can be complied in PC room 305
10/18 Lab: you will show your program to TAs
2
Review: compiling environment
Visual C++ 6.0 (we used in the lab)
New Project
Win32 console program
(an empty project)
Add cpp file to the project
Visual C++ .Net 2003
專案 Win32 主控台專案
在”應用程式設定”中,勾選”空專案”
Dev C++
New project Console applications
3
Review Question I
X=3;
Y=7;
Z=X==Y
What are the values of X, Y, Z?
4
Review Question II
X=3;
Y=7;
Z1=Y/X
Z2=Y%X
What are the values of X, Y, Z1,Z2?
5
What will we learn today
Chapter 4
Control statement
How do you control the “flow” of your
program?
Selection
Repetition
If
If … else
while
More about UML
6
Introduction: solving problem and
writing program
Before writing a program
Have a thorough understanding of
problem
Carefully plan your approach for solving it
While writing a program
Know what “building blocks” are available
Use good programming principles
7
Algorithms
Computing problems
Solved by executing a series of actions in a
specific order
Algorithm is a procedure determining
Actions to be executed
Order to be executed
Example: cooking/recipe
Program control
Specifies the order in which statements are
executed
8
Pseudocode (fake code)
Pseudocode
Artificial, informal language used to
develop algorithms
Similar to plain 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 (optional)
Help you to analyze/design/program
9
Goto statement
It is BAD!
Jumping between the lines is
unstructured
Structured programming
Avoid using goto statement
Also known as “goto elimination”
C is a structured language
10
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
Repetition structures
while, do/while, for
11
Flowchart
Graphical representation of an
algorithm BEGIN
ACTION1
ACTION2
END 12
Types of statement
Sequence statement
Selection statement
Single selection statement: If
Double selection statement: If … else
Multiple selection statement: switch
Repetition statement
Also known as loops or looping statement
While
Do … while
For
13
If statement (revisited)
Pseudocode
If student’s grade is greater than or equal to 60
Print “Passed”
C++
if (grade >= 60)
cout << “Passed”;
14
UML
Unified Modeling Language (UML)
An industry standard for modeling
software systems
Useful for object-oriented
programming
15
Sequence-structure activity diagram
Initial state
Action state
Final state
16
If single-selection statement
activity diagram
Decision symbol
Guard condition
17
if...else double-selection
Pseudocodes
If student’s grade is greater than or equal to 60
Print “passed”
else
Print “Failed”
C++ codes
if (grade >= 60)
cout << “Passed”;
else
cout << “Failed”;
18
if...else double-selection
statement activity diagram.
19
?:
Conditional Operator
Example
Cout << (grade >=60 ? “Passed” :
“Failed” );
Grade >=60 ? cout << “Passed” : cout
<< “Failed”;
Usage is similar to if … else
20
?:
Syntax
(condition) ? (if it’s true) : (if it’s false)
21
Nested if … else statement
Pseudocodes
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”
22
C++
if (studentGrade >=90)
cout <<"A";
else
if (studentGrade >=80)
cout <<"B";
else
if (studentGrade >=70)
cout <<"C";
else
if (studentGrade >=60)
cout <<"D";
else
cout <<"F";
23
Continued
C++
if (studentGrade >=90)
cout << “A”;
else if (studentGrade >=80)
cout << “B”;
else if (studentGrade >=70)
cout << “C”;
else if (studentGrade >=60)
cout << “D”;
else
cout << “F”;
if
else if
else 24
Some tips
A nested if...else statement can
perform much faster than a series of
single-selection if statements
In a nested if... else statement, test
the conditions that are more likely to
be true at the beginning of the nested
if...else statement.
25
Dangling-else problem
An example
If (x>5)
if (y>5)
cout << “x,y are greater than 5”;
else
cout << “x is <= 5”;
26
Continued
The reality (it is confusing)
Use {} to make your program clear
If (x>5)
if (y>5)
cout << “x,y are greater than 5”;
else
cout << “x is <= 5”;
27
{}
Use {} for clarity and multiple lines of
statement in if…else
If (x>5)
{
if (y>5)
cout << “x,y are greater than 5”;
}
else
cout << “x is <= 5”;
28
Tips
Always putting the braces in an
if...else statement (or any control
statement) helps prevent their
accidental omission, especially when
adding statements to an if or else
clause at a later time.
You could put {} before writing any
statements within {}
29
Review: if..else and {}
One statement
If (condition)
one statement;
else
another statement;
Multiple statements
if (condition)
{
multiple statements;
}
Else
{
more statements;
}
30
While
A repetition statement
Pseudocodes
While there are more items on my shopping list
Buy the next item and update the list
Syntax
while (condition)
statement to do;
31
An example with while
N
Example: compute a value which is 3
and is greater than 100
32
while repetition statement UML
activity diagram
UML uses the same icon for merge
symbol and decision symbol
33
Be careful with while
infinite loop
Repetition statement never terminates
This can make a program appear to
“hang” or “freeze” if the loop body
does not contain statements that
interact with the user.
Use while loop carefully
34
What have we learned so far?
Basic C++ program
Main program
Include C++ standard library (e.g.
iostream)
I/O (cin/cout)
Arithmetic
Control statement
Create your program from pseudocodes to C++
If, If…else, While
35
Conclusion
Read your textbook
2.1~2.7
4.1~4.7
1.7~1.10, 1.13
Install/use your C++ compiler
HW #1
10/17 12:00 (noon)
We will come back to Chapter 3 next week
and continue from 4.8 after finishing
Chapter 3.
36