C/Language Basics/While — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Версия 14:20, 25 мая 2010

a while loop nested in for loop

#include <stdio.h>
void main()
{
   long sum = 1L; 
   int i = 1;     /* Outer loop control variable     */
   int j = 1;     /* Inner loop control variable     */
   int count = 10; /* Number of sums to be calculated */

   for( i = 1 ; i <= count ; i++ )
   {
     sum = 1L; /* Initialize sum for the inner loop */
     j=1;      /* Initialize integer to be added    */
     printf("\n1");
     /* Calculate sum of integers from 1 to i */
     while(j < i)
     {
       sum += ++j;
       printf("+%d", j); /* Output +j on the same line */
     }
     printf(" = %ld\n", sum); /* Output  = sum */
   }
}


Use getchar inside while loop

  
#include <stdio.h>
#include <conio.h>
int main(void)
{
  char ch;
  ch = getchar();
  while(ch!="e") 
     ch = getchar();
  printf("Found the e.");
  return 0;
}


While loop to print char

#include <stdio.h>
int main() {
  char ch;
  ch = "a";
  while ( ch <= "z" ) 
      printf ( "%c", ch++ );
  
  printf ( "\n" );
}


While to sum integers

#include <stdio.h>
void main()
{
   long sum = 0L; 
   int i = 1;     /* Indexes through the integers       */
   int count = 10; /* The count of integers to be summed */

   /* Sum the integers from 1 to count */
   while(i <= count)
     sum += i++;
   printf("Total of the first %d numbers is %ld\n", count, sum);
}