C Tutorial/Statement/For statement

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Continue a for loop

#include <stdio.h>
 
int main(){
    int x=0;
 
    for(;;) {
        x++;
        if(x<=5) {
            printf("%d, ",x);
            continue;
        }
        printf("%d is greater than 5!\n",x);
        break;
    }
    return(0);
}
1, 2, 3, 4, 5, 6 is greater than 5!

for LOOP

The for loop is usually used when the number of iterations is predetermined.

The format of the for loop is


for (expr1;expr2;expr3){
        s1;
        s2 ;
    }

for loop backward

#include <stdio.h>
int main(void)
{
  long sum = 0L;
  int count = 10;
  int i;
  for (i = count ; i >= 1 ; sum += i-- );
  printf("\nTotal of the first %d numbers is %ld\n", count, sum);
  return 0;
}
Total of the first 10 numbers is 55

indefinite loop: empty for

#include <stdio.h>
#include <ctype.h>           /* For tolower() function */
int main(void)
{
  char answer = "N";
  printf("\nThis program calculates the average of any number of values.");
  for( ;; )                           /* Indefinite loop */
  {
    printf("Do you want to enter another value? (Y or N): ");
    scanf(" %c", &answer );
    if( tolower(answer) == "n" ){
      break;
    }
  }
  return 0;
}
This program calculates the average of any number of values.Do you want to enter another value? (Y o
     r N): N

Initialize loop control variable outside the for statement

#include <stdio.h>
int main(void)
{
  int count = 1;
  for( ; count <= 10 ; ++count)
    printf("\n%d", count);
  printf("\nWe have finished.\n");
  return 0;
}
1
     2
     3
     4
     5
     6
     7
     8
     9
     10
     We have finished.

Nest for loop: two different control variables

#include <stdio.h>
int main(void)
{
  long sum = 0L;
  int count = 10;
  int i,j;
  for(i = 1 ; i <= count ; i++ ){
    sum = 0L;
    for(j = 1 ; j <= i ; j++ )
      sum += j;
    printf("\n%d\t%ld", i, sum);
  }
  return 0;
}
1       1
     2       3
     3       6
     4       10
     5       15
     6       21
     7       28
     8       36
     9       45
     10      55

Nest if statement in for loop

#include <stdio.h>
int main(void)
{
  int chosen = 15;            
  int guess = 0;              
  int count = 3;              
  printf("\nA Number.\n");
  for( ; count>0 ; --count)
  {
    printf("\nYou have %d tr%s left.", count, count == 1 ? "y" : "ies");
    printf("\nEnter a guess: ");
    scanf("%d", &guess);
    if (guess == chosen)
    {
      printf("\nYou guessed it!\n");
      return 0;               
    }
    if(guess<1 || guess > 20)
      printf("between 1 and 20.\n ");
    else
      printf("Sorry. %d is wrong.\n", guess);
  }
  printf("\nYou have had three tries and failed. The number was %d\n", chosen);
  return 0;
}
A Number.
     
     You have 3 tries left.
     Enter a guess: 1
     Sorry. 1 is wrong.
     
     You have 2 tries left.
     Enter a guess: 2
     Sorry. 2 is wrong.
     
     You have 1 try left.
     Enter a guess: 3
     Sorry. 3 is wrong.
     
     You have had three tries and failed. The number was 15

Omit all three parts in for loop

#include <stdio.h>
int main()
{
    printf("~ to exit");
    for(;;)
    {
        char ch=getchar();
        if(ch=="~")
        {
            break;
        }
    }
}
~ to exit123
      ~

Sum integers in for statement

#include <stdio.h>
int main(void)
{
  long sum = 0L;
  int count = 0;
  
  printf("\nEnter the number of integers you want to sum: ");
  scanf(" %d", &count);
  
  for (int i = 1 ; i<= count ; sum += i++ );
  printf("\nTotal of the first %d numbers is %ld\n", count, sum);
  return 0;
}
Enter the number of integers you want to sum: 112
     
     Total of the first 112 numbers is 6328

The for loop with a comma operator

#include <stdio.h>
main(){
    int i, j;
   
    for (i = 0, j = 10; i < 3 && j > 8; i++, j--){
       printf (" the value of i and j %d %d\n",i, j);
    }
}
the value of i and j 0 10
      the value of i and j 1 9

Use char variable to control for loop

#include <stdio.h>
int main()
{
    char b,a="a" ;
    for(b="A";b<"K";b++) {
         printf("%d - %c ",a,b);
    }
    putchar("\n");  /* end of line */
    return(0);
}
97 - A 97 - B 97 - C 97 - D 97 - E 97 - F 97 - G 97 - H 97 - I 97 - J

Use for loop to print a rectangle

#include <stdio.h>
int main(void)
{
  printf("\n**************");         
  for(int count = 1 ; count <= 8 ; ++count)
    printf("\n*            *");       
  printf("\n**************\n");       
  return 0;
}
**************
     *            *
     *            *
     *            *
     *            *
     *            *
     *            *
     *            *
     *            *
     **************