当前位置: 首页 > 工具软件 > kLoop > 使用案例 >

c 语言loop语句,C语言循环语句(C language loop statement).doc

长孙永思
2023-12-01

C语言循环语句(C language loop statement)

C语言循环语句(C language loop statement)

Output for 10 groups per line.

#include

Void, main ()

{int, I, J, flag, k=0;

For (i=500; i<=800; i++)

{flag=1;

For (j=2; j

If (flag==1) {printf ("%5d", "I"); k++;

If (k%10==0) printf ("\n");

}

}

Printf ("\nk=%d\n", K);

}

The second part: break and continue statements

The function of the 1.1.break statement: to end the for statement in this layer.

1.2. example: write function, ask s=1+2+3... Until the value of S is greater than the t end loop, the output s is the value, and the value of T is read through the keyboard.

#include

Void, main ()

{int, I, s=0, t;

Printf (enter the value of T:);

Scanf ("%d", &t);

For (i=1;; i++)

{s+=i;

If (s>t) break;

}

Printf ("i=%d\ns=%d\n", "I", "s");

}

T=8

-- >

I=1, s=1

I=2, s=3

I=3, s=6

I=4, s=10

1.3. nested break statements:

--- write function, output all the prime numbers between 500--800, each line for 10 groups for output.

#include

Void, main ()

{int, I, J, flag, k=0;

For (i=500; i<=800; i++)

{flag=1;

For (j=2; j

If (flag==1) {printf ("%5d", "I"); k++;

If (k%10==0) printf ("\n");

}

}

Printf ("\nk=%d\n", K);

}

2.1, continue statement function: skip the following statements that have not been executed in this layer and proceed to the next cycle.

2.2 example:

#include

Void, main ()

{int, I, s=0, k=0;

For (i=1; i<=5; i++)

{s+=i;

If (s>8), {printf ("****i=%d, s=%d, k=%d\n", I, s, K); continue;}

K+=s;

Printf ("i=%d, s=%d, k=%d\n", I, s, K);

}

}

I=1, s=1, k=1

I=2, s=3, k=4

I=3, s=6, k=10

****i=4, s=10, k=10

****i=5, s=15, k=10

The third part: character input and output

Taking the basic data types in C language: integer, real, character

--- char ch;

Ch>='0'&&ch<='9'

Ch>='A'&&ch<='Z'

Ch>='a'&&ch<='z'

3.1 character inputs and outputs:

(1) call printf and scanf

--printf function:

* * * in the C language, the form character%c is used to implement character output.

Such as:

#include

Void, main ()

 类似资料: