Tải bản đầy đủ (.ppt) (18 trang)

C++ lecture 5

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (247.35 KB, 18 trang )

C++ Programming
Lecture 5

Control Structure I
(Selection)
– Part
I
The Hashemite
University
Computer Engineering
Department
(Adapted from the textbook slides)


Outline


Introduction.



if statement.



if/else statement.



Nested if/else statements.




Examples.

The Hashemite University

2


Control Structures


Sequential execution




Transfer of control




Statements executed one after the other in the order
written
When the next statement executed is not the next one
in sequence

All programs written in terms of 3
control structures:



Sequence structure




Selection structures




Built into C++. Programs executed sequentially by default.
C++ has three types - if, if/else, and switch

Repetition structures


C++ has three types - while, do/while, and for
The Hashemite University

3


Control structure example


Sequence
structure




int main()



{





i

cout<return 0;





int i=2,j=10;
int z= (++j)*--

}



Selection
structure




Repetition
structure



int main()



int main()



{



{




int grade=0;

cout<<“enter
grade”;

cin>>grade;


if(grade>90)

{

cout<<“high

grade”;

}

}
else

{
cout<<“low grade”;
} The Hashemite University
}
















int grade=0;
cout<<“enter grade”;
cin>>grade;
while (grade<50)
{
grade +=10;
}
cout<
4


Combination of Control
Structures


Two types of control structures
combination:




Control structure stacking: use them
one after the other.
Control structure nesting: use one
control structure inside the body of
another control structure.


The Hashemite University

5


Stacking vs. Nesting




Stacking control
structure
int main()

























int grade=0;
cout<<“enter grade”;
cin>>grade;
if(grade>90)
{
cout<<“high grade”;
}
else
{
cout<<“low grade”;
}







{

















Nesting control structure
int main()
{
int grade=0;
cout<<“enter grade”;
cin>>grade;
if (grade<90)
{
if (grade<60)
{
if(grade<50)
{
cout<<“fail”;
}
else
{
cout<<“pass”;

}
}
}
}

}
The Hashemite University

6


The if Selection Structure
I


Selection structure



used to choose among alternative courses of action
Pseudocode example:
If student’s grade is greater than or equal to 60
Display “Passed”



If the condition is true





If the condition is false




print statement executed and program goes on to next
statement
print statement is ignored and the program goes onto
the next statement

Indenting makes programs easier to read


C++ ignores white-space characters
The Hashemite University

7


The if Selection Structure II


Translation of pseudocode statement
into C++:
if ( grade >= 60 )
cout << "Passed";




Diamond symbol (decision symbol)



indicates decision is to be made
Contains an expression that can be true or false.




Test the condition, follow appropriate path

if structure is a singleentry/single-exit structure
The Hashemite University

8


The if Selection Structure III


Flowchart (or UML activity
diagram) of pseudocode statement
A decision can be made on
any expression.

grade >=
60

true


print “Passed”

zero - false
nonzero - true
Example:

false

3 - 4 is true

The Hashemite University

9


The if/else Selection Structure
I


if




if/else





Only performs an action if the condition is true (single
selection structure)
A different action is performed when condition is true and
when condition is false (double selection structure).

Psuedocode

if student’s grade is greater than or equal to 60
Display “Passed”
else
Display “Failed”



C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
The Hashemite University

10


The if/else Selection Structure
II
false

grade >= 60


display “Failed”



display “Passed”

Ternary conditional operator (?:) (the only C++ ternary
operator)




true

Takes three arguments (condition, value if true, value if false)

Our C++ code could be written as:
cout << ( grade >= 60 ? “Passed” : “Failed” );//omitting ( ) is syntax error
OR
grade >= 60 ? cout << “Passed” : cout << “Failed”;



Remember that the precedence of ?: is low, so do not forget
the parenthesis that is used
to force
its priority.
The Hashemite
University


11


Nested if/else Structure I


Nested if/else structures


Test for multiple cases by placing if/else selection
structures inside if/else selection structures.
if student’s grade is greater than or equal to 90
Display “A”
else
if student’s grade is greater than or equal to 80
Display“B”
else
if student’s grade is greater than or equal to 70
Display “C”
else
if student’s grade is greater than or equal to 60
Display “D”
else
Display“F”





Once a condition is met, the rest of the statements are

skipped

The above code can
be rewritten using else if
The Hashemite University
as in the next slide

12


Nested if/else Structure II
if student’s grade is greater than or equal to 90
Display “A”
else if student’s grade is greater than or equal to 80
Display “B”
else if student’s grade is greater than or equal to 70
Display“C”
else if student’s grade is greater than or equal to 60
Display “D”
else
Display“F”


Pay attention to the indentation
level and how it is differed from
the previous slide.
The Hashemite University

13



The if/else Body I






The body of if or else is the
statement that will be implemented
after if or else.
If no braces after if or else then the
body will be only the first statement
after them.
If there are braces, then the body
will be the compound statement.

The Hashemite University

14


The if/else Body II


Compound statement:
 Set of statements within a pair of braces
 Example:
if ( grade
cout <<

else {
cout <<
cout <<
}


>= 60 )
"Passed.\n";
"Failed.\n";
"You must take this course again.\n";

Without the braces,
cout << "You must take this course again.\n";



would be automatically executed and the
body of the else will be the first statement
after it only.
Block of code
 Compound statements with declarations
The Hashemite University

15


The if/else Body III







Every if/else or if is considered as one statement with
their bodies.
 How??
Placing lines of codes between else and the body of its
If( grade<50)
if is syntax error.

if (grade > 60)
cout<<"b";
cout<<"hello"; // will produce a syntax error
else
cout<<"c";

{ cout<<“F”;
cout<<“Fail”;
}
cout<<“you fail”; // syntax error
else
Cout<<“pass”;






Leaving the parenthesis after the if empty (you
have not put an expression for the condition)

is syntax error.
if () // no condition  syntax error
The Hashemite University
cout<<"b";

16


Examples


On board


Grading system

The Hashemite University

17


Additional Notes


This lecture covers the following
material from the textbook:


Fourth Edition:



Chapter 2: Sections 2.4, 2.5, 2.6

The Hashemite University

18



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×