Introduction to Programming Lecture 3 - Selection Statements PDF
Document Details
Uploaded by LargeCapacitySnowflakeObsidian2219
Astana IT University
Askar Khaimuldin
Tags
Summary
This is an introduction to programming lecture focusing on selection statements in C++. The lecture covers sequential execution, if statements, if-else statements, nested if statements, switch statements, and ternary operators. It is presented as lecture notes for students from ASTANA IT UNIVERSITY.
Full Transcript
Introduction to Programming Lecture 3 : Selection statements Askar Khaimuldin Senior-lecturer [email protected] Content Sequential execution if statement if-else statement Nested if statements s...
Introduction to Programming Lecture 3 : Selection statements Askar Khaimuldin Senior-lecturer [email protected] Content Sequential execution if statement if-else statement Nested if statements switch statement Ternary operator (?:) Sequential execution Every statement is executed one by one in the order 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 if statement if statements are used to provide a case when 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 if-else statement if-else statements are used to have exactly two 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 Nested if statements Testing several cases by placing if…else statements 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 if statements (Continued) In the chain of “if-else-if” statements only one 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 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 switch statement It is not allowed to provide the same value for multiple cases 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 Ternary operator (?:) This will give a value for finalGrade It is used in order not to exceed 100 Literature Deitel P.J. and Deitel H.M. 2017. C++ How to Program, 10th global edition (Chapter 4: p.146 - 154 ) Herbert Schildt. 2003. The Complete Reference C++, 4th edition (Chapter 3: p.93 - 104) Good luck