KEMBAR78
Computer Programming, Loops using Java - part 2 | PPT
Using Java
MINISTRY OF EDUCATION & HIGHER EDUCATION
COLLEGE OF SCIENCE AND TECHNOLOGY
KHANYOUNIS- PALESTINE
Lecture 11
Repetition Statements
Practices
 Identify and correct the errors in each of the
following pieces of code.
 int x = 1,
total;
while ( x <= 10 )
{ total += x; ++x; }
2
Presented & Prepared by: Mahmoud R. Alfarra
Practice 1
3
Presented & Prepared by: Mahmoud R. Alfarra
Practice 1
 Identify and correct the errors in each of the
following pieces of code.
 while ( x <= 100 )
total += x;
++x;
 while ( y > 0 )
{
System.out.println( y );
++y;
4
Presented & Prepared by: Mahmoud R. Alfarra
Practice 1
 Identify and correct the errors in each of the
following pieces of code.
 for ( i = 100, i >= 1, i++ )
System.out.println( i );
 The following code should output the odd integers from 19 to 1:
for ( i = 19; i >= 1; i += 2 )
System.out.println( i );
5
Presented & Prepared by: Mahmoud R. Alfarra
Practice 1
 Identify and correct the errors in each of the
following pieces of code.
 The following code should print whether integer value is odd or
even:
switch ( value % 2 )
{
case 0:
System.out.println( "Even integer" );
case 1:
System.out.println( "Odd integer" );
}
6
Presented & Prepared by: Mahmoud R. Alfarra
Practice 1
 Identify and correct the errors in each of the
following pieces of code.
 The following code should output the even integers from 2 to 100:
counter = 2;
do
{
System.out.println( counter );
counter += 2 ;
} While ( counter < 100 );
Trace the following program:
7
Presented & Prepared by: Mahmoud R. Alfarra
Practice 2
Trace the following program:
8
Presented & Prepared by: Mahmoud R. Alfarra
Practice 3
Trace the following program:
9
Presented & Prepared by: Mahmoud R. Alfarra
Practice 3
Trace the following program:
10
Presented & Prepared by: Mahmoud R. Alfarra
Practice 4
Trace the following program:
11
Presented & Prepared by: Mahmoud R. Alfarra
Practice 5
Trace the following program:
12
Presented & Prepared by: Mahmoud R. Alfarra
Practice 5
 Write an application that finds the smallest of
several integers. Assume that the first value read
specifies the number of values to input from the
user.
13
Presented & Prepared by: Mahmoud R. Alfarra
Practice 6
 Write an application that calculates the product of
the odd integers from 1 to 15.
14
Presented & Prepared by: Mahmoud R. Alfarra
Practice 7
 Write an application that prints the following
diamond shape. You may use output statements
that print a single asterisk (*), a single space or a
single newline character. Maximize your use of
repetition, and minimize the number of output
statements.
15
Presented & Prepared by: Mahmoud R. Alfarra
Practice 8
More practices in
the text book,
chapters 4 and 5
16
Presented & Prepared by: Mahmoud R. Alfarra
More Practices
‫أن‬ ‫ـد‬‫ب‬‫للع‬ ‫اإلميان‬ ‫درجة‬ ‫ترفع‬ ‫اليت‬ ‫االسباب‬ ‫أعظم‬ ‫من‬
:
‫هللا‬ ‫يتق‬
17
Presented & Prepared by: Mahmoud R. Alfarra
Practices
18
Presented & Prepared by: Mahmoud R. Alfarra

Computer Programming, Loops using Java - part 2

  • 1.
    Using Java MINISTRY OFEDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 11 Repetition Statements Practices
  • 2.
     Identify andcorrect the errors in each of the following pieces of code.  int x = 1, total; while ( x <= 10 ) { total += x; ++x; } 2 Presented & Prepared by: Mahmoud R. Alfarra Practice 1
  • 3.
    3 Presented & Preparedby: Mahmoud R. Alfarra Practice 1  Identify and correct the errors in each of the following pieces of code.  while ( x <= 100 ) total += x; ++x;  while ( y > 0 ) { System.out.println( y ); ++y;
  • 4.
    4 Presented & Preparedby: Mahmoud R. Alfarra Practice 1  Identify and correct the errors in each of the following pieces of code.  for ( i = 100, i >= 1, i++ ) System.out.println( i );  The following code should output the odd integers from 19 to 1: for ( i = 19; i >= 1; i += 2 ) System.out.println( i );
  • 5.
    5 Presented & Preparedby: Mahmoud R. Alfarra Practice 1  Identify and correct the errors in each of the following pieces of code.  The following code should print whether integer value is odd or even: switch ( value % 2 ) { case 0: System.out.println( "Even integer" ); case 1: System.out.println( "Odd integer" ); }
  • 6.
    6 Presented & Preparedby: Mahmoud R. Alfarra Practice 1  Identify and correct the errors in each of the following pieces of code.  The following code should output the even integers from 2 to 100: counter = 2; do { System.out.println( counter ); counter += 2 ; } While ( counter < 100 );
  • 7.
    Trace the followingprogram: 7 Presented & Prepared by: Mahmoud R. Alfarra Practice 2
  • 8.
    Trace the followingprogram: 8 Presented & Prepared by: Mahmoud R. Alfarra Practice 3
  • 9.
    Trace the followingprogram: 9 Presented & Prepared by: Mahmoud R. Alfarra Practice 3
  • 10.
    Trace the followingprogram: 10 Presented & Prepared by: Mahmoud R. Alfarra Practice 4
  • 11.
    Trace the followingprogram: 11 Presented & Prepared by: Mahmoud R. Alfarra Practice 5
  • 12.
    Trace the followingprogram: 12 Presented & Prepared by: Mahmoud R. Alfarra Practice 5
  • 13.
     Write anapplication that finds the smallest of several integers. Assume that the first value read specifies the number of values to input from the user. 13 Presented & Prepared by: Mahmoud R. Alfarra Practice 6
  • 14.
     Write anapplication that calculates the product of the odd integers from 1 to 15. 14 Presented & Prepared by: Mahmoud R. Alfarra Practice 7
  • 15.
     Write anapplication that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single newline character. Maximize your use of repetition, and minimize the number of output statements. 15 Presented & Prepared by: Mahmoud R. Alfarra Practice 8
  • 16.
    More practices in thetext book, chapters 4 and 5 16 Presented & Prepared by: Mahmoud R. Alfarra More Practices
  • 17.
    ‫أن‬ ‫ـد‬‫ب‬‫للع‬ ‫اإلميان‬‫درجة‬ ‫ترفع‬ ‫اليت‬ ‫االسباب‬ ‫أعظم‬ ‫من‬ : ‫هللا‬ ‫يتق‬ 17 Presented & Prepared by: Mahmoud R. Alfarra
  • 18.
    Practices 18 Presented & Preparedby: Mahmoud R. Alfarra