Advertisement
RicardasSim

while, do while

Mar 22nd, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main (int argc, char **argv)
  5. {
  6.  
  7.     int x, count;
  8.    
  9. printf("(1)\n----------\n");    
  10.  
  11.     x = 8;
  12.  
  13.     while(x){
  14.         printf("%d ", x);
  15.         x--;
  16.     }
  17.  
  18.     printf("\n");
  19.    
  20.     x = 8;
  21.     count = 0;
  22.  
  23.     while(x){
  24.         x--;
  25.         printf("%d ", x);
  26.         count ++;
  27.     }
  28.  
  29.     printf("\ncount: %d\n",count);
  30.  
  31. printf("\n(2)\n----------\n");  
  32.  
  33.     x = 8;
  34.     count = 0;
  35.  
  36.     while(x--){
  37.         printf("%d ", x);
  38.         count ++;
  39.     }
  40.  
  41.     printf("\ncount: %d\n",count);
  42.  
  43. printf("\n(3)\n----------\n");  
  44.  
  45.     x = 8;
  46.     count = 0;
  47.  
  48.     while(--x){
  49.         printf("%d ", x);
  50.         count ++;
  51.     }
  52.  
  53.     printf("\ncount: %d\n",count);
  54.  
  55.  
  56. printf("\n(4a)\n----------\n");  
  57.  
  58.     x = 8;
  59.     count = 0;
  60.     int i = 0;
  61.    
  62.     while(i<x){
  63.         printf("%d ", i);
  64.         i++;
  65.         count ++;
  66.     }
  67.  
  68.     printf("\ncount: %d\n",count);
  69.  
  70. printf("\n(4b)\n----------\n");  
  71.  
  72.     x = 8;
  73.     count = 0;
  74.     i = 0;
  75.    
  76.     while(i<=x){
  77.         printf("%d ", i);
  78.         i++;
  79.         count ++;
  80.     }
  81.  
  82.     printf("\ncount: %d\n",count);
  83.    
  84. printf("\n(5)\n----------\n");  
  85.  
  86.     x = 8;
  87.    
  88.     do{
  89.         printf("%d ", x);
  90.         x--;
  91.     }while(x);
  92.  
  93.     printf("\n");
  94.    
  95.     x = 8;
  96.     count = 0;
  97.    
  98.     do{
  99.         x--;
  100.         printf("%d ", x);
  101.         count ++;
  102.     }while(x);
  103.  
  104.     printf("\ncount: %d\n",count);
  105.  
  106. printf("\n(6)\n----------\n");  
  107.    
  108.     x = 8;
  109.     count = 0;
  110.    
  111.     do{
  112.         printf("%d ", x);
  113.         count ++;
  114.     }while(--x);
  115.  
  116.     printf("\ncount: %d\n",count);
  117.  
  118. printf("\n(7)\n----------\n");  
  119.    
  120.     x = 8;
  121.     count = 0;
  122.    
  123.     do{
  124.         printf("%d ", x);
  125.         count ++;
  126.     }while(x--);
  127.  
  128.     printf("\ncount: %d\n",count);
  129.  
  130. printf("\n(8a)\n----------\n");  
  131.    
  132.     x = 8;
  133.     count = 0;
  134.     i = 0;
  135.    
  136.     do{
  137.         printf("%d ", i);
  138.         i++;
  139.         count ++;
  140.     }while(i<x);
  141.  
  142.     printf("\ncount: %d\n",count);
  143.  
  144. printf("\n(8b)\n----------\n");  
  145.    
  146.     x = 8;
  147.     count = 0;
  148.     i = 0;
  149.    
  150.     do{
  151.         printf("%d ", i);
  152.         i++;
  153.         count ++;
  154.     }while(i<=x);
  155.  
  156.     printf("\ncount: %d\n\n",count);
  157.    
  158. return 0;
  159. }
  160.  
  161. /*
  162.  
  163. output:
  164.  
  165. (1)
  166. ----------
  167. 8 7 6 5 4 3 2 1
  168. 7 6 5 4 3 2 1 0
  169. count: 8
  170.  
  171. (2)
  172. ----------
  173. 7 6 5 4 3 2 1 0
  174. count: 8
  175.  
  176. (3)
  177. ----------
  178. 7 6 5 4 3 2 1
  179. count: 7
  180.  
  181. (4a)
  182. ----------
  183. 0 1 2 3 4 5 6 7
  184. count: 8
  185.  
  186. (4b)
  187. ----------
  188. 0 1 2 3 4 5 6 7 8
  189. count: 9
  190.  
  191. (5)
  192. ----------
  193. 8 7 6 5 4 3 2 1
  194. 7 6 5 4 3 2 1 0
  195. count: 8
  196.  
  197. (6)
  198. ----------
  199. 8 7 6 5 4 3 2 1
  200. count: 8
  201.  
  202. (7)
  203. ----------
  204. 8 7 6 5 4 3 2 1 0
  205. count: 9
  206.  
  207. (8a)
  208. ----------
  209. 0 1 2 3 4 5 6 7
  210. count: 8
  211.  
  212. (8b)
  213. ----------
  214. 0 1 2 3 4 5 6 7 8
  215. count: 9
  216.  
  217. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement