Похожие презентации:
Lecture 3 - Selection statements
1. Introduction to Programming Lecture 2 : Selection statements
Shyryn TutkyshbayevaPhD, Assistant Professor
sh.tutkyshbayeva@astanait.edu.kz
2. Content
• Sequential execution• if statement
• if-else statement
• Nested if statements
• switch statement
• Ternary operator (?:)
3. Sequential execution
• Every statement is executed one by one in theorder in which they are written.
• This is also called the flow of the activity
• In C++ community the usage of goto which is used
to jump to some other part of code was mostly
rejected. It leads to spaghetti code problem
• In the last example it is clear how the program
is executed.
• Since return statement comes before the last
cout the value of x will never be printed
4. if statement
• if statements are used to provide a casewhen one block of code must be
executed
• Every statement needs to have a
condition provided inside brackets
• Condition is a boolean expression that
evaluates either true or false
• In this example user enters two values
for x and y. Since y is a divisor it cannot
be zero.
• Hint: If y is zero the line 17 will not be
executed, because “return 1” forces to
exit main function with some error status
5. if-else statement
• if-else statements are used to have exactlytwo possible scenarios
• it is guaranteed that one of two blocks will be
executed
• Example: there are minimum values for grade
and attendance (50 and 70 respectively) to
pass a course
• Program informs a student whether he/she
passed or failed
6. Nested if statements
• Testing several cases by placing if…elsestatements inside another if…else statements
• Example: program must output a “Letter Grade”
from given student`s percent grade
• Spacing and
indentation are ignored
by the compiler
• Previous code can be
rewritten in this form
• This form avoids deep
indentation of the code
to the right
7. if statements (Continued)
• In the chain of “if-else-if” statements onlyone block can be executed
• Example: One student has attendance 65
and grade 85 (It is F)
• Line number 18 will not be executed,
since a program will exit that if structure
after executing line 14
• The if statement normally expects only
one statement in its body.
• To include multiple lines enclose the
statements in braces
• Dangling-else Problem
• The last message does not belong to
“else”, and will be printed regardless to if
statement above
8. switch statement
• Multiple-branch selection statement• It is used to evaluate to a character or integer value
(Floating point numbers are not allowed)
• The value of expression is tested for the equality to
constants in the case statement
• Every “case constantN” is followed with a colon (:)
• break is used to exit the switch structure
• When a match is found all statements are executed till it
reaches a break or the end of switch structure
• default (optional) is used to provide some instructions in
case no matches are found
9. switch statement
It is not allowed to provide the samevalue for multiple cases
10. Ternary operator (?:)
• You can use the ternary operator(?) to replace if-else statements of
the general form:
• Both if and else should have a
single expression (use colon for
else)
• It speeds up coding time
• Use brackets where it is needed
• If the ternary operator is used to
extract some value, data types of
both expressions should match
11. Ternary operator (?:)
This will give a valuefor finalGrade
It is used in order
not to exceed 100
12. Literature
• Deitel P.J. and Deitel H.M. 2017. C++ How to Program, 10thglobal edition (Chapter 4: p.146 - 154 )
• Herbert Schildt. 2003. The Complete Reference C++, 4th
edition (Chapter 3: p.93 - 104)