Docstoc

The for Statement

Document Sample
The for Statement Powered By Docstoc
					               Lecture Seven

              Decision Making,
           Branching and Looping


04/08/08         Md.Samsuzzaman, lecturer ,Dept   1
                        of CCE,PSTU
 Decision Making and Branching
 When a program breaks the sequential flow and jumps
  to another part of the code, it is known as branching.
  When branching is done on a condition it is known as
  conditional branching.

 Three decision making statements:

   1. if statement

   2. switch statement

   3. conditional operator statement
04/08/08             Md.Samsuzzaman,lecturer ,Dept     2
                            of CCE,PSTU
                      The if Statement

The if statement has the following syntax:

                              The condition must be a boolean expression.
       if is a Java             It must evaluate to either true or false.
     reserved word

                        if (condition)
                           statement;
                        Statement x;


            If the condition is true, the statement is executed.
                   If it is false, the statement is skipped.
04/08/08                 Md.Samsuzzaman,lecturer ,Dept                      3
                                of CCE,PSTU
            The if-else Statement
An else clause can be added to an if statement
 to make an if-else statement
              if ( condition )
                     statement1;
                   else
                     statement2;
                   Statement x;

If the condition is true, statement1 is executed; if the
condition is false, statement2 is executed

One or the other will be executed, but not both

04/08/08              Md.Samsuzzaman,lecturer ,Dept         4
                             of CCE,PSTU
           Nested if….Else Statements
 The if..else statement can be contained in another if or
  else statement.
      if (test condition1)
      {
              if (test condition2)
                      statement-1;
              else
                      statement-2;
      }
      else
              statement-3;
           statement-x;
04/08/08                  Md.Samsuzzaman,lecturer ,Dept   5
                                 of CCE,PSTU
           Nested if….Else Statements
 An else clause is matched to the last unmatched if (no
  matter what the indentation implies!)
 Example:
  if(female)
       if(bal>5000)
              bon = 0.05 * bal;
  else
       bon = 0.02 * bal;
  bal = bal + bon;
 Braces can be used to specify the if statement to
  which an else clause belongs
04/08/08           Md.Samsuzzaman,lecturer ,Dept       6
                          of CCE,PSTU
           Multiway Selection: Else if
Sometime you want to select one option from
 several alternatives                true
                                                  conditon1
                                                                     statement1
                                                  evaluated

           if (conditon1)                        false

              statement1;                         conditon2   true
                                                                     statement2
                                                  evaluated
           else if (condition2)
                                                 false
              statement2;
                                                  conditon3   true
           else if (condition3)                                       statement3
                                                  evaluated
              statement3;                        false
           else                                  statement4
              statement4;
04/08/08                  Md.Samsuzzaman,lecturer ,Dept                       7
                                 of CCE,PSTU
                             Else if example
double numberGrade = 83.6;
char letterGrade;

                                                          Output:
if (numberGrade >= 89.5) {
   letterGrade = ‘A’;
} else if (numberGrade >= 79.5) {                           My Grade is 83.6, B
   letterGrade = ‘B’;
} else if (numberGrade >= 69.5) {
   letterGrade = ‘C’;
} else if (numberGrade >= 59.5) {
   letterGrade = ‘D’;
} else {
   letterGrade = ‘F’;
}

System.out.println(“My Grade is ” + numberGrade + “, ” + letterGrade);


  04/08/08                          Md.Samsuzzaman,lecturer ,Dept                 8
                                           of CCE,PSTU
           The switch Statement
 The switch statement provides another means to decide which
  statement to execute next

 The switch statement evaluates an expression, then attempts to
  match the result to one of several possible cases

 The expression of a switch statement must result in an integral
  type, meaning an int or a char

 Each case contains a value and a list of statements

 The flow of control transfers to statement associated with the
  first value that matches


04/08/08              Md.Samsuzzaman,lecturer ,Dept                9
                             of CCE,PSTU
            The switch Statement
The general syntax of a switch statement is:


  switch      switch (expression) {
   and          case value1:
   case            statement-list1
    are         case value2:
 reserved          statement-list2
  words         case value3:
                                                   If expression
                   statement-list3
                                                   matches value2,
              }
                                                   control jumps
                                                   from here
04/08/08           Md.Samsuzzaman,lecturer ,Dept                     10
                          of CCE,PSTU
            The switch Statement
 Often a break statement is used as the last statement in each
  case's statement list

 A break statement causes control to transfer to the end of the
  switch statement

 If a break statement is not used, the flow of control will
  continue into the next case

 Sometimes this can be appropriate, but usually we want to
  execute only the statements associated with one case


04/08/08               Md.Samsuzzaman,lecturer ,Dept               11
                              of CCE,PSTU
           The switch Statement
 A switch statement can have an optional default case
 The default case has no associated value and simply
  uses the reserved word default
 If the default case is present, control will transfer to it
  if no other case value matches
 If there is no default case, and no other value
  matches, control falls through to the statement after
  the switch

04/08/08            Md.Samsuzzaman,lecturer ,Dept           12
                           of CCE,PSTU
                                Switch example
char letter = 'b';                            char letter = 'b';

switch (letter) {                             switch (letter) {
  case 'a':                                     case 'a':
     System.out.println("A");                      System.out.println("A");
     break;                                     case 'b':
  case 'b':                                        System.out.println("B");
     System.out.println("B");                   case 'c':
     break;                                        System.out.println("C");
  case 'c':                                        break;
     System.out.println("C");                   case 'd':
     break;                                        System.out.println("D");
  case 'd':                                        break;
     System.out.println("D");                   default:
     break;                                        System.out.println(”?");
  default:                                    }
     System.out.println(”?");
}


                        B                                B
                                                         C
      04/08/08                    Md.Samsuzzaman,lecturer ,Dept               13
                                         of CCE,PSTU
           The Conditional Operator
 Java has a conditional operator that evaluates a boolean
  condition that determines which of two other expressions is
  evaluated

 The result of the chosen expression is the result of the entire
  conditional operator

 Its syntax is:

               condition ? expression1 : expression2

 If the condition is true, expression1 is evaluated; if it is false,
  expression2 is evaluated
04/08/08                Md.Samsuzzaman,lecturer ,Dept               14
                               of CCE,PSTU
            The Conditional Operator
 The conditional operator is similar to an if-else statement, except that it
  forms an expression that returns a value

 For example:

                    larger = ((num1 > num2) ? num1 : num2);

     if (num1 > num2)

            larger = num1;

     else

           larger = num2;

 The conditional operator is ternary because it requires three operands



04/08/08                     Md.Samsuzzaman,lecturer ,Dept                      15
                                    of CCE,PSTU
           The Conditional Operator
Another example:


   System.out.println ("Your change is " + count +
      ((count == 1) ? "Dime" : "Dimes"));

If count equals 1, then "Dime" is printed

If count is anything other than 1, then "Dimes" is printed



04/08/08               Md.Samsuzzaman,lecturer ,Dept         16
                              of CCE,PSTU
             Repetition Statements
 Repetition statements allow us to execute a statement multiple
  times
 Often they are referred to as loops
 Like conditional statements, they are controlled by boolean
  expressions
 Java has three kinds of repetition statements:
      the while loop
      the do loop
      the for loop
 The programmer should choose the right kind of loop for the
  situation
04/08/08                Md.Samsuzzaman,lecturer ,Dept           17
                               of CCE,PSTU
             The while Statement
 The while statement has the following syntax:



                     while (condition)
     while is a
                      statement;
  reserved word

                   If the condition is true, the statement is executed.
                         Then the condition is evaluated again.

             The statement is executed repeatedly until
                    the condition becomes false.

04/08/08               Md.Samsuzzaman,lecturer ,Dept                      18
                              of CCE,PSTU
             Logic of a while Loop

                             condition
                             evaluated


                                    true         false


                             statement



   Note that if the condition of a while statement is false initially, the
   statement is never executed. Therefore, the body of a while loop will
   execute zero or more times

04/08/08                 Md.Samsuzzaman,lecturer ,Dept                   19
                                of CCE,PSTU
            while Loop Example
final int LIMIT = 5;
int count = 1;
                                                    Output:
while (count <= LIMIT) {
                                                      1
                                                      2
    System.out.println(count);                        3
                                                      4
    count += 1;                                       5
}


04/08/08            Md.Samsuzzaman,lecturer ,Dept             20
                           of CCE,PSTU
                  Infinite Loops
 The body of a while loop eventually must make the
  condition false

 If not, it is an infinite loop




04/08/08             Md.Samsuzzaman,lecturer ,Dept    21
                            of CCE,PSTU
                 Nested Loops
 Similar to nested if statements, loops can be nested as
  well

 That is, the body of a loop can contain another loop

 Each time through the outer loop, the inner loop goes
  through its full set of iterations




04/08/08           Md.Samsuzzaman,lecturer ,Dept         22
                          of CCE,PSTU
                  The do Statement
 The do statement has the following syntax:


     do and               do{
    while are               statement;
    reserved              } while (condition);
     words

           The statement is executed once initially,
             and then the condition is evaluated

             The statement is executed repeatedly
               until the condition becomes false

04/08/08                  Md.Samsuzzaman,lecturer ,Dept   23
                                 of CCE,PSTU
             do-while Example
final int LIMIT = 5;
int count = 1;
                                                   Output:
do {
                                                     1
  System.out.println(count);                         2
  count += 1;                                        3
                                                     4
} while (count <= LIMIT);                            5



04/08/08           Md.Samsuzzaman,lecturer ,Dept             24
                          of CCE,PSTU
           Comparing while and do
            while loop                                   do loop




                                                         statement
            condition
            evaluated
                                            true

                  true        false                      condition
                                                         evaluated
            statement
                                                              false




04/08/08                 Md.Samsuzzaman,lecturer ,Dept                25
                                of CCE,PSTU
                   The for Statement
 The for statement has the following syntax:
              The initialization      The statement is
    Reserved
               is executed once      executed until the
     word
            before the loop begins condition becomes false


       for (initialization; condition; increment)
         statement;

      The increment portion is executed at the
      end of each iteration
      The condition-statement-increment cycle is
      executed repeatedly

04/08/08                    Md.Samsuzzaman,lecturer ,Dept    26
                                   of CCE,PSTU
           The for Statement
A for loop is functionally equivalent to the
 following while loop structure:
              initialization;
              while (condition) {
                 statement;
                 increment;
              }




04/08/08         Md.Samsuzzaman,lecturer ,Dept   27
                        of CCE,PSTU
           Logic of a for loop

                initialization


                  condition
                  evaluated

                         true         false

                  statement


                  increment




04/08/08      Md.Samsuzzaman,lecturer ,Dept   28
                     of CCE,PSTU
              The for Statement
 Like a while loop, the condition of a for statement is
  tested prior to executing the loop body

 Therefore, the body of a for loop will execute zero or
  more times

 It is well suited for executing a loop a specific
  number of times that can be determined in advance



04/08/08           Md.Samsuzzaman,lecturer ,Dept           29
                          of CCE,PSTU
                       for Example

final int LIMIT = 5;
for (int count = 1; count <= LIMIT; count++) {
    System.out.println(count);
}
                        Output:

                            1
                            2
                            3
                            4
                            5
04/08/08                Md.Samsuzzaman,lecturer ,Dept   30
                               of CCE,PSTU
                The for Statement
 Each expression in the header of a for loop is optional

     If the initialization is left out, no initialization is
      performed
     If the condition is left out, it is always considered to be
      true, and therefore creates an infinite loop
     If the increment is left out, no increment operation is
      performed

 Both semi-colons are always required in the for loop
  header
04/08/08               Md.Samsuzzaman,lecturer ,Dept           31
                              of CCE,PSTU
       Choosing a Loop Structure
 When you can’t determine how many times you want
  to execute the loop body, use a while statement or a
  do statement
     If it might be zero or more times, use a while statement
     If it will be at least once, use a do statement

 If you can determine how many times you want to
  execute the loop body, use a for statement


04/08/08               Md.Samsuzzaman,lecturer ,Dept        32
                              of CCE,PSTU
Chapter Five from text book




04/08/08       Md.Samsuzzaman,lecturer ,Dept   33
                      of CCE,PSTU

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:9/28/2012
language:Unknown
pages:33