public class if 4 public static void mainString
public class if 4{ public static void main(String[] args){ int a=2, b=3, c; if(a<b)System. out. println("a is less than b"); if(a==b)System. out. println(" you won't see this"); System. out. println(); c=a-b; if(c<0){ System. out. println(" c contains : "+c); System. out. println(" c is negative"); } else System. out. println("c is non negative"); System. out. println(); } } a is less than b c contains : -1 c is negative
start ﻣﺨﻄﻂ ﺍﻧﺴﻴﺎﺏ ﺍﻟﺒﺮﻧﺎﻣﺞ : (Flow chart) Int a, b, c, d double root 1, root 2 false A≠ 0 true Print “not quadratic solution” d=b 2 -4 ac false Print “ imaginary roots” d>=0 true Root 1=(-b+√d)/2 a Root 2=(-b-√d)/2 a Print root 1, root 2 End
import javax. swing. *; public class prog 6{ public static void main(String[ ] args){ String s 1, s 2, s 3; int a, b, c, d; double root 1, root 2; s 1= JOption. Pane. show. Input. Dialog("a? "); s 2= JOption. Pane. show. Input. Dialog("b? "); s 3= JOption. Pane. show. Input. Dialog("c? "); a=Integer. parse. Int(s 1); b=Integer. parse. Int(s 2); c=Integer. parse. Int(s 3); if(a!=0) { d=b*b-4*a*c; if(d>=0){ root 1=(-b+Math. sqrt(d))/2*a; root 2=(-b-Math. sqrt(d))/2*a; JOption. Pane. show. Message. Dialog(null, "root 1="+root 1, "results", JOption. Pane. PLAIN_MESSAGE); JOption. Pane. show. Message. Dialog(null, "root 2="+root 2, "results", JOption. Pane. PLAIN_MESSAGE); } else JOption. Pane. show. Message. Dialog(null, "Imaginary roots", "results", JOption. Pane. PLAIN_MESSAGE); } else JOption. Pane. show. Message. Dialog(null, "It is not Quadratic solution“ , "results“ , JOption. Pane. PLAIN_MESSAGE); System. exit(0); }}
: (Loops) ﺍﻟﺤﻠﻘﺎﺕ ﺍﻟﺘﻜﺮﺍﺭﻳﺔ : ﺍﻟﺒﺴﻴﻄﺔ for ﺍﻟﺼﻴﻐﺔ ﺍﻟﻌﺎﻣﺔ ﻟﺤﻠﻘﺔ : for ﺣﻠﻘﺔ /1 for(initialization; condition; iteration) statement; Start Execute Initialization Evaluate condition true Execute statement false End loop Execute Update_iteration
: 1 ﻣﺜﺎﻝ int i; for (i = 0; i<100; i++) { System. out. println( "Welcome to Java"); }
: 2 ﻣﺜﺎﻝ public class prg 15 for{ public static void main(String[ ] args){ int i; for(i=0; i<=10; i++) System. out. println(i); } }
: 1 ﻣﺜﺎﻝ • Count down from 3 to 1 for(int count = 3; count >= 1; count--) { System. out. print("T = " + count); System. out. println(" and counting"); } System. out. println(“ off!"); output: T = 3 and counting T = 2 and counting T = 1 and counting off!
: 2 ﻣﺜﺎﻝ public class sqt{ public static void main(String[ ] args){ double n, st; for(n=1. 0; n<10. 0; n++){ st=Math. sqrt(n); System. out. println("square root of "+n+" = "+st); System. out. println("---------------"); } } }
: ﺣﻠﻘﺎﺕ ﺑﺪﻭﻥ ﻣﺘﻦ int i; int s=0; for(i=1; i<=5; s+=i++); System. out. println(“s : "+s); s+=i++ s=s+i; i++;
: continue ﻭ break ﻋﺒﺎﺭﺗﻲ Break and Continue Statements: : break ( ﻋﺒﺎﺭﺓ 1 . ﺩﺍﺧﻞ ﺍﻟﺤﻠﻘﺔ ﺗﻨﻬﻲ ﺍﻟﺤﻠﻘﺔ ﺛﻢ ﻳﺘﻢ ﺗﻨﻔﻴﺬ ﺑﻘﻴﺔ ﺍﻟﺒﺮﻧﺎﻣﺞ ﻣﻦ ﺍﻟﺠﻤﻠﺔ ﺍﻟﺘﻲ ﺗﻠﻲ ﺍﻟﺤﻠﻘﺔ break ﻋﺒﺎﺭﺓ : ﻣﺜﺎﻝ ﺑﺴﻴﻂ public class Break 1{ public static void main(String[ ] args){ int num=100; for (int i=0; i<=num; i++){ if(i*i>= num)break; System. out. print(i+" "); } System. out. print(" Loop complete. "); } } 0 1 2 3 4 5 6 7 8 9 Loop complete.
public class Break 2 { public static void main(String[ ] args) { int i; for(i=1; i<4; i++) { one: { two: { three: { System. out. println("n i is "+i); if(i==1) break one; if(i==2) break two; if(i==3) break three; System. out. print(" this is won't print"); i is 1 } after block one System. out. println(" after block three"); } } System. out. println(" after block two"); System. out. println(" after block one"); } System. out. println(" after for"); i is 2 after block two after block one i is 3 after block three after block two after block one after for
: 2 ﻣﺜﺎﻝ public class Contin 2{ public static void main(String[ ] args){ int i; for ( i= 1; i<= 10; i++){ if ((i%2)!=0) continue; System. out. println(i); } }} 2 4 6 8 10
: 2 • ﻣﺜﺎﻝ public class Nest. For{ public static void main(String[ ] args){ for ( int i= 2; i<= 10; i++){ System. out. print("Factors of "+i+" is : "); for ( int j= 2; j<i; j++) if ((i%j) ==0) System. out. print(j+" "); Factors of 2 is : System. out. println(); Factors of 3 is : Factors of 4 is : 2 } Factors of 5 is : Factors of 6 is : 2 3 }} Factors of 7 is : Factors of 8 is : 2 4 Factors of 9 is : 3 Factors of 10 is : 2 5
. ﻓﺄﻨﻬﺎ ﺗﻮﻗﻒ ﻋﻤﻞ ﺍﻟﺤﻠﻘﺔ ﺍﻟﺪﺍﺧﻠﻴﺔ ﻓﻘﻂ ، ﻓﻲ ﺍﻟﺤﻠﻘﺎﺕ ﺍﻟﻤﺘﺪﺍﺧﻠﺔ break • ﻋﻨﺪﻣﺎ ﺗﺴﺘﺨﺪﻡ : ﻣﺜﺎﻝ public class Break. Nes. For{ public static void main(String[ ] args){ done: for(int i=0; i<10; i++){ for(int j=0; j<10; j++){ for(int k=0; k<10; k++){ System. out. println(k); if(k==5)break done; } System. out. print("after k loop: "); } System. out. print("after j loop: "); } 0 System. out. print("after i loop: "); 1 } } 2 3 4 5 after i loop
: ﻣﺜﺎﻝ public class Break. NFor{ public static void main(String[ ] args){ int x, y; stop 1: for( x=0; x<5; x++){ for(y=0; y<5; y++){ if(y==2)break stop 1; System. out. println("x and y : "+x+" "+y); } x and y : 0 System. out. println(); x and y : 0 for( x=0; x<5; x++){ x and y : 0 stop 2: { x and y : 1 for(y=0; y<5; y++){ x and y : 1 if(y==2)break stop 2; x and y : 2 System. out. println("x and y : "+x+" "+y); x and y : 2 } x and y : 3 } x and y : 4 }} 0 1 0 1 0 1
: ﻣﺎ ﻫﻲ ﻣﺨﺮﺟﺎﺕ ﺍﻟﺒﺮﻧﺎﻣﺞ ﺍﻟﺘﺎﻟﻲ public class grade{ public static void main(String[ ] args){ int score=60; char g; boolean honors; honors = (score >= 80); if(score >= 90) g = 'A'; else if (score >= 80) g = 'B'; else if (score >= 70) g = 'C'; else if (score >= 60) g = 'D'; else g = 'F'; System. out. println("grade of this student is: "); System. out. println(g); }}
- Slides: 29