Tutorial 3: Conditionals and Iteration cs202/2009-01/tutorials/tutorial-3/tutorial...¢  Tutorial 3:

  • View
    25

  • Download
    1

Embed Size (px)

Text of Tutorial 3: Conditionals and Iteration cs202/2009-01/tutorials/tutorial-3/tutorial...¢ ...

  • Tutorial 3: Conditionals and Iteration COMP 202: Intro to Computing 1

    TA: Robert Rolnick E-mail: Robert.Rolnick@mail.mcgill.ca

    Winter 2009

    Tutorial 3: Conditionals and Iteration

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

  • Tutorial 3: Conditionals and Iteration

    1.Conditions

    2.Conditionals

    3.Iteration

    4.Practice Programs

    5.Addendum

    Tutorial 3: Conditionals and Iteration

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

  • Tutorial 3: Conditionals and Iteration

     Conditions are expressions that return a boolean (true or false).

     Comparisons and equality tests are the most common conditions. For example (5 < 3) is a condition which will return false.

     Conditions come up frequently when discussing conditional statements, and iteration.

     Strings conditions are done differently than those on fundamental data types such as int, double, etc. An example is coming later.

    Conditions

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

    General Information Comparison Operators Boolean Manipulation

    “The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!' but ‘That's funny...’”

    ~Isaac Asimov

  • Tutorial 3: Conditionals and Iteration

     The following comparison operators are useful to compare fundamental data types (int, float, double, etc.)

    Comparison Operators

    Operator Name Example Returns

    == Equal to 4 == 4 true

    != Not-Equal to 4 != 4 false

    < Less Than 8 < 8 false

    4 true

    >= Greater Than Or Equal 9 >= 10 false

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

    General Information Comparison Operators Boolean Manipulation

    “If debugging is the process of removing software bugs, then programming must be the process of putting them in.”

    ~Edsger Dijkstra

  • Tutorial 3: Conditionals and Iteration

     Although you can compare floating point numbers (floats and doubles) with the == and != operators, it is discouraged.

     This is because floating point round off error.

     This small bit of error may cause two numbers to be non-equal when compared, despite being (theoretically) equal based on the operations performed.

     When comparing floats to a specific value, you should do it with a tolerance. (In other words, allow yourself to be within a very small range of the number you want.)

    Comparison Operators (Continued)

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

    General Information Comparison Operators Boolean Manipulation

  • Tutorial 3: Conditionals and Iteration

     The following operators can be used to compare booleans:

    Boolean Manipulation

    A B A && B A || B A == B A != B

    true true true true true false

    true false false true false true

    false true false true false true

    false false false false true false

     There is also a fifth operator called the xor operator. It uses a ^.  For booleans ^ does the same thing as !=.  The xor operator’s true purpose is beyond the scope of this class.

     &&, and operator  ||, or operator

     ==, equality operator  !=, in-equality operator

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

    General Information Comparison Operators Boolean Manipulation

    “People who deal with bits should expect to get bitten.” ~ Jon Bentley

  • Tutorial 3: Conditionals and Iteration

     Java does short circuiting of the && and || operators.

     Suppose you are given the conditional expression:

    Boolean Manipulation (Continued)

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

    General Information Comparison Operators Boolean Manipulation

    if (/* condition 1 */ && /* condition 2 */)

     If condition 1 is false, then condition 2 will not be evaluated.

     Now consider the following conditional expression:

    if (/* condition 1 */ || /* condition 2 */)

     If condition 1 is true, then condition 2 will not be evaluated.

     These properties are helpful when you learn about the null value.

  • Tutorial 3: Conditionals and Iteration

    Boolean Manipulation (Continued)

     You can also use the ! operator, to toggle a boolean.

    A !A

    true false

    false true

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

    General Information Comparison Operators Boolean Manipulation

  • Conditionals

    Tutorial 3: Conditionals and Iteration

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

    General Information If Statements Switch Statements Ternary Statements

     Conditional statements allow you to control your program’s flow.

     Using conditionals, you can define multiple paths in your code, and then choose a path based on the result of a condition.

     Java offers three different types of conditional arguments: if statements, switch statements, and ternary statements.

     Ternary statements are barely used in this course, but they can be very helpful in some circumstances.

    “I know it doesn't sound like a big effort, but programmers are really, really lazy, and they like to minimize motion.”

    ~Steve Yegge

  • Conditionals (Continued)

    Tutorial 3: Conditionals and Iteration

    General Information If Statements Switch Statements Ternary Statements

     Below is a list of keywords related to conditional statements.

    • break • case • default • else • if • switch

     By the end of this tutorial, you should understand all of them.

     You can also nest conditional statements. That is to say, you can place one conditional statement inside another.

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

  • If Statements

    Tutorial 3: Conditionals and Iteration

    General Information If Statements Switch Statements Ternary Statements

     In its simplest form, an if statement looks like this:

    “Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.”

    ~ Antoine de Saint Exupéry

    if (/* condition */)

    {

    //Condition evaluated to true

    }

     The code inside the if statement only gets evaluated if the condition gets evaluated to true.

     Any code before or after an if statement gets evaluated regardless of the result of the condition.

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

  • If – Else Statements

    Tutorial 3: Conditionals and Iteration

    General Information If Statements Switch Statements Ternary Statements

     In addition to saying what to do if the condition is true, you can also say what to do if the condition evaluates to false.

     You do this with an if-else statement.

    if (/* condition */)

    {

    //Condition evaluated to true

    }

    else

    {

    //Condition evaluated to false

    }

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

  • If – Else If – Else Statements

    Tutorial 3: Conditionals and Iteration

    General Information If Statements Switch Statements Ternary Statements

     You can even define more paths using else if statements.

    if (/* condition 1 */)

    {

    //Condition1 evaluated to true

    }

    else if (/* condition 2 */)

    {

    //Condition1 evaluated to false

    //Condition2 evaluated to true

    }

    else

    {

    //Condition1 evaluated to false

    //Condition2 evaluated to false

    }

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

  • Tutorial 3: Conditionals and Iteration

     Both else if statements and else statements are optional.

     You can chain as many else if statements as you want.

     You can use at most one else statement per if block.

     The else statement is always the last one in an if block.

     At each if block, the code will take at most one path. (If you have an else statement, the code is guaranteed to take one path.)

     If execution path takes the first condition that evaluates to true.

    If Statements: Tips

    General Information If Statements Switch Statements Ternary Statements

    Conditions Conditionals

    Iteration Practice Programs

    Addendum

  • Tutorial 3: Conditionals and Iteration

     You don’t need the block (brace brackets) around an if statement! Should they be absent, the first line following the condition is executed as the block. For example:

    If Statements: Tips (Continued)

    General Information If Statements Switch Statements Ternary Statements

     This approach is legal but highly discouraged because it leads to hard to find bugs. Also, it may act unexpectedly when nesting ifs.

    int x = 8;

    if ((x % 2) == 0)

    System.out.println("x is