More about control statements

Chapter 5

What’s new in Chapter 5

•      The switch statement

•      Variations of the for loop

•      Example using a do..while loop

•      Using end-of-file indicators

•      Break and continue statements

•      Logical operators and(&&) and or(||) and negation(!)

•    Also less often used Exclusive OR (^)

•      There are also a lot of examples of programs using the control structures      

 

Variations of for loops

•      You can start with a high number and decrement instead of increment

–    for (c = 10; c > 0; c--)

•      You don’t have to increment or decrement by one

–    for (c = 10; c > 0; c-=2)

•      Remember what the three parts of a for loop stand for: 
initialize, terminating condition, change control variable

–    Any expression that does this will compile

•      You can also declare the type of  the loop variable inside the for statement

–   for (int c=1; c<=30; c++)

–    It is best not to use this style if the variable will be used outside the loop

 

Nested loops

public static void main(String[ ] args)            
{  int outside, inside;

         for (outside=0; outside <5; outside++)

                {   for (inside = 0; inside < 5; inside++)

                            {  System.out.print(inside + "  ");  }

                      System.out.println();

                 }

     }

•    Walk through this loop

Variations on nested loops

for (outside=0; outside <5; outside++)

     {       for (inside = 0; inside < outside; inside++)

                         System.out.print(inside + "  ");

              System.out.println();

     }

•      Another variation

for (outside=0; outside <5; outside++)

     {   for (inside = outside; inside < 5; inside++)

                         System.out.print(inside + "  ");

          System.out.println();

     }

break and continue

•      The break statement always

–   Terminates the current iteration immediately, then

–   Terminates the loop

•      The continue statement always

–   Terminates the current iteration immediately, then

–   Proceeds to start the next iteration

 

Breaking out of the middle of a loop

•      You can put the condition in the middle of a loop, then use the break keyword to exit the loop

•      Execution will then continue after the last statement in the loop block

•      This is not considered the best programming practice

•      You can always write a loop so you do not need to break out of the middle

 

Complex Boolean expressions
are connected by logical operators

•      They return type boolean (true or false)

•      How to use the logical operators

–  AND  &&  both operands must be true for the statement as a whole to be true

( (6>5) && (4==7) )  is false

–  OR   ||  if either operands is true, the statement as a whole is true

( (6>5) || (4==7) )  is true

–  NOT  !  Negates the operand

!(6==6)   is false

 


Truth tables 

•       Gives the truth value for all combinations of a proposition

•      Look at the truth table for negation

Conjunction  AND  

 

•       A  AND B  is true only when both A and B are true

•      A is one operand, B is the other  (A) && (B)

•      A AND  B is false any other time

Example:  A AND  B

A is (Helena is Montana’s capitol)

B is (Bozeman is Montana’s largest city)

(Helena is Montana’s capitol) &&

(Bozeman is Montana’s largest city)

is false

 

Disjunction  OR   

•     A OR B is true when either A or B are true

•     A || B is false only when both A and B are false

Example:  A || B

A is (Helena is Montana’s capitol)

B is (Bozeman is Montana’s largest city)

The statement|
(Helena is Montana’s capitol)
||

(Bozeman is Montana’s largest city)
is true