course webpage: cs101/ cs 101 ...cs101/2019.1/lectures/lecture7.pdf · autumn 2019 cs101@cse iit...
TRANSCRIPT
![Page 1: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/1.jpg)
Autumn 2019 CS101@CSE IIT Bombay
CS 101 Computer Programming and
Utilization
Puruwith
CS101 TAs and CSE Staff
Course webpage: https://www.cse.iitb.ac.in/~cs101/
Lecture 7:
![Page 2: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/2.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•––
•––
••
02/08/19 2
![Page 3: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/3.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•–
•–
•–
•–
•–
02/08/19 3
![Page 4: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/4.jpg)
Autumn 2019 CS101@CSE IIT Bombay
while vs. repeat
repeatwhile
whilerepeat
02/08/19 4
![Page 5: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/5.jpg)
Autumn 2019 CS101@CSE IIT Bombay
repeat whileint n, x=0, sum=0;
cin >> n;
repeat(n) {
cin << x;
sum = sum + x;
}
cout << sum;
02/08/19 5
![Page 6: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/6.jpg)
Autumn 2019 CS101@CSE IIT Bombay
repeat while
int n, x=0, sum=0;
cin >> n;
repeat(n) {
cin << x;
sum = sum + x;
}
cout << sum;
02/08/19 6
int i=0, n, x=0, sum=0;
cin >> n;
while(i<n) {
cin << x;
sum = sum + x;
i++;
}
cout << sum;
![Page 7: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/7.jpg)
Autumn 2019 CS101@CSE IIT Bombay
while vs. repeat
whilerepeat
02/08/19 7
![Page 8: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/8.jpg)
Autumn 2019 CS101@CSE IIT Bombay
while repeat
02/08/19 8
// print positive integers
int n;
cin >> n;
while(n>=0) {
cout << n; // print
cin >> n; // next number
}
![Page 9: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/9.jpg)
Autumn 2019 CS101@CSE IIT Bombay
break
•
•
•
02/08/19 9
![Page 10: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/10.jpg)
Autumn 2019 CS101@CSE IIT Bombay
repeat whileint n, x=0, sum=0;
cin >> n;
repeat(n) {
cin << x;
sum = sum + x;
}
cout << sum;
02/08/19 10
int i=0, n, x=0, sum=0;
cin >> n;
while(i<n) {
cin << x;
sum = sum + x;
i++;
}
cout << sum;int i=0, n, x=0, sum=0;
cin >> n;
while(true) { //infinite loop
if(i >=n) break;
cin << x;
sum = sum + x;
i++;
}
cout << sum;
![Page 11: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/11.jpg)
Autumn 2019 CS101@CSE IIT Bombay
continue
•
•
•
02/08/19 11
![Page 12: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/12.jpg)
Autumn 2019 CS101@CSE IIT Bombay02/08/19 12
main_program{
float nextmark, sum = 0;
while (true){
cin >> nextmark;
if(nextmark > 100)
continue;
if(nextmark < 0)
break;
sum = sum + nextmark;
}
cout << sum << endl;
}
![Page 13: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/13.jpg)
Autumn 2019 CS101@CSE IIT Bombay02/08/19 13
![Page 14: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/14.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•–
– …–––
02/08/19 14
![Page 15: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/15.jpg)
Autumn 2019 CS101@CSE IIT Bombay
••
••
–––
02/08/19 15
![Page 16: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/16.jpg)
Autumn 2019 CS101@CSE IIT Bombay02/08/19 16
![Page 17: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/17.jpg)
Autumn 2019 CS101@CSE IIT Bombay
#include <simplecpp>
main_program {
int n;
int first=0; int second=1;
cin >> n;
while (n<=0) // get valid input
cin >> n;
if (n==1) cout << “0” << endl;
else if (n==2) cout << “0 1” << endl;
repeat (n-2) {
num = first + second; // state update
first = second; // state update
second = num; // state update
cout << “ “ << num;
}
}
02/08/19 17
![Page 18: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/18.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•
•
––––
02/08/19 18
![Page 19: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/19.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•–––
•––
02/08/19 19
![Page 20: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/20.jpg)
Autumn 2019 CS101@CSE IIT Bombay02/08/19 20
![Page 21: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/21.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•
•
02/08/19 21
![Page 22: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/22.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•••
••
02/08/19 22
![Page 23: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/23.jpg)
Autumn 2019 CS101@CSE IIT Bombay02/08/19 23
![Page 24: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/24.jpg)
Autumn 2019 CS101@CSE IIT Bombay02/08/19 24
#include <simplecpp>
main_program {
int n, digit;
cin >> n;
while (n<=0) // get valid input
cin >> n;
while (n>0) { // size of loop unknown
digit = n%10; // get last digit
n = n/10; // update number
cout << “ “ << digit;
}
}
![Page 25: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/25.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•
•––––
02/08/19 25
![Page 26: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/26.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•
02/08/19 26
![Page 27: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/27.jpg)
Autumn 2019 CS101@CSE IIT Bombay
••••••
02/08/19 27
![Page 28: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/28.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•
•
02/08/19 28
![Page 29: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/29.jpg)
Autumn 2019 CS101@CSE IIT Bombay
1.2.3.4.
02/08/19 29
![Page 30: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/30.jpg)
Autumn 2019 CS101@CSE IIT Bombay
do-while
•
•
![Page 31: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/31.jpg)
Autumn 2019 CS101@CSE IIT Bombay
for
•
•
•
![Page 32: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/32.jpg)
Autumn 2019 CS101@CSE IIT Bombay
for
for(initialization; condition; update)
body
••••
![Page 33: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/33.jpg)
Autumn 2019 CS101@CSE IIT Bombay
for
Initialization
Previous statement in the program
Condition
Body
Update
Next statement in the Program
False
True
![Page 34: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/34.jpg)
Autumn 2019 CS101@CSE IIT Bombay
![Page 35: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/35.jpg)
Autumn 2019 CS101@CSE IIT Bombay
•
![Page 36: Course webpage: cs101/ CS 101 ...cs101/2019.1/lectures/Lecture7.pdf · Autumn 2019 CS101@CSE IIT Bombay while vs. repeat repeat while while repeat 02/08/19 4](https://reader034.vdocuments.us/reader034/viewer/2022051909/5ffd77effdf5ea6b445970de/html5/thumbnails/36.jpg)
Autumn 2019 CS101@CSE IIT Bombay
main_program{
int n; cin >> n;
bool found = false;
for(int i=2; i < n && !found; i++){
if(n % i == 0){
found = true;
}
}
if(found) cout << "Composite.\n";
else cout << "Prime.\n";
}