Sunum yükleniyor. Lütfen bekleyiniz

Sunum yükleniyor. Lütfen bekleyiniz

Bölüm 5 – Kontrol İfadeleri : 2.Kısım

Benzer bir sunumlar


... konulu sunumlar: "Bölüm 5 – Kontrol İfadeleri : 2.Kısım"— Sunum transkripti:

1 Bölüm 5 – Kontrol İfadeleri : 2.Kısım
İçerik Giriş Bir Kontrollü Döngü İfadesinde Olması Gerekenler for Döngü Deyimleri for Döngü Deyimi ile alakalı Örnekler do…while Döngü Deyimi switch Çok Seçmeli Komut break ve continue Komutları Etiketli break ve continue Komutları Mantıksal Operatörler Yapısal Programlama Özeti

2 Yapısal programlama komutlarına devam ediyoruz.
5.1 Giriş Yapısal programlama komutlarına devam ediyoruz. Java’nın kontrol komutlarını hatırlayalım

3 5.2 Bir Kontrollü Döngü İfadesinde Olması Gerekenler
Sayaç-kontrollü döngü : Kontrol değişkeni (döngü sayacı) Kontrol değişkenine ilk değer verme Her dönüşte kontrol değişkenini artırma/azaltma Kontrol değişkeninin son değere ulaşıp ulaşmadığını döngüdeki şart ile tesbiti

4 for ( int counter = 1; counter <= 10; counter++ )
5.3 for Döngü Komutu for ( int counter = 1; counter <= 10; counter++ ) Kontrol değişkenin bir artımı Kontrol değişkeni Kontrol değişkenin son değeri for anahtar kelime Döngünün şartı Kontrol değişkenin ilk değeri Noktalı virgül ile ayrım Noktalı virgül ie ayrım Fig. 5.3 for deyiminin ayrıntılı anlatımı.

5 5.3 for Döngü Yapısı for ( ilk değer verme; döngü şartı; artış ) { komutlar; } Aynı ifade aşağıdaki gibi de yazılabilir: İlk değer verme; while ( döngü şartı ) { komutlar; artış; }

6 5.4 for İfadeleri için Örnekler
for deyiminde kontrol değişkenin değişimi Kontrol değişkenini 1’den başlatıp 100 ‘e varıncaya kadar 1 artışla ilerletecek for deyimi for ( int i = 1; i <= 100; i++ ) Kontrol değişkenini 100’den başlatıp 1‘e varıncaya kadar 1’er 1’er azaltıp ilerletecek for deyimi for ( int i = 100; i >= 1; i-- ) Kontrol değişkenini 7’den başlatıp 7’şer artışla 77 kadar ilerlecek for deyimi for ( int i = 7; i <= 77; i += 7 )

7 increment number by 2 each iteration
// Fig. 5.5: Sum.java // 1’den 100’e kadar çift sayıların toplamı. import javax.swing.JOptionPane; 4 public class Toplam { 6 public static void main( String args[] ) { int toplam = 0; // ilk değer verme 10 // 2 den 100’e kadar çift sayıların toplamı for ( int sayi = 2; sayi <= 100; sayi += 2 ) toplam += sayi; 14 // display results JOptionPane.showMessageDialog( null, “Toplam " + toplam, "Total Even Integers from 2 to 100", JOptionPane.INFORMATION_MESSAGE ); 19 System.exit( 0 ); // terminate application 21 } // end main 23 24 } // end class Sum increment number by 2 each iteration Sum.java Line 12

8 Interest.java Lines 13-15 Line 18 Line 19
// Fig. 5.6: Interest.java // Calculating compound interest. import java.text.NumberFormat; // class for numeric formatting import java.util.Locale; // class for country-specific information 5 import javax.swing.JOptionPane; import javax.swing.JTextArea; 8 public class Interest { 10 public static void main( String args[] ) { double amount; // amount on deposit at end of each year double principal = ; // initial amount before interest double rate = 0.05; // interest rate 16 // create NumberFormat for currency in US dollar format NumberFormat moneyFormat = NumberFormat.getCurrencyInstance( Locale.US ); 20 // create JTextArea to display output JTextArea outputTextArea = new JTextArea(); 23 // set first line of text in outputTextArea outputTextArea.setText( "Year\tAmount on deposit\n" ); 26 Java treats floating-points as type double Interest.java Lines Line 18 Line 19 NumberFormat can format numeric values as currency Display currency values with dollar sign ($)

9 Calculate amount with for statement
// calculate amount on deposit for each of ten years for ( int year = 1; year <= 10; year++ ) { 29 // calculate new amount for specified year amount = principal * Math.pow( rate, year ); 32 // append one line of text to outputTextArea outputTextArea.append( year + "\t" + moneyFormat.format( amount ) + "\n" ); 36 } // end for 38 // display results JOptionPane.showMessageDialog( null, outputTextArea, "Compound Interest", JOptionPane.INFORMATION_MESSAGE ); 42 System.exit( 0 ); // terminate the application 44 } // end main 46 47 } // end class Interest Calculate amount with for statement Interest.java Lines 28-31

10 5.5 do…while Döngü İfadesi
do…while yapısı while komutuna benzer. Bu döngü yapısında döngü içindeki blok en az bir defa çalışır.

11 Fig. 5.8 do…while akış diyagramı.
Blok kodları [true] [false] şart Fig. 5.8 do…while akış diyagramı.

12 5.6 switch Çok-Şeçmeli Yapılar
switch deyimi Çok seçmeli zamanlar için kullanılır

13 SwitchTest.java Lines 16-21: Getting user’s input
// Fig. 5.9: SwitchTest.java // Drawing lines, rectangles or ovals based on user input. import java.awt.Graphics; 4 import javax.swing.*; 6 public class SwitchTest extends JApplet { int choice; // user's choice of which shape to draw 9 // initialize applet by obtaining user's choice public void init() { String input; // user's input 14 // obtain user's choice input = JOptionPane.showInputDialog( "Enter 1 to draw lines\n" + "Enter 2 to draw rectangles\n" + "Enter 3 to draw ovals\n" ); 20 choice = Integer.parseInt( input ); // convert input to int 22 } // end method init 24 // draw shapes on applet's background public void paint( Graphics g ) { super.paint( g ); // call paint method inherited from JApplet 29 for ( int i = 0; i < 10; i++ ) { // loop 10 times (0-9) 31 SwitchTest.java Lines 16-21: Getting user’s input Get user’s input in JApplet

14 user input (choice) is controlling expression
switch ( choice ) { // determine shape to draw 33 case 1: // draw a line g.drawLine( 10, 10, 250, 10 + i * 10 ); break; // done processing case 37 case 2: // draw a rectangle g.drawRect( 10 + i * 10, 10 + i * 10, i * 10, 50 + i * 10 ); break; // done processing case 42 case 3: // draw an oval g.drawOval( 10 + i * 10, 10 + i * 10, i * 10, 50 + i * 10 ); break; // done processing case 47 default: // draw string indicating invalid value entered g.drawString( "Invalid value entered", , 20 + i * 15 ); 51 } // end switch 53 } // end for 55 } // end method paint 57 58 } // end class SwitchTest user input (choice) is controlling expression switch statement determines which case label to execute, depending on controlling expression SwitchTest.java Line 32: controlling expression Line 32: switch statement Line 48 default case for invalid entries

15 SwitchTest.java

16 SwitchTest.java

17 Fig. 5.10 break komutu ile switch akış diyagramı.
case a action(s) break default action(s) [true] case b action(s) case z action(s) . [false] case a case b case z Fig break komutu ile switch akış diyagramı.

18 5.7 break ve continue deyimleri
break/continue Programın akış sırasını değiştirir. break deyimi Kontrol yapısından çıkışı sağlar. while, for, do…while or switch ifadelerinde kullanılır continue deyimi Döngünün başına döner. while, for or do…while ifadelerinde kullanılır.

19 BreakTest.java Line 12 Lines 14-15
// Fig. 5.11: BreakTest.java // Terminating a loop with break. import javax.swing.JOptionPane; 4 public class BreakTest { 6 public static void main( String args[] ) { String output = ""; int count; 11 for ( count = 1; count <= 10; count++ ) { // loop 10 times 13 if ( count == 5 ) // if count is 5, break; // terminate loop 16 output += count + " "; 18 } // end for 20 output += "\nBroke out of loop at count = " + count; JOptionPane.showMessageDialog( null, output ); 23 System.exit( 0 ); // terminate application 25 } // end main 27 28 } // end class BreakTest BreakTest.java Line 12 Lines 14-15 exit for structure (break) when count equals 5 Loop 10 times

20 ContinueTest.java Line 11 Lines 13-14
// Fig. 5.12: ContinueTest.java // Continuing with the next iteration of a loop. import javax.swing.JOptionPane; 4 public class ContinueTest { 6 public static void main( String args[] ) { String output = ""; 10 for ( int count = 1; count <= 10; count++ ) { // loop 10 times 12 if ( count == 5 ) // if count is 5, continue; // skip remaining code in loop 15 output += count + " "; 17 } // end for 19 output += "\nUsed continue to skip printing 5"; JOptionPane.showMessageDialog( null, output ); 22 System.exit( 0 ); // terminate application 24 } // end main 26 27 } // end class ContinueTest ContinueTest.java Line 11 Lines 13-14 Skip line 16 and proceed to line 11 when count equals 5 Loop 10 times

21 5.8 Etiketli break ve continue İfadeleri
Etiketli blok {} arasındaki kodlar Parantez öncesi blokğu adalandırıcı etiket Etiketli break ifadesi İçinde bulunduğu bloktan çıkmasını sağlar. Blok sonından çalışmaya devam eder. Etiketli continue ifadesi Blok içindeki kodları atlar Etiketin başına gelerek programa devam eder.

22 BreakLabelTest.java Line 11 Line 14 Line 17 Lines 19-20
// Fig. 5.13: BreakLabelTest.java // Labeled break statement. import javax.swing.JOptionPane; 4 public class BreakLabelTest { 6 public static void main( String args[] ) { String output = ""; 10 stop: { // labeled block 12 // count 10 rows for ( int row = 1; row <= 10; row++ ) { 15 // count 5 columns for ( int column = 1; column <= 5 ; column++ ) { 18 if ( row == 5 ) // if row is 5, break stop; // jump to end of stop block 21 output += "* "; 23 } // end inner for 25 output += "\n"; 27 } // end outer for 29 BreakLabelTest.java Line 11 Line 14 Line 17 Lines 19-20 stop is the labeled block Loop 10 times Nested loop 5 times Exit to line 35 (next slide)

23 BreakLabelTest.java 30 // following line is skipped
output += "\nLoops terminated normally"; 32 } // end labeled block 34 JOptionPane.showMessageDialog( null, output, "Testing break with a label", JOptionPane.INFORMATION_MESSAGE ); 38 System.exit( 0 ); // terminate application 40 } // end main 42 43 } // end class BreakLabelTest BreakLabelTest.java

24 ContinueLabelTest.java Line 11 Line 14 Line 17 Lines 21-22
// Fig. 5.14: ContinueLabelTest.java // Labeled continue statement. import javax.swing.JOptionPane; 4 public class ContinueLabelTest { 6 public static void main( String args[] ) { String output = ""; 10 nextRow: // target label of continue statement 12 // count 5 rows for ( int row = 1; row <= 5; row++ ) { output += "\n"; 16 // count 10 columns per row for ( int column = 1; column <= 10; column++ ) { 19 // if column greater than row, start next row if ( column > row ) continue nextRow; // next iteration of labeled loop 23 output += "* "; 25 } // end inner for 27 } // end outer for ContinueLabelTest.java Line 11 Line 14 Line 17 Lines 21-22 nextRow is the labeled block Loop 5 times Nested loop 10 times continue to line 11 (nextRow)

25 ContinueLabelTest.java 29
JOptionPane.showMessageDialog( null, output, "Testing continue with a label", JOptionPane.INFORMATION_MESSAGE ); 33 System.exit( 0 ); // terminate application 35 } // end main 37 38 } // end class ContinueLabelTest ContinueLabelTest.java

26 Mantıksal operatörler
5.9 Mantısal Operatörler Mantıksal operatörler Daha karışık şartlar oluşturmak amaçlı Basit şartları birleştirmek amaçlı kullanılır. Java mantıksal operatörler && (koşul AND) & (mantıksal AND) || (koşul OR) | (mantıksal OR) ^ (dışlayan OR) ! (mantıksal NOT)

27

28

29 LogicalOperators.java Lines 16-20 Lines 23-27
// Fig. 5.19: LogicalOperators.java // Logical operators. import javax.swing.*; 4 public class LogicalOperators 6 public static void main( String args[] ) { // create JTextArea to display results JTextArea outputArea = new JTextArea( 17, 20 ); 11 // attach JTextArea to a JScrollPane so user can scroll results JScrollPane scroller = new JScrollPane( outputArea ); 14 // create truth table for && (conditional AND) operator String output = "Logical AND (&&)" + "\nfalse && false: " + ( false && false ) + "\nfalse && true: " + ( false && true ) + "\ntrue && false: " + ( true && false ) + "\ntrue && true: " + ( true && true ); 21 // create truth table for || (conditional OR) operator output += "\n\nLogical OR (||)" + "\nfalse || false: " + ( false || false ) + "\nfalse || true: " + ( false || true ) + "\ntrue || false: " + ( true || false ) + "\ntrue || true: " + ( true || true ); 28 LogicalOperators.java Lines Lines 23-27 Conditional AND truth table Conditional OR truth table

30 LogicalOperators.java Lines 30-34 Lines 37-41 Lines 44-48 Lines 51-53
// create truth table for & (boolean logical AND) operator output += "\n\nBoolean logical AND (&)" + "\nfalse & false: " + ( false & false ) + "\nfalse & true: " + ( false & true ) + "\ntrue & false: " + ( true & false ) + "\ntrue & true: " + ( true & true ); 35 // create truth table for | (boolean logical inclusive OR) operator output += "\n\nBoolean logical inclusive OR (|)" + "\nfalse | false: " + ( false | false ) + "\nfalse | true: " + ( false | true ) + "\ntrue | false: " + ( true | false ) + "\ntrue | true: " + ( true | true ); 42 // create truth table for ^ (boolean logical exclusive OR) operator output += "\n\nBoolean logical exclusive OR (^)" + "\nfalse ^ false: " + ( false ^ false ) + "\nfalse ^ true: " + ( false ^ true ) + "\ntrue ^ false: " + ( true ^ false ) + "\ntrue ^ true: " + ( true ^ true ); 49 // create truth table for ! (logical negation) operator output += "\n\nLogical NOT (!)" + "\n!false: " + ( !false ) + "\n!true: " + ( !true ); 54 outputArea.setText( output ); // place results in JTextArea 56 Boolean logical AND truth table LogicalOperators.java Lines Lines Lines Lines 51-53 Boolean logical inclusive OR truth table Boolean logical exclusive OR truth table Logical NOT truth table

31 57 JOptionPane.showMessageDialog( null, scroller,
"Truth Tables", JOptionPane.INFORMATION_MESSAGE ); 59 System.exit( 0 ); // terminate application 61 } // end main 63 64 } // end class LogicalOperators LogicalOperators.java

32

33 5.10 Yapısal Programlama (Özet)
Sıralı Yapılar Java daki yerleşik ifadeler Seçimli Yapılar if, if…else ve switch Tekrarlana Yapılar while, do…while ve for

34 [t] [f] break Tekrarlı while statement do while statement for statement Seçimli Sıralı if else statement (double selection) if statement (single selection) switch statement (multiple selection) . default Fig Java’s single-entry/single-exit sequence, selection and repetition statements.

35 Fig. 5.23 En basit akış diyagramı.
action state Fig En basit akış diyagramı.

36 . action state apply Rule 2 Fig En basit akış diyagramına kural 2 ‘yi uygulayıcı yeni sıralı komutlar ekleniyor.

37 action state apply Rule 3 [f] [t] Fig Basit akış diyagramına kural 3 yani kontrol ve tekrarlı yapıları akleyebilirz.

38 Fig. 5.26 Hatalı akış diyagramı.
action state Fig Hatalı akış diyagramı.


"Bölüm 5 – Kontrol İfadeleri : 2.Kısım" indir ppt

Benzer bir sunumlar


Google Reklamları