الشروط و switch
JAVA · داخل main
int score = 85; if (score >= 90) { System.out.println("A"); } else if (score >= 80) { System.out.println("B"); } else { System.out.println("C"); }
الناتج
B
الشرط بين قوسين ( )، والكود بين { }. والعمليات المنطقية في Java: && (و)، || (أو)، ! (ليس).
JAVA · داخل main
String dept = "IT"; switch (dept) { case "IT" -> System.out.println("Floor 3"); case "HR" -> System.out.println("Floor 1"); default -> System.out.println("Ask reception"); }
الناتج
Floor 3
انتبهقارن النصوص بـ
.equals() مو بـ ==. لأن == تقارن هل هم نفس الكائن في الذاكرة، مو هل النص نفسه.JAVA · داخل main
String a = "MIS"; String b = new String("MIS"); System.out.println(a == b); System.out.println(a.equals(b));
الناتج
false true
نفس الفكرة باللغتين
Python
if score >= 90: print("A") elif score >= 80: print("B")
Java
if (score >= 90) { System.out.println("A"); } else if (score >= 80) { System.out.println("B"); }