Friday, May 15, 2015

While loop


LOOP:--
Loop Is Define as computer programming sequence of instruction that is continually repeated until a certain condition reached. if the condition has reached to the first instruction then loop repeated the sequence. The result of loop is repeated continually until the operation is success.
 While loop is also use for repeating block of statement in the programming until some condition is made. In case of while loop initialization is written before while condition is written inside while & increment /decrements is written inside while loop. It is used for both expected & unexpected ending.
Syntax :-
                Initialization;
                While (condition)
                { …………………;
                  ………………….;
                  ………………….;
                Increment/decrement;
                }

Example :-
                Write a program to display natural number from 1-100.
                                Int main()
                {
                                Int i = 1;
                                While (i < = 100);
                                {
                                Printf(“%d/t”,i);
                                i++;
                                retrun(0);
                                }

                Write a program to display even  number from 10-100.
                                Int main
                {
                                Int i = 10;
                                While (i < = 100);
                                {
                                If(i%2==0)
                                Printf(“%d is even ”,i);
                                i++;
                                retrun(0);
                                }

Write a program to display all upper case alphabet A-Z.
                                Int main
                {
                                Char ch = ‘A’
                                While(ch < = ‘Z’)
                                {
                                Printf(“%c/t”, ch);
                                Ch ++;
                                Return(0);
                                }



Write a program to display your name as many as time the user press y.
                                Int main
                {
                                Char ch = ‘Y’
                                While(ch = = ‘Y’ // ch == ‘Y’)
                                {
                                Printf(“Michal”);
                                Printf(“/n Do You want to continus Y/n”);
                                Scant(“%C”, & ch);
}
                                Return(0);
                                }


No comments:

Post a Comment