Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.

Similar presentations


Presentation on theme: "Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary."— Presentation transcript:

1 Chapter 3

2 Outline Relational Operators Loops Decisions Logical Operators Precedence Summary

3 Relational Operators Relational operators compare two values Equal to, less than, greater than Type bool is hold one value true or false Bool is used in loops and decision to influence what the program will do next Example Jane = 44, harry = 12 (jane == harry) // false (harry <= 12) ? (jane > harry) ? (harry != 12) ? (0) ? (44)?

4 C++ relational operators OperatorMeaning >Greater than <Less than ==Equal to !=Not equal to >=Greater than or equal to <=Less than or equal to

5 Loops The for Loop for loop executes a section of code a fixed number of times Syntax 1- for (initialization; condition; increase) statement; 2- for (initialization; condition; increase) { statement; }

6 Initialization expression Test Expression Body of loop Increment Expression Exit False True For Loop Flow Chart

7 Example #include Int main() { int j; for(j=0; j<15; j++) cout<<j * j<<“ ”; cout<<endl; return 0; }

8 for Loop variation Blocks and variable visibility Variables defined in for statements Multiple initialization and Test expressions For(j = 0, alpha=100; j<50; j++, alpha--) { // body of loop }

9 The While Loop Syntax While (test expression) statement; While (test expression) { statements; }

10 Test Expression Body of loop Exit False True While Loop Flow Chart

11 Example Int main() { int n =99; while (n!=0) cin>>n; cout<<endl; return 0; }

12 The do-while Loop Execute atleast for one time Syntax 1-do statement; While(Test Expression) 2-do { statement; } while(test expression)

13 Test Expression Body of loop Exit False True Do -While Loop Flow Chart

14 Decisions – The if Statement Syntax Example 1-if(test expression) statement; 2-if(test expression) { statement; } Int main() { int x; cout<<“Enter a number”; cin>>x; if(x>100) cout<<“That number is greater than 100\n”; return 0; }

15 The if... else statement Syntax Example 1- If (test expression) statement; else statement; 1- If (test expression) { statement; } else { statement; } if(x>100) cout<<“Number is greater than 100”; else cout<<“Number is not greater than 100”;

16 The else …if Construct Example if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; Else cout << "x is 0";

17 The switch Statement Description Syntax Alternative of else…if construct All decision depend on the value of the same variable Case constant must be an integer of character constant like 3 or ‘a’ else…if construction can use series of expression of unrelated variables Default will execute if no case will be matched Switch (n) { case 1: statement; break; case 2: statement; break; default: statement; } where n is character or integer variable

18 The conditional operator Syntax Result = (test expression) ? Expression 1 : Expression 2 Conditional expression Conditional operator Equivalent to if…else structure if (alpha < beta) min = alpha elsemin = (alpha<beta) ? alpha : beta; min = beta

19 Example int main() { for(int j=0; j<80; j++) { char ch = (j%8) ? ‘ ’: ‘x’ ; // ch is ‘x’ if column is multiple cout<< ch;//of 8, and space ‘ ’ otherwise } return 0; }

20 Logical Operators (&&, ||, !) AND(&&) OPERATOROR(||) OPERATOR Example ( (5 == 5) && (3 > 6) ) // evaluates to false (true && false) ( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ) !(5==5) // evaluates to false !(true)

21 Precedence Summary Operator typeOperatorsPrecedence Unary!, ++, --, +, -Highest ArithmeticMultiplicative *, /, % Additive +, - RelationalInequality, = Equality ==, != LogicalAnd (&&), OR (||) Conditional?: Assignment=, +=, -=, /=, %=Lowest

22 The break Statement Causes an exit from loop Next statement after break is executed is statement following the loop int main () { int n; for (n=10; n>0; n--) { cout << n << ", "; if (n==3) { cout << "countdown aborted!"; break; } return 0; } End of Loop Normal Loop return Condition within loop Break; Output: 10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!

23 The continue statement Continue causes to go back to top of the loop Skip rest of statements in loop int main () { for (int n=10; n>0; n--) { if (n==5) continue; cout << n << ", "; } cout << "FIRE!\n"; return 0; } Condition within loop continue; Normal Loop return Start of Loop Output: 10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!

24 The goto Statement goto allows to make an absolute jump to another point in the program Unconditional jump, ignoring nesting statements The destination point is identified by a label e.g. loop: int main () { int n=10; loop: cout << n << ", "; n--; if (n>0) goto loop; cout << "FIRE!\n"; return 0; } Output: 10, 9, 8, 7, 6,5, 4, 3, 2, 1, FIRE!

25 Exercise Create a program that loops 30 times, but only outputs numbers that are not divisible by 3 or 5. Create a simple calculator with addition, subtraction, multiplication and division operations. Numbers should be entered by user.


Download ppt "Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary."

Similar presentations


Ads by Google