Conditionals: if if/else flowcharts

New topic: Conditionals
Click on this label:
Screen Shot 2014-10-02 at 10.05.29 AM

Classwork:
Flowcharts: write a short code fragment for each of the following flowcharts:
if and if/else
flowchars-if-else

while and for loops

flowchart-loop

More on conditionals with the slides on this link:

Assignments:

  1. Write a more general and robust version of ComplexRootsQuad.java that prints the roots of the polynomial ax^2 + bx + c, prints real and complex solutions.
  2. What (if anything) is wrong with each of the following statements?
    if (a > b) then c = 0;
    if a > b { c = 0; }
    if (a > b) c = 0;
    if (a > b) c = 0 else b = 0;
  3. Write a code fragment that prints true if the double variables x and y are both strictly between 0 and 1 and false otherwise.
  4. Write a program AllEqual_YI.java that takes three integer command-line arguments and prints equal if all three are equal, and not equal otherwise.
  5. Rewrite TenHellos.java to make a program ManyHellos_YI.java that prompts the user for the consecutive lines to print… You may assume that the argument is less than 1000. Hint: consider using i % 10 and i % 100 to determine whether to use “st”, “nd”, “rd”, or “th” for printing the ith Hello.Example output:
    * Starting number? 4
    * Ending number? 8

    * 4th Hello
    * 5th Hello
    * 6th Hello
    * 7th Hello
    * 8th Hello

  6. What is the value of m and n after executing the following code?
    int n = 123456789;
    int m = 0;
    while (n != 0) {
       m = (10 * m) + (n % 10);
       n = n / 10;
    }
    
    
  7. What does the following code print out?
    int f = 0, g = 1;
    for (int i = 0; i <= 15; i++) {
       System.out.println(f);
       f = f + g;
       g = f - g;
    }
    
    

A site to check your understanding: