• No results found

CS 101

N/A
N/A
Protected

Academic year: 2022

Share "CS 101 "

Copied!
36
0
0

Loading.... (view fulltext now)

Full text

(1)

Autumn 2019 CS101@CSE IIT Bombay

CS 101

Computer Programming and Utilization

Puru

with

CS101 TAs and CSE Staff

Course webpage: https://www.cse.iitb.ac.in/~cs101/

Lecture 7:

(2)

Autumn 2019 CS101@CSE IIT Bombay

– –

– –

02/08/19 2

(3)

Autumn 2019 CS101@CSE IIT Bombay

02/08/19 3

(4)

Autumn 2019 CS101@CSE IIT Bombay

while vs. repeat

repeat

while

while

repeat

02/08/19 4

(5)

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 5

(6)

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;

(7)

Autumn 2019 CS101@CSE IIT Bombay

while vs. repeat

while

repeat

02/08/19 7

(8)

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

}

(9)

Autumn 2019 CS101@CSE IIT Bombay

break

02/08/19 9

(10)

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 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;

(11)

Autumn 2019 CS101@CSE IIT Bombay

continue

02/08/19 11

(12)

Autumn 2019 CS101@CSE IIT Bombay

02/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;

}

(13)

Autumn 2019 CS101@CSE IIT Bombay

02/08/19 13

(14)

Autumn 2019 CS101@CSE IIT Bombay

– …

– – –

02/08/19 14

(15)

Autumn 2019 CS101@CSE IIT Bombay

– – –

02/08/19 15

(16)

Autumn 2019 CS101@CSE IIT Bombay

02/08/19 16

(17)

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

(18)

Autumn 2019 CS101@CSE IIT Bombay

– – – –

02/08/19 18

(19)

Autumn 2019 CS101@CSE IIT Bombay

– – –

– –

02/08/19 19

(20)

Autumn 2019 CS101@CSE IIT Bombay

02/08/19 20

(21)

Autumn 2019 CS101@CSE IIT Bombay

02/08/19 21

(22)

Autumn 2019 CS101@CSE IIT Bombay

02/08/19 22

(23)

Autumn 2019 CS101@CSE IIT Bombay

02/08/19 23

(24)

Autumn 2019 CS101@CSE IIT Bombay

02/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;

}

}

(25)

Autumn 2019 CS101@CSE IIT Bombay

– – – –

02/08/19 25

(26)

Autumn 2019 CS101@CSE IIT Bombay

02/08/19 26

(27)

Autumn 2019 CS101@CSE IIT Bombay

02/08/19 27

(28)

Autumn 2019 CS101@CSE IIT Bombay

02/08/19 28

(29)

Autumn 2019 CS101@CSE IIT Bombay

1.

2.

3.

4.

02/08/19 29

(30)

Autumn 2019 CS101@CSE IIT Bombay

do-while

(31)

Autumn 2019 CS101@CSE IIT Bombay

for

(32)

Autumn 2019 CS101@CSE IIT Bombay

for

for(initialization; condition; update) body

(33)

Autumn 2019 CS101@CSE IIT Bombay

for

Initialization

Previous statement in the program

Condition

Body

Update

Next statement in the Program False

True

(34)

Autumn 2019 CS101@CSE IIT Bombay

(35)

Autumn 2019 CS101@CSE IIT Bombay

(36)

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";

}

References

Related documents

• If you read into a char type variable, the ASCII code of the typed character gets stored. • If you type the character ‘f’, the ASCII value of ‘f’ will

Going backward from final winner sequence which ends in state S2 (indicated By the 2 nd tuple), we recover the sequence..2. The HMM,

Going backward from final winner sequence which ends in state S2 (indicated By the 2 nd tuple), we recover

• Condition is considered true if exp1 relates to exp2 as per the specified relational operator relop.. Flowchart of the

But you will learn enough basic principles of programming to enable you to learn to program in different environment relatively easily..?. So Let's

iteration, skipping the statements from the continue statement to the end of the loop

• If the condition is true originally, then the value of some variable used in condition must change in the execution of body, so that eventually condition becomes false. •

Variables are regions of memory which can store values Variables have a type, as decided at the time of creation Choose variable names to fit the purpose for which the. variable