Mukezh

Loops Session

Dec 18th, 2020 (edited)
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. Loops
  2.  
  3. printf("%d",1); Loop provision
  4. printf("%d",1);
  5. printf("%d",1);
  6. printf("%d",1);
  7. printf("%d",1);
  8.  
  9. Loop
  10. > While
  11. > for
  12. > do while
  13.  
  14. WHILE LOOP
  15. ==========
  16.  
  17. while(condition)
  18. {
  19. while body;
  20. }
  21.  
  22. a=1;
  23. while(a<=5)
  24. (
  25. )
  26. Infinite Loop. There is no increment.
  27. To run a Loop we need to increase the count each time.
  28.  
  29. a=1;
  30. while(a<=5)
  31. (
  32. a=a+1;
  33. )
  34.  
  35. a=1;
  36. while(a<=10)
  37. (
  38. a=a+2;
  39. ) 5 Times
  40.  
  41. a=5;
  42. while(a<=1)
  43. (
  44. a=a-1;
  45. ) 0 TIMES
  46.  
  47. a=5;
  48. while(a>=1) A= 5 - 0 Big to Small (-) (>)
  49. ( A=0 - 5 Small to Big (+) (<)
  50. a=a-1;
  51. )
  52. Loop is otherwise called Iteration.
  53. a(variable) is Iterator.
  54.  
  55. a=1;
  56. while(a<=5)
  57. {
  58. printf("%d",1);
  59. a=a+1;
  60. } O/P : 11111
  61.  
  62. printf("%d",1);
  63. printf("%d",2);
  64. printf("%d",3);
  65. printf("%d",4);
  66. printf("%d",5);
  67. ***************************
  68. a=1;
  69. while(a<=5)
  70. {
  71. printf("%d",a);
  72. a=a+1;
  73. } O/P : 12345
  74. ******************************
  75. a=5; a 5 4 3 2 1
  76. while(a<=5) 1 2 3 4 5
  77. {
  78. printf("%d", 6-a );
  79. a=a-1;
  80. } O/P : 12345
  81. **************************
  82. a=5;
  83. while(a>=5)
  84. {
  85. printf("%d",a);
  86. a=a-1;
  87. }
  88.  
  89. -----------------------
  90. a=b=1;
  91. while(a)
  92. {
  93. } Valid
  94. ---------------------------------------
  95. a=b=1;
  96. while(a)
  97. {
  98. a=b<=3;
  99. printf("%d %d \n", a, b);
  100. b=b+1;
  101. }
  102. printf("%d %d \n",a+10,b+10);
  103. O/p : 1 1
  104. 1 2
  105. 1 3
  106. 0 4
  107. 10 15
  108. -------------------------------------------
  109. a=b=10;
  110. while (a)
  111. {
  112. a= b < = 13;
  113. printf("%d %d \n",a,b );
  114. b=b+1;
  115. }
  116. printf("%d %d \n",a+10,b+10);
  117. //////////////////////////////////////////////////////
  118. -----------For Loop-----------
  119. for(intialization; Condition; increment or decrement)
  120. {
  121. for body;
  122. }
  123.  
  124. *****************************
  125. void main()
  126. {
  127. int a;
  128.  
  129. for(a=1;a<=3;a=a+1) a=1 2 3 4
  130. {
  131. printf("a");
  132. }
  133.  
  134. } O/P : aaa
  135. *****************************
  136.  
  137. int a,b;
  138. for(a=1,b=10;a<=b; a=a+1,b=b+1)
  139. {
  140. printf("%d %d ",a,b);
  141. }
  142. printf("%d %d ",a+10,b+10);
  143. O/P : 1 10 2 9 3 8 4 7 5 6 16 15
  144.  
  145. *******************************
  146. int a,b;
  147. for(a=b=1; a ;b=b+1)
  148. {
  149. a=b<=3;
  150. printf("%d %d ",a,b);
  151. }
  152. printf("%d %d ",a+10,b+10);
  153.  
  154. O/P :
  155.  
  156. *******************************
  157. void main ()
  158. {
  159. for( ; ; ) Valid...
  160. {
  161. printf("Hello"); Infinite times Hello
  162. }
  163. }
  164. Condition Will be true if it is empty
  165. ********************************
  166. void main()
  167. {
  168. int a=1;
  169. for( ;a<=3; )
  170. {
  171. printf("%d"a);
  172. a=a+1;
  173. }
  174. }
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
Add Comment
Please, Sign In to add comment