Sunteți pe pagina 1din 23

Bharatiya Vidya Bhavan Bhavan, Kuwait Kendra K it K d

@Bharatiya Vidya Bhavan Middle East, All rights reserved

while loop

@Bharatiya Vidya Bhavan Middle East, All rights reserved

syntax:syntax:initialization expression while (test expression) hil (t t i ) { statements update expression;}


@Bharatiya Vidya Bhavan Middle East, All rights reserved

Rewrite the following for loop using a while loop 1. f (i 1; 5 1 for (i = 1 i<=5; i++) )
cout<<United Kingdom\ cout<<United Kingdom\n;
@Bharatiya Vidya Bhavan Middle East, All rights reserved

2. 2.
for (i 1; <=10; +=2) f (i = 1 i< 10 i+ 2)

cout<<UK\t; cout<<UK\t t UK UK\

@Bharatiya Vidya Bhavan Middle East, All rights reserved

Q. Write a program to input a number and print p p its multiplication table p using a while loop. g p
@Bharatiya Vidya Bhavan Middle East, All rights reserved

Q. Write a program to input a number and print p p the sum of its digits. g

@Bharatiya Vidya Bhavan Middle East, All rights reserved

Q. Write a program to input a number and p check whether it is p palindrome or not.


@Bharatiya Vidya Bhavan Middle East, All rights reserved

Q. Write a program to input a number and p check whether it is a p prime number or not.
@Bharatiya Vidya Bhavan Middle East, All rights reserved

Q. Q Write a program to input a number and check whether it is an Armstrong number or not. (A number is Armstrong when the number and sum of h th b d f the cubes of its digits are the same.) Example:- 153 ) Example:p
@Bharatiya Vidya Bhavan Middle East, All rights reserved

do--while do--while loop

@Bharatiya Vidya Bhavan Middle East, All rights reserved

syntax:syntax:initialization expression do { statements update expression; } while(condition);


@Bharatiya Vidya Bhavan Middle East, All rights reserved

Example:Example:for(k=8;k<=5;k++) { cout<<\ cout<<\nok; }

@Bharatiya Vidya Bhavan Middle East, All rights reserved

Example:Example:K=8; While(k<=5) { cout<<\ cout<<\nok; k++; }


@Bharatiya Vidya Bhavan Middle East, All rights reserved

Example:Example:K=8; do { cout<<\ cout<<\nok; k++; } while(k<=5);


@Bharatiya Vidya Bhavan Middle East, All rights reserved

Difference between 3 loops

@Bharatiya Vidya Bhavan Middle East, All rights reserved

for d hil l f and while loops are called entry controlled p p p loops or pre-check loops whereas do-while loop is called exit controlled loop ll d it t ll d l or post check loop. loop
@Bharatiya Vidya Bhavan Middle East, All rights reserved

In case of for and while loops loops, the text expression is evaluated at the beginning of the l th loop i b f i.e, before executing ti the loop body If test body. expression evaluates to false for the first time itself, the loop i never executed. l is t d
@Bharatiya Vidya Bhavan Middle East, All rights reserved

Example 1:for(int k=8;k<=5;k++) { System.out.println( ok ); System out println(ok); }

@Bharatiya Vidya Bhavan Middle East, All rights reserved

Example 2:int K=8; while(k<=5) ( ) { System.out.println(ok); S t t i tl ( k) k ; k++; }


@Bharatiya Vidya Bhavan Middle East, All rights reserved

In both example 1 and 2, body of the loop will not get executed at all.

@Bharatiya Vidya Bhavan Middle East, All rights reserved

Example 3:p int k=8; do { System.out.println(ok); k ; k++; } while(k<=5);

@Bharatiya Vidya Bhavan Middle East, All rights reserved

In case o do - while loop, the text of e oop, e e expression is evaluated at the bottom of the loop after executing the loop body So even when the body. p , test expression evaluates to false, the loop is executed at least once. In example 3, body of the loop will execute once even though the test expression is false.
@Bharatiya Vidya Bhavan Middle East, All rights reserved

S-ar putea să vă placă și