Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.

Similar presentations


Presentation on theme: "Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005."— Presentation transcript:

1 Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005

2 Topics  A compound (block) statement  Variable scope  Relational expression  Logical expression  The if statement  The switch statement  The break statement  The conditional expression

3 A compound statement  Consists of zero or more statements enclosed in open/close braces  Are commonly called “blocks”.

4 Compound statements  A program accomplishes several tasks Ex. Recall the program in your homework1 that reads the grades of a student’s assignments and tests from the keyboard, computes WAT and WATA, computes the finalScore of this student, transforms the finalScore to a letter (finalGrade), and displays the finalGrade.

5 Compound statements void main() { double hw1,hw2,hw3,hw4,hw5,hw6,t1,t2,t3; //read inputs from the keyboard for assignments and tests { … } //computes the weighted average of tests (WAT) and WATA double WAT,WATA; { … } //computes the finalScore from WAT and WATA double finalScore; { … } //transforms finalScore to finalGrade---a letter char finalGrade; { … } //display the finalGrade { … }

6 Scope of a variable  Scope of a variable is the block in which it is defined, from the point of definition to the end of the block  Usually defined at beginning of function  May be defined close to first use

7 Still More About Variable Definitions and Scope  Variables defined inside { } have local or block scope void main() { //read inputs from the keyboard for assignments and tests { double hw1,hw2,hw3,hw4,hw5,hw6,t1,t2,t3; … } //computes the weighted average of tests (WAT) and WATA double WAT,WATA; { … } … }

8 Still More About Variable Definitions and Scope  When a block is nested inside another block, a variable defined in the inner block may have the same as a variable defined in the outer block When in inner block, outer definition is not available Should avoid give same names

9 Ex  For any given non-zero integer i, compute the expression 15.0/i, and output the result.

10 Relational Operators  Used to compare numbers to determine relative order  Operators: > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to

11 Relational Expressions  Boolean expressions – true or false  Examples: 12 > 5 is true 7 <= 5 is false if x is 10, then x == 10 is true, x != 8 is true, and x == 8 is false

12 Relational Expressions  Can be assigned to a variable: result = x <= y;  Assigns 0 for false, 1 for true  Do not confuse = and ==

13 Relational Expressions  Type compatibility of relational operators  Precedence of relational operators  Associativity of relational operators priorityOperator 1Arithmetical operator 2 >= 3== != 4= += -= *= /= %=

14 Logical Operators  Used to create relational expressions from other relational expressions  Unary logical operator not: operator meaning explanation ! NOTReverses the value of an expression – true expression becomes false, and false becomes true

15 Logical Operators  Binary logical operator and: operator meaning explanation && AND New relational expression is true if both expressions are true aba && b true false true false

16 Logical Operators  Binary logical operator or: operator meaning explanation || OR New relational expression is true if either expression is true aba || b true false true false

17 Logical Operators - notes  ! has highest precedence, followed by &&, then ||  If the value of an expression can be determined by evaluating just the sub-expression on left side of a logical operator, then the sub- expression on the right side will not be evaluated (short circuit evaluation)

18 Precedence and associativity PriorityOperators 1! 2Arithmetic operators 3Inequality relational > >= < <= 4Equality and inequality == != 5&& 6|| 7Assignment operators


Download ppt "Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005."

Similar presentations


Ads by Google