C/Language Basics/For
Содержание
- 1 A simple for loop
- 2 Demonstrate multiple loop control variables
- 3 For loop: bigger increase
- 4 For loop condition: loop will not execute
- 5 For loop: Draw a box
- 6 For loop: List ten integers
- 7 For loop with init value, stop condition and incr
- 8 For loop without first part
- 9 For loop without the third part
- 10 For : no first part
- 11 For: no init and loop backwards
- 12 For statement: backwards
- 13 Infinite for loop with break
- 14 More calculation in the for statement
- 15 More complex for loop: more statements
- 16 Nested for
- 17 nested for Demo
- 18 Nested for loop demo
- 19 Prime number tester
- 20 Reverse order of for loop
- 21 See the for loop terminating condition
- 22 Set the init value for a for loop
- 23 Simplest for loop
- 24 Simplest for loop: user control the loop
- 25 Three level nested for loop
- 26 Two condition for ending a for loop
- 27 Use char as int in a for loop
- 28 Use for loop as the user selection"s controller
- 29 Use function as for loop control variable
- 30 Use int as the while loop controller
- 31 Use integer as the for loop controller
A simple for loop
#include <stdio.h>
int main(void)
{
int i;
/* count by 2s */
for( i = 0; i < 100; i += 2)
printf("%d ", i);
return 0;
}
Demonstrate multiple loop control variables
#include <stdio.h>
#include <string.h>
void converge(char *targ, char *src);
int main(void)
{
char target[80] = "This is a test";
converge(target, "This is a test of converge().");
printf("Final string: %s\n", target);
return 0;
}
/* copies one string into another.
It copies characters to both the ends,
converging at the middle. */
void converge(char *targ, char *src)
{
int i, j;
printf("%s\n", targ);
for(i = 0, j = strlen(src); i <= j; i++, j--) {
targ[i] = src[i];
targ[j] = src[j];
printf("%s\n", targ);
}
}
For loop: bigger increase
#include <stdio.h>
int main(void)
{
int i;
for(i = 0; i < 102; i = i + 5)
printf("%d ", i);
return 0;
}
For loop condition: loop will not execute
#include <stdio.h>
int main(void)
{
int i;
/* this loop will not execute */
for(i = 11; i < 11; i = i + 1)
printf("%d ", i);
printf("terminating");
return 0;
}
For loop: Draw a box
#include <stdio.h>
void main() {
int count = 0;
printf("\n**************"); /* box top */
for(count = 1 ; count <= 8 ; ++count)
printf("\n* *"); /* box sides */
printf("\n**************\n"); /* bottom of the box */
}
For loop: List ten integers
#include <stdio.h>
void main()
{
int count = 1;
for( ; count <= 10 ; ++count)
printf("\n %d", count);
printf("\n Finished.\n");
}
For loop with init value, stop condition and incr
#include <stdio.h>
int main(void)
{
int i, j, k;
for( k = 0; k < 10; k = k + 1) {
printf("Enter a number: ");
scanf("%d", &i);
printf("Enter a non-zero number: ");
scanf("%d", &j);
if(j != 0)
printf("%d\n", i / j);
if(j == 0)
printf("Cannot divide by zero\n");
}
return 0;
}
For loop without first part
#include <stdio.h>
int main(void)
{
int i;
printf("Enter an integer: ");
scanf("%d", &i);
for(; i; i--)
printf("%d ", i);
return 0;
}
For loop without the third part
#include <stdio.h>
int main(void)
{
int i;
for(i=0; i<10; ) {
printf("%d ", i);
i++;
}
return 0;
}
For : no first part
#include <stdio.h>
void main()
{
long luckyNumber = 15;
int yourInput = 0;
int count = 3; /* The maximum number of tries */
for( ; count>0 ; --count) {
printf("\nYou have %d tr%s left.", count, count == 1 ? "y" : "ies");
printf("\nEnter: "); /* Prompt for a guess */
scanf("%d", &yourInput); /* Read in a guess */
/* Check for a correct guess */
if (yourInput == luckyNumber)
{
printf("\nYou guessed it!\n");
return; /* End the program */
}
/* Check for an invalid guess */
if(yourInput<1 || yourInput > 20)
printf("I said between 1 and 20.\n ");
else
printf("Sorry. %d is wrong.\n", yourInput);
}
printf("\nYou have had three tries and failed. The number was %ld\n",
luckyNumber);
}
For: no init and loop backwards
#include <stdio.h>
#include <stdlib.h> /* For rand() and srand() */
#include <time.h> /* For time() function */
void main()
{
int chosen = 0; /* The lucky number */
int guess = 0; /* Stores a guess */
int count = 3; /* The maximum number of tries */
int limit = 20; /* Upper limit for pseudo-random values */
srand(time(NULL)); /* Use clock value as starting seed */
chosen = 1 + rand()%limit; /* Random int 1 to limit */
printf("\nI have chosen a number between 1 and 20"
" which you must guess.\n");
for( ; count>0 ; --count)
{
printf("\nYou have %d tr%s left.", count, count == 1 ? "y" : "ies");
printf("\nEnter a guess: "); /* Prompt for a guess */
scanf("%d", &guess); /* Read in a guess */
/* Check for a correct guess */
if (guess == chosen)
{
printf("\nYou guessed it!\n");
return; /* End the program */
}
/* Check for an invalid guess */
if(guess<1 || guess > 20)
printf("I said between 1 and 20.\n ");
else
printf("Sorry. %d is wrong.\n", guess);
}
printf("\nYou have had three tries and failed. The number was %ld\n",
chosen);
}
For statement: backwards
#include <stdio.h>
void main() {
long sum = 0L;
int count = 10; /* The number of integers to be summed */
int i = 0; /* The loop counter */
/* Sum integers from count to 1 */
for (i = count ; i >= 1 ; sum += i-- );
printf("\nTotal of the first %d numbers is %ld\n", count, sum);
}
Infinite for loop with break
#include <stdio.h>
int main(void) {
char ch;
for( ; ; ) { /* infinite for loop */
printf("Load, Save, Edit, Quit?\n");
do {
printf ("Enter your selection: ");
ch = getchar();
} while(ch!="L" && ch!="S" && ch!="E" && ch!="Q");
if(ch == "Q")
break;
}
return 0;
}
More calculation in the for statement
#include <stdio.h>
int main(void)
{
int num, i;
printf("Enter the number to test: ");
scanf("%d", &num);
for( i = 2; i < (num / 2) + 1; i = i + 1)
if( ( num % i ) == 0)
printf("%d ", i);
return 0;
}
More complex for loop: more statements
#include <stdio.h>
int main(void)
{
int i, j;
/* count to 49 */
for( i = 0, j = 100; i < j; i++, j--)
printf("%d ", i);
return 0;
}
Nested for
#include <stdio.h>
void main()
{
long sum = 0L;
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 = 0L; /* Initialize sum for the inner loop */
/* Calculate sum of integers from 1 to i */
for(j = 1 ; j <= i ; j++ )
sum += j;
printf("\n%d\t%ld", i, sum); /* Output sum of 1 to i */
}
}
nested for Demo
#include <stdio.h>
int main() {
int row,column;
for(row = 1; tow <= 10; row++) {
for(column = 1; column <= 10; column++)
printf("%6d", row * column);
putchar("\n");
}
}
Nested for loop demo
#include <stdio.h>
int main(void)
{
int answer, i, chances, right;
for(i = 1; i < 11; i++) {
printf(" %d + %d = ?", i , i);
scanf("%d", &answer);
if(answer == i + i)
printf("Right!\n");
else {
printf("Wrong.\n");
printf("Try again.\n");
right = 0;
/* nested for */
for(chances = 0; chances < 3 && !right; chances++) {
printf(" %d + %d = ? ", i, i);
scanf("%d", &answer);
if(answer == i + i) {
printf("Right!\n");
right = 1;
}
}
/* if answer still wrong, tell user */
if(!right)
printf("The answer is %d.\n", i + i);
}
}
return 0;
}
Prime number tester
#include <stdio.h>
int main(void)
{
int num, i, is_prime;
printf("Enter a number: ");
scanf("%d", &num);
is_prime = 1;
for(i = 2; i <= num / 2; i = i + 1)
if((num%i)==0)
is_prime = 0;
if(is_prime==1)
printf(" is prime.");
else
printf("not prime.");
return 0;
}
Reverse order of for loop
#include <stdio.h>
int main(void)
{
int i, j;
printf("Enter a number: ");
scanf("%d", &i);
for(j = i; j > 0; j--)
printf("%d\n", j);
printf("\a");
return 0;
}
See the for loop terminating condition
#include <stdio.h>
int main(void) {
int i;
for(i = 1; i < 11; i = i+1)
printf("%d ", i);
printf("terminating");
return 0;
}
Set the init value for a for loop
#include <stdio.h>
int main(void)
{
int i;
for ( i = 17; i < 101; i = i + 1)
if( ( i % 17) == 0)
printf("%d ", i);
return 0;
}
Simplest for loop
#include <stdio.h>
int main(void)
{
int i;
for( i = 1; i < 101; i++)
printf("%d ", i);
return 0;
}
Simplest for loop: user control the loop
#include <stdio.h>
main() {
int i,n;
scanf("%d",&n);
for(i = 0; i < n; i = i+1) {
printf("the numbers are %d \n",i);
}
}
Three level nested for loop
#include <stdio.h>
int main(void)
{
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 26; j++)
for( k = 0; k < 2; k++)
printf("%c", "A" + j);
return 0;
}
Two condition for ending a for loop
#include <stdio.h>
int main(void)
{
int magic; /* magic number */
int guess; /* user"s guess */
int i;
magic = 1325;
guess = 0;
for(i = 0; i < 10 && guess != magic; i++) {
printf("Enter your guess: ");
scanf("%d", &guess);
if(guess == magic) {
printf("RIGHT!");
printf(" %d is the magic number.\n", magic);
}
else {
printf("wrong...");
if(guess > magic)
printf(" too high.\n");
else
printf(" too low.\n");
}
}
return 0;
}
Use char as int in a for loop
#include <stdio.h>
int main(void)
{
int i;
char j;
i = 0;
for(j = 1; j < 101; j++)
i = j + i;
printf("Total is: %d", i);
return 0;
}
Use for loop as the user selection"s controller
#include <stdio.h>
#include <conio.h>
int main(void)
{
int i;
char ch;
ch = "a"; /* initial value */
for(i=0; ch != "q"; i++) {
printf("pass: %d\n", i);
ch = getchar();
}
return 0;
}
Use function as for loop control variable
#include <stdio.h>
int sqrnum(int num);
int readnum(void);
int prompt(void);
int main(void)
{
int t;
for(prompt(); t=readnum(); prompt())
sqrnum(t);
return 0;
}
int prompt(void)
{
printf("Enter a number: ");
return 0;
}
int readnum(void)
{
int t;
scanf("%d", &t);
return t;
}
int sqrnum(int num)
{
printf("%d\n", num*num);
return num*num;
}
Use int as the while loop controller
#include <stdio.h>
main() {
int i,n;
scanf("%d",&n);
i = 0;
do {
printf("the numbers are %d \n",i);
i = i +1;
}while( i<n);
}
Use integer as the for loop controller
#include <stdio.h>
int main(void)
{
int i;
for(i = 1; i < 101; i = i + 1)
printf("%d ", i);
return 0;
}