While 12100 while public class While Loop Example





דוגמא - While לולאת . 1+2+…+100 לחישוב הסכום while - • שימוש ב public class While. Loop. Example 1 { public static void main(String[] args){ int i = 1; int sum = 0; while( i <= 100 ){ sum += i; i++; } } } 1. x+= y is the same as x=x+y 2. i++ is the same as i=i+1


דוגמא - do-while לולאת . 1+2+…+100 לחישוב הסכום do-while - • שימוש ב public class Do. While. Loop. Example { public static void main(String[] args){ int i = 1; int sum = 0; do{ sum += i; i++; } while (i <= 100); } }

![דוגמא -for לולאת בעזרת לולאה public static void main(String[] args){ Scanner sc = דוגמא -for לולאת בעזרת לולאה public static void main(String[] args){ Scanner sc =](http://slidetodoc.com/presentation_image_h/0c08cec7da1996f93049b692f53247cb/image-9.jpg)
דוגמא -for לולאת בעזרת לולאה public static void main(String[] args){ Scanner sc = new Scanner(System. in); int x = sc. next. Int(); int y = sc. next. Int(); ; int res=1; for (int i = 0; i < y; i++){ res = res*x; } System. out. println(x + "^" + y + "=" + res); } • חישוב

![פתרון public class Pi. Approx { public static void main(String[] args){ double sum פתרון public class Pi. Approx { public static void main(String[] args){ double sum](http://slidetodoc.com/presentation_image_h/0c08cec7da1996f93049b692f53247cb/image-11.jpg)
פתרון public class Pi. Approx { public static void main(String[] args){ double sum = 0; double sign = 1; for (int i = 0; i < 100000; i++){ sum += sign / (2*i + 1); sign = sign *(-1); } System. out. println(sum * 4); } }


![פתרון public class Log 2 { public static void main(String[] args){ Scanner sc פתרון public class Log 2 { public static void main(String[] args){ Scanner sc](http://slidetodoc.com/presentation_image_h/0c08cec7da1996f93049b692f53247cb/image-14.jpg)
פתרון public class Log 2 { public static void main(String[] args){ Scanner sc = new Scanner(System. in); int x = sc. next. Int(); int temp = x; int count = 0; while (temp > 1){ temp = temp / 2; count++; } System. out. println("log 2(" + x+") = " + count ); } }


![פתרון public class Pythagorean. Triples { public static void main(String[] args){ for(int a פתרון public class Pythagorean. Triples { public static void main(String[] args){ for(int a](http://slidetodoc.com/presentation_image_h/0c08cec7da1996f93049b692f53247cb/image-17.jpg)
פתרון public class Pythagorean. Triples { public static void main(String[] args){ for(int a = 1; a <= 100; a++){ for(int b = a+1; b <= 100; b++){ for(int c = b + 1; c <= 100; c++){ if (a*a + b*b == c*c){ System. out. println("("+a+", "+b+", "+c+")"); } } }

![ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in); ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in);](http://slidetodoc.com/presentation_image_h/0c08cec7da1996f93049b692f53247cb/image-19.jpg)
ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in); int num = sc. next. Int(); if (num % 2 = 0){ System. out. println("even"); } else{ System. out. println("odd"); } } ? לאיזה קטגוריה נכנסת השגיאה בקוד זה
![ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in); ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in);](http://slidetodoc.com/presentation_image_h/0c08cec7da1996f93049b692f53247cb/image-20.jpg)
ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in); int num = sc. next. Int(); if (num % 2 = 0){ System. out. println("even"); } else{ System. out. println("odd"); } } לאיזה קטגוריה נכנסת השגיאה בקוד זה? קומפילציה
![ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in); ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in);](http://slidetodoc.com/presentation_image_h/0c08cec7da1996f93049b692f53247cb/image-21.jpg)
ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in); int x = sc. next. Int(); int y = sc. next. Int(); System. out. println("x / y = " + x/y); } ? האם הקוד תקין
![ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in); ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in);](http://slidetodoc.com/presentation_image_h/0c08cec7da1996f93049b692f53247cb/image-22.jpg)
ניפוי שגיאות public static void main(String[] args){ Scanner sc = new Scanner(System. in); int x = sc. next. Int(); int y = sc. next. Int(); System. out. println("x / y = " + x/y); } ! האם הקוד תקין? יתכנו שגיאות בזמן ריצה

ניפוי שגיאות . במקרים מסוימים מתקבל פלט שלא כמצופה , אולם. מספרים 3 לפניכם קוד למציאת המקסימום בין import java. util. Scanner; public class Example { public static void main(String[] args){ Scanner sc = new Scanner(System. in); int x = sc. next. Int(); int y = sc. next. Int(); int z = sc. next. Int(); int max; if (x > y && x > z){ max = x; } else if (y > x && y > z){ max = y; } else{ max = z; } System. out. println("max(x, y, z)=" + max); sc. close(); } ? האם הקוד תקין } •

ניפוי שגיאות . במקרים מסוימים מתקבל פלט שלא כמצופה , אולם. מספרים 3 לפניכם קוד למציאת המקסימום בין import java. util. Scanner; public class Example { public static void main(String[] args){ Scanner sc = new Scanner(System. in); 5>5? int x = sc. next. Int(); No! int y = sc. next. Int(); int z = sc. next. Int(); int max; if (x > y && x > z){ 5>5? max = x; Again. No! } else if (y > x && y > z){ max = y; } else{ max = z; } System. out. println("max(x, y, z)=" + max); sc. close(); } ? האם הקוד תקין שגיאה לוגית } max(5, 5, 1)=1 •

Debugging within Eclipse קביעת breakpoint

Debugging within Eclipse

debug מצב



- Slides: 30