> first >> second >> third ; cout << "The sorted list is: "; if (first <= second && second <= third) // 1st smlst cout << first <<" "<< second <<" "<< third < Download presentation Presentation is loading. Please wait. Published byDamian Cooper
Modified over 6 years ago
1
IF-ELSE כתוב תוכנית הקולטת שלושה מספרים ומדפיסה אותם בסדר עולה(ממיינת אותם) קלט:7,2,4 ,פלט:2,4,7 .
2
#include <iostream> using namespace std; {
3
עוד פתרון: #include <iostream> using namespace std; int main()
4
cout << first <<" "<< third <<" "<< second <<endl ;
5
Increment/Decrement פעולות
6
דוגמה 2: #include <iostream> using namespace std; int main() { int a=5; cout<< "a original ="<<a<<endl; cout <<"a after a++ =" <<a++<<endl; return 0; } a original =5 a after a++ =5 תוצאה: 5 ו 5 (קודם מדפים אחר כך משנה)
7
while לולאות while (condition is true) { Do_something; } do {
8
תכונות הלולאה בדרך כלל יש לכל לולאה את תכונות הבאות: אתחול מונה.
9
דוגמא: נחשב סכום ריבועים של מספרים טבעים מ 1 עד nבעזרת WHILE #include<iostream> using namespace std; int main() { int n,i; int sum=0; cout<<"Enter number n"<<endl; cin>>n; i=1; while(i<=n) sum=sum+i*i; i++; } cout<<"sum="<<sum<<endl; return 0; אתחול מונה תנאי מסיים קידום מונה
10
דוגמה:5 פתרון משוואה ליניארית: a*x+b=0 (משתמשים בלולאה)
11
דיון אלגוריתם לבדיקה האם מספר טבעי הוא ראשוני?
12
אלגוריתם יעיל יותר #include <iostream> #include <cmath>
13
תכנית מדפיסה מספר נתון בסדר ספרות הפוך וסופרת את מספר הספרות של המספר בעזרת שימוש בלולאת WHILE.
14
Do...While loop do } גוף הלולאה { while (condition is true);
15
הלולאה do while תמיד מבצעת את גוף הלולאה בדיוק פעם אחת, ורק אז בוחנת את המצב. (התנאי נבדק רק לאחר הביצוע הראשון של הלולאה ולכן, לולאה זו מבטיחה שגוף הלולאה תבוצע לפחות פעם אחת). אם התנאי בלולאה נכון, ביצוע חוזר על עצמו, אחרת הלולאה מסתיימת והתוכנית עוברת לשורות הקוד שאחרי הלולאה. הערה: לולאת do...while כולה חייבת להסתיים בנקודה-פסיק (; ).
16
דוגמא: #include <iostream> using namespace std; int main() {
17
אבל שימו לב לדוגמה הבאה:
19
דוגמא: הדפסת מסר מספר פעמים עד שקולטים מסר שמוגדר כיציאה מהתוכנית
21
לולאות FOR ניתן להגדיר לולאות ב C++ על-ידי פקודות הבאות: למשל תבנית של לולאת FOR: for ( [<initialize>]; [<condition>]; [<step>] ) { Do_something; } כאשר אנו משתמשים בפקודת for ראשית מתבצע הביטוי initialize , לאחר מכן נבדק ערכו של הביטוי condition. אם ערכו של ביטוי זה הוא אמת, נבצע את הפקודות שבין הסוגריים המסולסלות. לאחר מכן, נבצע את הביטוי step ונחזור לבדוק שוב את condition בצורה כזו נמשיך: ביצוע הקוד שבין הסוגריים המסולסלות, ביצוע הביטוי step ובדיקת ערכו של condition אם לא קיימים סוגריים מסולסלות אחרי for תבוצע הפקודה שאחרי ,for-
22
נחשב סכום ריבועים של מספרים טבעים מ 1 עד n
23
אתחול כפול ניתן לאתחל יותר ממשתנה אחד בלולאת ה- for
24
השמטת משפטים בלולאת ה- for
25
הפקודה break בשפת ++C\C קיימות פקודות המאפשרות יציאת חירום מלולאה או אי-קיום של חלק מסויים של לולאה. 1). פקודה break מאפשרת יציאת חירום מלולאה. אחרי פקודה break לולאה מסתיימת מייד ומתבצע חלק של התוכנה שאחרי הלולאה.
26
בשפת C++ נכתוב: דוגמא: התוכנית מדפיסה ריבועים של מספרים טבעים מ 1 עד 7
27
#include <iostream> using namespace std; int main() { int n=7;
28
בדוגמה זו הושמט משפט התנאי. כדי לסיים את הלולאה משתמשים בפקודת break.
29
בדוגמה זו הושמט גם משפט הקידום ולכן הלולאה תהיה אינסופית.
30
פקודה continue פקודה continue מאפשרת לא לבצע חלק מסויים של לולאה. אחרי פקודה continue לולאה אינה מסתיימת וממשיכה עד סיום של תנאי הלולאה. התוכנית מדפיסה מספרים טבעיים מ 1 עד 7 #include <iostream> using namespace std; int main() { int n=7; for (int i=1; i<=n; i++) cout <<"i="<<i <<" "; cout<<endl; return 0; } i=1 i=2 i=3 i=4 i=5 i=6 i=7 Press any key to continue . . .
31
נשתמש בפקודה continue #include <iostream> using namespace std;
32
לולאה בתוך לולאה תוכנית המדפיסה את לוח הכפל תוך שימוש שני לולאות for מקוננות
33
#include <iostream>
34
עם שימוש ב setw() - קביעת רוחב שדה הפלט
35
טבלת השוואה של לולאות for ו- while
36
case…switch בחירה המונית:
37
#include <iostream> using namespace std; int main () {
38
תפריט בעזרת פקודה Switch
39
input the number of operation you want
40
#include <iostream>
Similar presentations © 2024 SlidePlayer.com. Inc. Log in
IF-ELSE כתוב תוכנית הקולטת שלושה מספרים ומדפיסה אותם בסדר עולה(ממיינת אותם) קלט:7,2,4 ,פלט:2,4,7 .
Similar presentations
Presentation on theme: "IF-ELSE כתוב תוכנית הקולטת שלושה מספרים ומדפיסה אותם בסדר עולה(ממיינת אותם) קלט:7,2,4 ,פלט:2,4,7 ."— Presentation transcript:
int main() { int first, second, third ; // the 3 input nums cout << "Enter three integer numbers: " ; cin >> first >> second >> third ; cout << "The sorted list is: "; if (first <= second && second <= third) // 1st smlst cout << first <<" "<< second <<" "<< third <<endl ; if (first <= third && third <= second) cout << first <<" "<< third <<" "<< second <<endl ; if (second <= first && first <= third) // 2nd smlst cout << second <<" "<< first <<" "<< third <<endl ; if (second <= third && third <= first) cout << second <<" "<< third <<" "<< first <<endl ; if (third <= second && second <= first) // 3rd smlst cout << third <<" "<< second <<" "<< first <<endl ; if (third <= first && first <= second) cout << third <<" "<< first <<" "<< second <<endl ; }
int first, second, third ; // the 3 input nums cout << "Enter three integer numbers: " ; cin >> first >> second >> third ; cout << "The sorted list is: "; if (first <= second) { if( second <= third) // 1st smalest cout << first <<" "<< second <<" "<< third <<endl ; } else if(first>=third) cout << third <<" "<< first <<" "<< second <<endl ; else
} else if (second >= third ){ cout << third <<" "<< second <<" "<< first <<endl ; else cout << second <<" "<< third <<" "<< first <<endl ;
לפעמים יש צורך לבצע הגדלה(הקטנה של משתנה ב 1 בלבד) ז"א לבצע: אז במקרה כזה קיימת פקודה ++,-- יש 2 אפשרויות לבצע פקודה זו: 1). Prefix increment(decrement) 2). Postfix increment(decrement) דוגמא: #include <iostream> using namespace std; int main() { int a=5; cout<< "a original ="<<a<<endl; cout <<"a after ++a =" <<++a<<endl; return 0; } תוצאה: a original =5 a after ++a =6
תנאי מסיים. קידום מונה.
#include <iostream> using namespace std; int main() { // pitron mishvaa linearit a*x+b=0 double a, b, x; int menu = 1; while (menu != 0) cout << "enter a, b "<<endl; cin>>a>>b; if (a==0.0 && b==0.0){ cout<<“solution any number"<<endl; } else if (a==0.0 && b!=0.0) { cout<<“no solutaion"<<endl; else{ x = b/a; cout<<“one solution x= "<<x<<endl; cout << "menu:\n 1 to continue \n 0 to exit\nmenu = "; cin >> menu;
האם ניתן לייעל את הבדיקה? באילו דרכים? חישוב כל המספרים הראשונים עד n. #include <iostream> using namespace std; int main() { cout << "enter number "; int number; cin >> number; int dev = 2; bool prime = true; while (dev<number) if (number%dev == 0) prime = false; dev = dev + 1; } if (prime) cout<<"number is prime"<<endl; else cout<<"number is not prime"<<endl;
using namespace std; int main() { cout << "enter number "; int number; cin >> number; int dev = 2; bool prime = true; while (dev<=(int)sqrt((double)number) && prime) if (number%dev == 0) prime = false; dev = dev + 1; } if (prime) cout<<"number is prime"<<endl; else cout<<"number is not prime"<<endl;
#include<iostream> using namespace std; int main() { int num, new_num = 0, r, count = 0; cout << "enter num: "; cin >> num; while (num) r = num % 10; new_num = new_num * 10 + r; count++; num = num / 10; cout << "new num=" << new_num << endl; cout << "num of digits ="<< count << endl; return 0; }
while (condition is true);
int main() { int i = 0; do cout<< “good morning" << endl; i++; } while (i <5); return 0;
#include <iostream> using namespace std; int main() { int i = 100,n=0; do cout<< "Boker tov" << endl; i++; n++;//loop counter } while (i <5); cout<<"After loop i="<<i<<endl; cout<<"After loop n="<<n<<endl; return 0;
#include <iostream> #include <string> using namespace std; // void main() { string yword; do cout << "Enter your message or 'bye' to complete the program: " << endl; cin >> yword; } while ( yword != "bye" ); // cout << "\nThank You\n";
דוגמא: נחשב סכום ריבועים של מספרים טבעים מ 1 עד n #include<iostream> using namespace std; int main() { int n; int sum=0; cout<<"Enter number n"<<endl; cin>>n; for (int i=1; i<=n; i++) sum=sum+i*i; cout<<"sum="<<sum<<endl; return 0; }
#include <iostream< using namespace std; int main() { int i, j; for (i=0,j=0; i<10; i=i+1, j++) cout << i+j ", ">>; } cout<<endl; הפלט הוא:
השמטת משפטים בלולאת ה- for. בלולאת ה- for ניתן להשמיט חלק מן המשפטים אשר בכותרת ואף כולם. בדוגמה זו הושמט משפט האתחול והאתחול מתבצע לפני הלולאה ולכן הקוד תקין. int i=0; for (; i<10; i=i+1) { cout<<" i="<<i<<“,”; } cout<<endl;
בשפת C++ נכתוב: #include <iostream> using namespace std; int main() { int n=7; for (int i=1; i<=n; i++) cout << i*i<<" "; return 0; } פלט על ה מסך:
נשתמש בפקודה break : #include <iostream> using namespace std; int main() { int n=7; for (int i=1; i<=n; i++) if(i==5) break ; сout << i*i<<" "; } return 0; פלט על המסך אחרי i= =5 הלולאה הסתיימה
int main() } int i=0; for (; ; i=i+1) cout<<" i="<<i", ">>; if (i == 4) break; cout<<endl; return 0; { הפלט הוא:
int main() { int i=0; for (; ; ) cout<<" i="<<i<<", "; i = i+1; if (i == 4) break; cout<<endl; return 0; } i=0, i=1, i=2, i=3, Press any key to continue . . .
int main() { int n=7; for (int i=1; i<=n; i++) if(i==5) continue; cout <<"i="<<i <<" "; } cout<<endl; return 0; i=1 i=2 i=3 i=4 i=6 i=7 Press any key to continue . . .
using namespace std; int main() { int i,j; for(i=1;i<=10;i++) for(j=1;j<=10;j++) { if (i*j<10) cout<<" "<<i*j<<" "; else cout<<i*j<<" "; } cout<<endl; return 0; Press any key to continue . . .
#include<iostream> #include<iomanip> using namespace std; int main() { for (int i = 1; i <=10; i++) for (int j = 1; j <=10; j++) cout << setw(5) << i*j; } cout << endl;
#include <iostream> using namespace std; // int main() { int main () int num=0; // while while( num <= 10 ) cout << "num " << num << endl; num++; } // end while cout << "\nThank You\n"; // for for (int num=0; num<=10; num++ ) // end for
לפעמים כאשר יש בחירה לוגית מורכבת מהרבה רמות נוח להשתמש באוסף הפקודות: switch (expression) { case (value1): Execute_something; break; case (value2): . case (valueN): default: } switch מקבל משתנה או ביטוי (int או char) ואז התוכנית פונה אל שורת ה- case המתאימה ומבצעת את הפקודות הכתובות אחריה עד שהיא מגיעה לפקודת break-. -מטרת הפקודה break היא למנוע את המשך ביצוע הפקודות הבאות ב- case-ים שבאים אחרי. אם הערך המתקבל ממשפט ה- switch אינו תואם לאף אחד מה- case-ים מתבצעות ההוראות שאחרי פקודת ה- default. הפקודה default לא חייבת להופיע אך במקרה זה אם הקבוע שחושב ב switch אינו תואם את אחד ה- case- ים לא יבוצע דבר.
#include <iostream> using namespace std; int main () { char selection; cout << "Are you sure ? (y/n) : "; cin >> selection; switch (selection) case 'y' : cout << "yes" << endl; break; case 'n' : cout << "no" << endl; default: cout << "What? " << endl; }
#include <iostream> using namespace std; int main() { int k; cout<<"input the number of operation you want\n"; cout<<"1:for reading a file\n"; cout<<"2:for saving a file\n"; cout <<"3:for saving a file an another name\n"; cout<<"0-for quitting\n"; cin>>k; switch(k) case 1: cout<<"reading\n"; break; case 2: cout<<"saving\n"; case 3: cout<<"saving file in another name\n"; case 0: cout<<"quitting\n"; default: cout<<"you have pressed on a wrong number\n"; } return 0;
1:for reading a file 2:for saving a file 3:for saving a file an another name 0-for quitting 2 saving Press any key to continue . . . input the number of operation you want 1:for reading a file 2:for saving a file 3:for saving a file an another name 0-for quitting quitting Press any key to continue . . .
using namespace std; int main() { int menu, numb1, numb2, total,exit=1; while (exit) cout << "enter two numbers \n"; cin >> numb1 >> numb2 ; cout <<"numl= "<<numb1<<endl; cout <<"num2= "<<numb2<<endl; cout <<"\nenter choice\n"; cout <<"1= addition\n"; cout <<"2= substraction\n"; cin >> menu; switch( menu ) case 1: total = numb1 + numb2; cout <<numb1<< " + " <<numb2 << " = " << total << endl; break; case 2: total = numb1 - numb2; cout << numb1 << " - " << numb2 << " = " << total << endl; default: cout <<"Invalid option selected\n"; } cout <<"enter 0-for exit,1-for continue :"; cin>>exit; return 0;
Similar presentations
All rights reserved.