Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

lecture07

VIEWS: 0 PAGES: 30

									    Lecture 7
Repetitions (Part 2 )
     C Programming
         AAST
         CCIT
       Spring 2012


                        1
         Lecture 7 topics

• For Statement for Looping

• Nested Loops




                              2
                 For Loop

SYNTAX

for ( initialization ; test expression ; update )
{
       0 or more statements to repeat


}

                                                3
The for loop contains

   an initialization

   an expression to test for continuing

 an update to execute after each
 iteration of the body



                                      4
  Example of Repetition
int num;

for ( num = 1 ; num <= 3 ; num++ )
{
    printf( “ % d Potato \n ” , num );
}




                                         5
num     ?
                   Example of Repetition
      int num;

      for ( num = 1 ; num <= 3 ; num++ )

        printf( “ % d Potato \n ” , num );

                       OUTPUT




                                             6
num     1            Example of Repetition

      int num;

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                          OUTPUT




                                                 7
num     1            Example of Repetition

      int num;
                            true

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                          OUTPUT




                                                 8
num     1            Example of Repetition

      int num;

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                           OUTPUT

                 1Potato


                                                 9
num     2            Example of Repetition

      int num;

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                           OUTPUT

                 1Potato


                                                 10
num     2            Example of Repetition

      int num;
                            true

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                           OUTPUT

                 1Potato


                                                 11
num     2            Example of Repetition

      int num;

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                           OUTPUT

                 1Potato
                 2Potato
                                                 12
num     3            Example of Repetition

      int num;

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                           OUTPUT

                 1Potato
                 2Potato
                                                 13
num     3            Example of Repetition

      int num;
                            true

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                           OUTPUT

                 1Potato
                 2Potato
                                                 14
num     3            Example of Repetition

      int num;

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                           OUTPUT

                 1Potato
                 2Potato
                 3Potato                         15
num     4            Example of Repetition

      int num;

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                           OUTPUT

                 1Potato
                 2Potato
                 3Potato                         16
num     4            Example of Repetition

      int num;
                            false

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

                           OUTPUT

                 1Potato
                 2Potato
                 3Potato                         17
num     4            Example of Repetition

      int num;
                            false

      for ( num = 1 ; num <= 3 ; num++ )

            printf( “ % d Potato \n ” , num );

            When the loop control condition
            is evaluated and has value false, the
            loop is said to be “satisfied” and
            control passes to the statement
            following the For statement.
                                                    18
          The output was:

1Potato
2Potato
3Potato




                            19
          Count-controlled Loop
int count ;
for ( count = 4 ; count > 0 ; count-- )
{
      printf( “ %d \n “ , count ) ;
}
printf ( “Done” ) ;

OUTPUT:    4
           3
           2
           1
                                          20
           Done
       What is the output?
int count;

for ( count = 0 ; count < 10 ; count++ )
{
        printf( “*” );
}


                                           21
                 OUTPUT

 **********



NOTE: the 10 asterisks are all on one line. Why?




                                                   22
 What output from this loop?
int count;

for (count = 0; count < 10; count++) ;
{
       printf ( “*” );
}


                                         23
                   OUTPUT
• One * !     Why?
• the ; right after the ( ) means that the body
  statement is a null statement
• in general, the Body of the for loop is whatever
  statement immediately follows the ( )
• that statement can be a single statement, a
  block, or a null statement
• actually, the code outputs one * after the loop
  completes its counting to 10
                                                24
             Nested Loops
initialize outer loop
while ( outer loop condition )
{     ...

      initialize inner loop
      while ( inner loop condition )
      {
              inner loop processing and update
      }
       ...
}
 The famous Triangle example
 Write a program to ask the user for an
 integer n , and then draw a triangle of
 stars with length n & height n .

If n=4

*
* *
* * *
* * * *
                 Example
Write a program that displays the
multiplication tables ( 1 - 12 ).

  1 x 1 = 1
  1 x 2 = 2
  ….
  1 x 12 = 12
  2 x 1 = 2
  ….
  12 x 12 = 144

                                    27
       FYI: Break Statement
• break statement can be used with Switch or
  any of the 3 looping structures

• it causes an immediate exit from the Switch,
  While, Do-While, or For statement in which it
  appears

• if the break is inside nested structures,
  control exits only the innermost structure
  containing it
• We don’t recommend using it.                 28
      FYI: Continue Statement
• is valid only within loops

• terminates the current loop iteration, but not the
  entire loop

• in a For or While, continue causes the rest of the
  body statement to be skipped--in a For statement,
  the update is done

• in a Do-While, the exit condition is tested, and if
  true, the next loop iteration is begun

• We don’t recommend using it.
                                                        29
The End

    Lecture 7
 C Programming
  Spring 2012
                 30

								
To top