Introduction To Programming Information Technology , 1’st
Download
Report
Transcript Introduction To Programming Information Technology , 1’st
MINISTRY OF EDUCATION & HIGHER EDUCATION
COLLEGE OF SCIENCE AND TECHNOLOGY
KHANYOUNIS- PALESTINE
Using Java
Lecture 11
Repetition Statements
Practices
Practice 1
Identify and correct the errors in each of the
following pieces of code.
int x = 1,
total;
while ( x <= 10 )
{ total += x; ++x; }
Presented & Prepared by: Mahmoud R. Alfarra
2
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;
Presented & Prepared by: Mahmoud R. Alfarra
3
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 );
Presented & Prepared by: Mahmoud R. Alfarra
4
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" );
}
Presented & Prepared by: Mahmoud R. Alfarra
5
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 );
Presented & Prepared by: Mahmoud R. Alfarra
6
Practice 2
Trace the following program:
Presented & Prepared by: Mahmoud R. Alfarra
7
Practice 3
Trace the following program:
Presented & Prepared by: Mahmoud R. Alfarra
8
Practice 3
Trace the following program:
Presented & Prepared by: Mahmoud R. Alfarra
9
Practice 4
Trace the following program:
Presented & Prepared by: Mahmoud R. Alfarra
10
Practice 5
Trace the following program:
Presented & Prepared by: Mahmoud R. Alfarra
11
Practice 5
Trace the following program:
Presented & Prepared by: Mahmoud R. Alfarra
12
Practice 6
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.
Presented & Prepared by: Mahmoud R. Alfarra
13
Practice 7
Write an application that calculates the product of
the odd integers from 1 to 15.
Presented & Prepared by: Mahmoud R. Alfarra
14
Practice 8
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.
Presented & Prepared by: Mahmoud R. Alfarra
15
More Practices
More
practices in
the text book,
chapters 4 and 5
Presented & Prepared by: Mahmoud R. Alfarra
16
من أعظم االسباب التي ترفع درجة اإليمان للعبـد أن:
يتق اهلل
17
Presented & Prepared by: Mahmoud R. Alfarra
Practices
Presented & Prepared by: Mahmoud R. Alfarra
18