unit 2 control structures if else for while
play

UNIT 2: CONTROL STRUCTURES. IF ELSE, FOR, WHILE TEXT LOGISTICS - PowerPoint PPT Presentation

CS103L SPRING 2017 UNIT 2: CONTROL STRUCTURES. IF ELSE, FOR, WHILE TEXT LOGISTICS Lab #3 due Friday - must go if you havent checked it off yet HW#2 due Thursday - note updated due dates TEXT LEARNING OBJECTIVES Understand


  1. CS103L SPRING 2017 UNIT 2: CONTROL STRUCTURES. IF… ELSE, FOR, WHILE

  2. TEXT LOGISTICS ▸ Lab #3 due Friday - must go if you haven’t checked it off yet ▸ HW#2 due Thursday - note updated due dates

  3. TEXT LEARNING OBJECTIVES ▸ Understand control flow statements: ▸ if, else-if, else ▸ while, do-while ▸ for

  4. TEXT QUICK WARMUP ▸ Write a program to ask the user to enter two integers representing hours then minutes ▸ In class exercises ▸ http://bits.usc.edu/cs103/in-class-exercises/in-class-exercises-set1/ ▸ printseconds ▸ Skeleton code including variables is already started

  5. TEXT COMPARISONS AND LOGICAL OPERATORS ▸ Often we want to check if a condition is true or false ▸ Is x == 100? Is y < 35.5? Is x > z? ▸ Control flow statements require conditions ▸ In C/C++ the integer value 0 means ‘false’. Any value other than 0 is ‘true’ ▸ Have bool type and true/false constants to help ▸ Behind the scenes true == 1, false == 0

  6. TEXT CONDITIONS ▸ Conditions usually arrive from comparisons and logical operators ▸ Comparisons: ▸ ==, !=, >, <, >=, <= ▸ Equals to, not equals, greater than, less than, greater than or equal, less than or equal

  7. TEXT • • LOGICAL OPERATORS • • • ▸ Combine the result of expressions ‘logically’ – – ( ( !x ) || (y && ( !z ) ) ) … if x=100, y= 3, z=0 then this expression is… ▸ Logical AND: && operator expr_1 && expr_2 • – ▸ x == 1 && y <= 2 – A B AND A B OR A NOT ▸ Logical OR: || operator expr_1 || expr_2 False False False False False False False True False True False False True True True False ▸ x >= 5 || x < 0 True False False True False True True True True True True True ▸ Logical NOT: ! operator !expr_1 ▸ !(x >= 5 || x < 0) ▸ !x && y < 0 ▸ Operator precedence: ! then && then ||

  8. TEXT LOGICAL OPERATOR PRACTICE ▸ x=100;y=-3;z=0 ▸ !x || y && !z ▸ With parens: ((!x) || ( y && (!z)))

  9. TEXT LOGICAL OPERATOR PRACTICE ▸ Which of the following does *not* check if an integer x is in the range [-1 to 5] (inclusive) ▸ x >= -1 && x <=5 ▸ -1 <= x <= 5 ▸ !(x < -1 || x > 5) ▸ x > -2 && x < 6

  10. TEXT IF STATEMENTS ▸ Controlling the flow of your code

  11. TEXT CONTROL STATEMENT #1: IF…ELSE IF…ELSE ▸ Used to control what code executes based on one or more conditions if ( condition1 ) ▸ Form 1: basic if statement { // executed if condition1 is True } ▸ else block is optional else True False { condition // executed if neither condition // above is True } // following statements If Block Else Statements Block Statements Following statements

  12. TEXT If…Else If…Else IF STATEMENT • if ( condition1 ) ▸ Use if to execute only parts of your code { // executed if condition1 is True } else if ( condition2 ) ▸ else if is optional, can have as many as you • { need // executed if condition2 is True – // but condition1 was False } else if ( condition3 ) ▸ final else is also optional, will execute if *none* { • of the conditions are true // executed if condition3 is True // but condition1 and condition2 • { … } indicate code // were False } ▸ Use {} brackets to associate code with the else condition { // executed if neither condition // above is True }

  13. TEXT FULL IF STATEMENT FLOW CHART if ( condition1 ) { // executed if condition1 is True True False } condition else if ( condition2 ) 1 { // executed if condition2 is True True False cond2 // but condition1 was False condition } else { Else If Else If Block // executed if neither condition If Block Else Statements Statements Statements Block Statements // above is True Statements } // following statements Following statements Following Statements

  14. TEXT IN CLASS EXCERCISES ▸ discount ▸ weekday ▸ N-th

  15. TEXT STYLE NOTES ▸ Code “style” ▸ What do we mean by that? ▸ Often there are many, many different ways to write even simple code ▸ Try to choose version that is clear, shows intent

  16. TEXT STYLE QUESTION ▸ Which is better? ▸ Why? int x; int x; cin >> x; cin >> x; if( x >= 0) { if( x >= 0) { cout << “Positive”; cout << “Positive”; } } if( x < 0 ) { else { cout << “Negative”; cout << “Negative”; } }

  17. TEXT COMMON BUG ▸ What’s wrong here? int x; cin >> x; if( x = 1) { cout << “x is 1!” << endl; } else { cout << “x is not 1.” << endl; }

  18. TEXT = VS == ▸ Common mistake to use = (assignment) instead of == (equals to) ▸ The = operator returns the value assigned, so ( x = 1) returns 1 ▸ if( x = 1) will always get 1 → true int x; ▸ if() block will always execute! cin >> x; if( x = 1) { cout << “x is 1!” << endl; } else { cout << “x is not 1.” << endl; }

  19. TEXT = VS == FIXED ▸ Fixed code int x; cin >> x; if( x == 1) { cout << “x is 1!” << endl; } else { cout << “x is not 1.” << endl; }

  20. TEXT ? OPERATOR ▸ Often we find ourselves writing code of the form: if (x>0) { z = 2; } else { z = 1; } ▸ ? is a short cut: z = x > 0 ? 2 : 1; ▸ Syntax: condition ? expr_if_true : expr_if_false;

  21. TEXT LOOPS ▸ Doing something more than once

  22. TEXT for Repetition #include <iostream> WHY DO WE NEED LOOPS? • using namespace std; t a int main() se { • ▸ Almost *all* programs have at least one loop… cout << 1 << endl; cout << 2 << endl; ... – 100 cout << 100 << endl; ▸ Print out numbers 1 - 100 return 0; – } – ▸ Play a game of rounds until there is a winner – • Imagine the game of 'war'…it ▸ Can we do this without loops? #include <iostream> Assume this produces • • Imagine the game of 'war'…it a true/false result using namespace std; indicating if the game is over after without loops, but… int main() ▸ Maybe? performing a turn { • bool gameOver; gameOver = take_turn(); without loops, but… if( ! gameOver ){ gameOver = take_turn(); if( ! gameOver ) { ... { } }

  23. TEXT LOOP TYPE #1: WHILE LOOP ▸ While loop ▸ Do something while a condition is true ▸ Two forms: ▸ while(condition){ //something } ▸ do { //something } while(condition)

  24. TEXT WHILE LOOP • ▸ condition is evaluated first // While Type 1: – while ( condition ) { ▸ If condition is true, body is executed – // code to be repeated // (should update condition) ▸ After body, condition is checked again } • ▸ Body *should* update condition – – ▸ Why? – ▸ How many times will body execute?

  25. TEXT • – DO-WHILE LOOP – ▸ Body is executed • ▸ Then condition is checked, if true, body is executed again – // While Type 2: – ▸ Body should update condition do { – // code to be repeated ▸ How many times will body execute? // (should update condition) } while ( condition );

  26. TEXT WHILE LOOP = REPEATING IF // guessing game bool guessedCorrect = false; • if( !guessedCorrect ) { guessedCorrect = guessAgain(); ▸ While loop is like a repeating if statement } t // want to repeat if cond. check again if( !guessedCorrect ) • ▸ If your description of the problem (algorithm) { guessedCorrect = guessAgain(); } // want to repeat if cond. check again contains: An if-statement will only execute once ▸ “until <xxx> is true” ' // guessing game bool guessedCorrect = false; ▸ “as long as <yyy> is bigger than <x> while( !guessedCorrect ) { guessedCorrect = guessAgain(); } ▸ “while the player’s guess is wrong” A 'while' loop acts as a repeating 'if' ▸ Then you need a while loop statement

  27. TEXT IN CLASS EXERCISES ▸ countodd

  28. TEXT FOR LOOP ▸ Very common looping structure ▸ Anatomy: conditional expression or Loop Initialization statement update statement for(init stmt; cond; update stmt) { • // body of loop } • • • • Outputs 0 5 10 15 … •

  29. TEXT FOR LOOP or Loop ▸ Initialization statement: for(init stmt; cond; update stmt) ▸ Any valid statement, executed first { • // body of loop ▸ Conditional Expression: } • ▸ Expression evaluated next • ▸ Body: • ▸ Executed if conditional expression is true • Outputs 0 5 10 15 … ▸ Update statement: • ▸ Any valid statement, executed after body ▸ Condition is checked again after Update and body is executed if true… until condition is false

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend