You are on page 1of 19

1)Revise

C++ 2) Notes

Loops Assignment
3)

Lecture- 5

Raghav Garg
Rounds Iteration
What and
-

Output

I
Why? Repetition
->
Good
·

morning
· Initialize
·
Good Morning
initialisation condition · check
↑ a ~increment · Good
Morning
· inside loop
· increment
·
Good Morning
Good Morning
·
Repeat ·

i Y&B*56
=

S baar"Good Morning"
For Loop
for(int i = 1; i<=10; i++){

// code

}
Ques : Print hello world ‘n’ times. Take ‘n’ as
input from user in separate lines
int ni

cin;
How for loop works : the various parameters.

initialization, condition, increment/decrement


Ques : Print numbers from 1 to 100

fort loop wall variable, e is iterator


Ques : Print all the even numbers from 1 to 100
Output
1) Check
·
2
2) Inside Loop
·Y
3)Increment

i A & B Y
=
·
6

·
8

10
·

&
Ques : Print the table of 19.
Output
for (inti=19;i < 190;i
=
=
+
19(2 019

cont is endl; 038

3 · S7

&

i 193/857
=
Ques : Display this AP - 1,3,5,7,9.. upto ‘n’
terms.
n 6
=
10
a

19
an

for
the
coutiende;
3
1,d 2
MI:Mathematics
=

a
=

an a
= +
(n-1)d 2*n -

1
=

(n-1).2
+
*
= 1 + 2n -2 =2n-1
Ques : Display this AP - 1,3,5,7,9.. upto ⑤
‘n’
-
-
-

terms.
4, 7, 10, 13, 17

a 4,d
= 3
=

an a
=
(n 1)d
+
-

4
= +

(n- 1).3

=Y 3n
+
-
3

3n+ 1
=
Ques : Display this GP - 1,2,4,8,16,32,.. upto ‘n’
terms. B45678 n
=

128 256
2 by
-
price:A to 1

(ne n 8
=
8
-

S
2
Ques : Display this AP - E
100,97,94,..upto all
N

terms which are positive.


100 97949188...... 41-2-3...

Dhancha method - 1 ton Extra variable

a 100, d 3, ?,an ?
=

n
=
=
=
-

an > 0

103< 37
(-1)d 0 34

M
>
a
=>
+

heast
-

3u ↑
-

100
=> (n-1)73)
+
> 0
=> n < 103
100>
=

3(n-1)
100>
=

3n-3 I n < 34.33


While Loop i xxB11
=

I
int i = 0;
while(i<10){
// code
i++;

}
Predict the output

/for
E

int main( ) { sevance

int j ;
while ( j <= 10 ) {
cout << j << endl;
j=j+1;
}
}
Predict the output Output

I
int main( ) { x x0
=

int x = 1 ;
while ( x == 1 ) {
x=x-1;
cout << x << endl;
}
}
Do- While
Loop reless
do {

//code

} while ( another == 'y' ) ;


Find the error
int main() {

Gator
int x;
while (x = 0; x <= 255; x++) {
cout << "Ascii value of Character " << (char)('a' + x) ;
cout << " is " << x << endl;
}
}
I: Print all alphabets (uppercase) with their ASCII

values.

A 65

B 66

267

D 68
I

2 90
Thank you!

You might also like