Done by: Esonboyev Umid python 3 -bsic operators, Decision making (if),Nested If Statements


Download 64.06 Kb.
Sana17.12.2022
Hajmi64.06 Kb.
#1025682
Bog'liq
umid if

Done by: Esonboyev Umid

  • PYTHON 3 –Bsic operators, Decision making (if),Nested If Statements.

Logical AND and Nesting

  • If we have the && operator, do we need nesting?
  • if(BooleanExpression1) {

    if(BooleanExpression2) {

    Both conditions met

    }

    else {

    Both conditions NOT met

    }

    }

    else {

    Both conditions NOT met

    }

    if(BooleanExpression1 && BooleanExpression2) {

    Both conditions met

    }

    else {

    Both conditions NOT met

    }

Logical AND and Nesting

  • Answer: Yes, to act if one of the conditions is not met:
  • if(BooleanExpression1) {

    if(BooleanExpression2) {

    Both conditions met

    }

    else {

    Condition 2 not met

    }

    }

    else {

    Condition 1 not met

    }

    if(BooleanExpression1 && BooleanExpression2) {

    Both conditions met

    }

    else {

    Both conditions NOT met

    }

Exercise

  • Change NestedIfStatement.java to tell the user if she does not meet one requirement, the other requirement, or BOTH.
    • Hint: we talked about something last lecture that will help us…
      • Flags

Logical OR Example

  • New Topics:
    • Logical OR

Exercise

  • Change LogicalOrOperator.java to allow the user to enter capital or lower case Y or N as an answer.
    • Hint
      • Use another OR operator.
  • Also, check to make sure the user entered a valid response.
    • Y, N, y, or n.

Comparing String Objects

  • Imagine you have declared two String variables as such:
  • String x = "buffalo";

    String y = “bison";

  • What does x == y resolve to?
    • false, but not for the reason you think!
      • The == operator compares what the reference values are (what objects they are pointing to), NOT what the value of the string is.
        • In some cases this causes problems
  • You should use methods in the String class in order to compare String variables
    • equals
    • compareTo

String equals method

  • To check if two strings are the same you can use the equals method.
  • StringReference.equals(OtherString);

    • If the string referred to by StringReference is equal to the one referred to by OtherString, then true is returned.
      • Otherwise false is returned.
    • This comparison is case-sensitive ("buffalo" and "Buffalo" are not equal)
      • To do case-insensitive comparison, use equalsIgnoreCase

String compareTo method

  • If you want to determine which string is “greater” use the compareTo method.
  • StringReference.compareTo(OtherString);

    • If the string referred to by StringReference is equal to the one referred to by OtherString, then 0 is returned.
    • If the string referred to by StringReference is “less than” to the one referred to by OtherString, then a negative number is is returned.
    • If the string referred to by StringReference is “greater than” to the one referred to by OtherString , then a positive number is returned.
    • This comparison is case-sensitive ("buffalo" and "Buffalo" are not equal)
      • To do case-insensitive comparison, use compareToIgnoreCase

String compareTo method

  • What does it mean for strings to be “greater than” or “less than” each other?
    • Java compares the two strings, character by character from left to right.
    • When there is a difference in a character it compares the Unicode values of the characters.
    • If a string is shorter than another, and the shorter one is the beginning of the longer one, the shorter one is considered less.
      • "hi" is less than "high"

String Comparison Example

  • New Topics:
    • String == operator
    • equals method
    • compareTo method

Block-Level Scope

  • If a variable is declared inside of a block, it is said to have block-level scope.
    • If a variable has Block-Level Scope it is in scope from its declaration to the ending of the block in which it was declared.
    • if(BooleanExpression) {

      int x;

      }

        • The variable x has scope from the point it was declared to the }
        • This does not have much use now, but will be more useful when we learn loops.

The Conditional Operator

  • Java provides and operator to create short expressions that work like if-else statements.
  • BooleanExpression ? Value1 : Value2;

    • If BooleanExpression is true, Value1 is returned
    • If BooleanExpression is false, Value2 is returned
  • Example:
  • if (score < 60)

    System.out.println("You Fail");

    else

    System.out.println("You Pass");

    System.out.println("You "

    + score < 60 ? "Fail" : "Pass");


Download 64.06 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling