View
5
Download
0
Category
Preview:
Citation preview
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجانًاالنوتات متوفرة hs.com-enginfo@ 260 4444 9 (1 ) م. حمادة شعبان
April 2016
Do While Switch & For Control
Statements
Week 4
والبعض أحالمهم، تحقيق همهم الناس بعض
.اآلخرين أحالم هدم همهم اآلخر
نوت الجافا
. أجزاءتتكون النوت من عشرة
على نوت كل أسبوعيحتوي
من ألمثلة وتمارينشرح وحلول
سابقة.هوموركات وامتحانات
النوتات اإلضافية
. تتكون من عشرة أجزاء أيضا
حلول على نوت كل شابتر يحتوي
إضافية.امتحانات وألمثلة
حقوق النوت
لنسخ حقوق ا أتيحلتعميم الفائدة فإني
ل لك وشروحات الفيديو وإعادة اإلصدار
لبعض األجزاء أو الكل. سواء البشر
(00965-26094444واتساب )
يتم تنقحة النوت وتحديثها
ي.الجديد إليها بشكل دوروإضافة
.نتشوق النتقادك الهادف
لطلب نسخة ملونة مجانا06244449
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجانًاالنوتات متوفرة hs.com-enginfo@ 260 4444 9 (2 ) م. حمادة شعبان
April 2016
5.2 Essentials of Counter-Controlled Repetition
The elements required to perform counter-controlled repetition are:
1. A control variable.
2. The initial value.
3. The increment or decrement of the control variable.
4. The loop-continuation condition.
The elements above are defined in line 8, 10 and 13.
This program can be made more concise by initializing counter to 0 in
line 8 and having the following while statement
while ( ++counter <= 10 )
System.out.printf( “%d “, counter );
This code saves a statement and eliminates the need for braces around
the loop’s body because the increment is performed before testing.
يمكن وما تحقيقه، يمكن تخيله يمكن ما
.إليه للوصول طريقًا نعدم لن تحقيقه
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجانًاالنوتات متوفرة hs.com-enginfo@ 260 4444 9 (3 ) م. حمادة شعبان
April 2016
5.3 for Repetition Statement
for repetition statement specifies the counter-controlled-repetition details
in a single line of code
In line 10, the condition is ( counter <= 10 ). If you incorrectly specified
( counter < 10 ) as the condition, the loop would iterate only nine times.
Line 10 is sometimes called the for statement header. The for header
does it all. If there’s more than one statement in the body of the for,
braces are required.
The general format of the for statement is
for ( initialization; condition; increment or decrement)
statement(s)
If the condition is initially false, the program does not execute the for
statement’s body. Instead, the execution proceeds with the statement
following the for.
In all cases, the for statement can be represented with an equivalent
while statement.
The initialization, condition and increment portions of a for statement can
contain arithmetic expressions. For example,
for ( int j = x; j <= 4*y; j += y/x ) أكثر هو المرء غاية نسيان
.انتشارًا الغباء أشكال
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجانًاالنوتات متوفرة hs.com-enginfo@ 260 4444 9 (4 ) م. حمادة شعبان
April 2016
5.4 Examples Using the for Statement
The following examples show techniques for varying the control variable in
a for statement.
a) Varying the control variable from 1 to 100 in increments of 1.
for ( int i = 1; i<= 100; i++ )
b) Varying the control variable from 100 to 1 in decrements of 1.
for ( int i = 100; i>= 1; i-- )
c) Varying the control variable from 7 to 77 in increments of 7.
for ( int i = 7; i<= 77; i += 7 )
d) Varying the control variable from 20 to 2 in decrements of 2.
for ( int i = 20; i>= 2; i -= 2 )
e) Varying the control variable over the values 2, 5, 8, 11, 14, 17, 20.
for ( int i = 2; i<= 20; i += 3 )
f) Varying the control variable over the values 99, 88, 77, 66, 55, 44, 33, 22,
11, 0.
for ( int i = 99; i>= 0; i -= 11 )
إلدددى انتقدددل السدددهل، الشددد ء بعمدددل ابددددأ
.المستحيل ستفعل بعدها قليال، األصعب
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجانًاالنوتات متوفرة hs.com-enginfo@ 260 4444 9 (5 ) م. حمادة شعبان
April 2016
The following application uses a for statement to sum the even integers
from 2 to 20 and store the result in an int variable called total.
You could merge the body of the for statement in lines 11-12 into the
increment portion of the header by using a comma as follows:
for (int number = 2; number <= 20; total += number, number +=2);
But this is not recommended.
أن أو بظروفك، تتحكم أن يمكن
.لك الخيار! فيك تتحكم تتركها
ذاكر بضمير وحصل دنانير
ألي خطأ إمالئي أو فني أو ديناران!
(SMSمالحظة هامة )وتساب أو
(360 4444 9)إدارة التدريب
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجانًاالنوتات متوفرة hs.com-enginfo@ 260 4444 9 (6 ) م. حمادة شعبان
April 2016
5.5 do…while Repetition Statement
In the while, the program tests the condition at the beginning of the loop,
before executing the body. If the condition is false, the body never
executes.
The do…while statement tests the condition after executing the loop’s
body. Therefore, the body always gets executed at least once.
If the condition is true, the loop continues from the first body statement
(line 12).
If the condition is false, the loop terminates and the program continues
with the next statement after the loop.
لم إن تحزن الناس بعض
.عنها تتحدث مشاكل تجد
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجانًاالنوتات متوفرة hs.com-enginfo@ 260 4444 9 (7 ) م. حمادة شعبان
April 2016
5.6 switch Multiple-Selection Statement
The switch multiple-selection statement performs different actions based
on the possible values of a constant integral expression of type byte,
short, int char or boolean.
The following program uses a switch statement to determine whether
each grade is the equivalent of an A, B, C, D or F and to increment the
appropriate grade counter.
األعذار، الفاشلون يعدم ال
.الفن هذا يتقنون إنهم
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجانًاالنوتات متوفرة hs.com-enginfo@ 260 4444 9 (8 ) م. حمادة شعبان
April 2016
مما أكثر فعل تستطيع لن
.نفسك عن تعتقده
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجانًاالنوتات متوفرة hs.com-enginfo@ 260 4444 9 (9 ) م. حمادة شعبان
April 2016
The condition at line 57 calls Scanner method hasNext to determine
whether there’s more data to input. Method hasNext returns false once
the user types the end-of-file indicator.
We assume that the user enters a valid grade in the range 0-100.
The switch statement consists of a block that contains a sequence of
case labels and a default case.
(grade/10) is the switch’s controlling expression evaluates to an integral
value. When we divide a value from 0 to 100 by 10, the result is always a
value from 0 to 10.
بالطريقة حياتك فلتحيا منك بدالً أحد يموت لن
.هم يراها كما ال مناسبة أنت تراها الت
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (10 ) م. حمادة شعبان
April 2016
The break statement causes program control to proceed with the first
statement after the switch.
Cases at lines 74-75 enable the program to perform the same set of
statements.
The switch statement does not provide a mechanism for testing ranges
of values, so every value you need to test must be listed in a separate
case label.
If no match occurs between the controlling expression’s value and a
case label, the default case executes.
You can use character constants in switch’s cases, such as ‘A’, ‘7’ or ‘$’.
The following example shows how to use a string in switch’s controlling-
expression and in case labels.
حد ف هدفا ليس الموت أن طالما
األموات، حياة نعيش فلماذا ذاته،
حياتنا؟ ف األوحد الهدف وكأنه
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (11 ) م. حمادة شعبان
April 2016
Line 103 determines whether the user entered at least one grade,
this helps us to avoid dividing by zero.
يجاهدون لكنهم الفوز، على الناس فطر
.الهزيمة على يؤلفوها ك أنفسهم
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (12 ) م. حمادة شعبان
April 2016
5.7 break and continue Statements
The break statement, when executed in a while, for, do…while or switch,
causes immediate exit from that statement.
The continue statement, when executed in a while, for or do…while, skips
the remaining statements in the loop body and proceeds with the next
iteration of the loop.
عشتها، الت بالسنوات ليست العبرة النهاية، ف
.السنوات تلك ف الحياة نبض ف بل
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (13 ) م. حمادة شعبان
April 2016
Exercise 5.10: what does the following program do?
( لكل دورةiteration من اللوب ) مرات. 5الخارجية يتم تنفيذ اللوب الداخللية
run:
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
كثيدددرة، تنظدددر التددد العيدددون
.قليلة تبصر الت تلك لكن
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (14 ) م. حمادة شعبان
April 2016
Exercise 5.12: Write an application that calculates the product of the odd
integers from 1 to 15.
public class Product
{
public static void main (String[] args)
{
int product = 1;
for ( int i=1; i<=15; i++)
if ( i%2 == 1)
product *= i;
System.out.printf("The product of the odd integers
from 1 to 15 is: %d\n", product);
}
}
( يمكن كتابة الـfor( أعاله بطريقة أكثر حرفية وبدون استخدام )if:كما يلي )
for ( int i=1; i<=15; i+=2)
product *= i;
.فيك َخْيرَ فال حاسٌد لك يكن لم إذا
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (15 ) م. حمادة شعبان
April 2016
Exercise 5.15: Write an application that displays the following patterns
separately, one below the other. Use for loops to generate the patterns.
All asterisks (*) should be printed by a single statement of the form
System.out.print ('*'); which causes the asterisks to print side by side.
A statement of the form System.out.println (); can be used to move to the
next line. A statement of the form System.out.print ( ' ' ); can be used to
display a space for the last two patterns. There should be no other output
statements in the program. [Hint: The last two patterns require that each
line begin with an appropriate number of blank spaces.]
البعض ألن فقط طغاة، الناس بعض
.لهم يركع أن وافق اآلخر
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (16 ) م. حمادة شعبان
April 2016
public class Triangle {
public static void main(String[] args)
{
inti, j, k;
for ( i = 1; i<= 10; i++ )
{
for ( j = 1; j <= i; j++ )
System.out.print("*");
System.out.println();
}
System.out.println();
for ( i = 10; i>= 1; i-- ){
for ( j = 1; j <= i; j++ )
System.out.print("*");
System.out.println();
}
System.out.println();
for ( i = 0; i<= 10; i++ ){
for ( j = 0; j <i; j++)
System.out.print(" ");
for ( j = 1; j <= 10-i; j++ )
System.out.print("*");
System.out.println();
}
System.out.println();
for ( i = 1; i<= 10; i++ ){
for ( j = 1; j <= 10-i; j++ )
System.out.print(" ");
for ( j = 1; j <=i; j++)
System.out.print("*");
System.out.println();
}
System.out.println();
}
}
بالحد جهدها تبذل الناس أغلب
.بقاء مجرد لبقائهم، يكف الذي
يمكننا الوصننوى عفنن نفنن
loopsالمطفنننننوب بكتابنننننة
بأشكاى أخرى مختففة.
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (17 ) م. حمادة شعبان
April 2016
Exercise 5.17: An online retailer sells five products whose retail prices are
as follows: Product 1, $2.98; product 2, $4.50; product 3, $9.98; product
4, $4.49 and product 5, $6.87. Write an application that reads a series of
pairs of numbers as follows:
a) product number
b) quantity sold
Your program should use a switch statement to determine the retail price
for each product. It should calculate and display the total retail value of all
products sold. Use a sentinel-controlled loop to determine when the
program should stop looping and display the final results.
عن عبارة الناس من كثير
.صوتية ظاهرة مجرد
حاول بنفسك أوالً
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (18 ) م. حمادة شعبان
April 2016
Exercise 5.19: Assume that i = 1, j = 2, k = 3 and m = 2. What does each of
the following statements print?
a) System.out.println (i == 1 );
b) System.out.println ( j == 3 );
c) System.out.println ( ( i>= 1 ) && ( j < 4 ) );
d) System.out.println ( ( m <= 99 ) & ( k < m ) );
e) System.out.println ( ( j >= i ) || ( k == m ) );
f) System.out.println ( ( k + m < j ) | ( 3 - j >= k ) );
g) System.out.println ( !( k > m ) );
أولئك عدا األسرار يكتمون الناس كل
.بأسرارنا لهم نفض الذين
حاول بنفسك أوالً
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (19 ) م. حمادة شعبان
April 2016
Exercise 5.21: A right triangle can have sides whose lengths are all
integers. The set of three integer values for the lengths of the sides of a
right triangle is called a Pythagorean triple. The lengths of the three sides
must satisfy the relationship that the sum of the squares of two of the
sides is equal to the square of the hypotenuse. Write an application that
displays a table of the Pythagorean triples for side1, side2 and the
hypotenuse, all no larger than 500. Use a triple-nested for loop that tries
all possibilities.
كبروا، ألنهم اللعب عن الناس يتوقف ال
.اللعب عن توقفوا ألنهم يكبرون بل
حاول بنفسك أوالً
eng-hs.net ( على الموقعمجاناالشرح متوفر فيديو )
الزهراءو كيفانو بالخالديةداخل الجمعيات الرئيسية الديوان بأفرع تصوير مجانًاملونة النوتات متوفرة
net.hs-eng ,com.hs-eng بالموقعين مجاناً النوتات متوفرة hs.com-enginfo@ 260 4444 9 (20 ) م. حمادة شعبان
April 2016
Exercise: Write a program that reads two integers and print their greatest
common divisor.
GCD (18, 24) is (6).
GCD (12, 36) is (12).
GCD (7, 15) is (1).
كبروا، ألنهم اللعب عن الناس يتوقف ال
.اللعب عن توقفوا ألنهم يكبرون بل
حاول بنفسك أوالً
Recommended