08-Dark Horse programmer------C language Learning notes---the cyclic structure of C language

Source: Internet
Author: User
Tags word wrap

Dark Horse programmer------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Loop structure

01-Type Loop while

Format:

while (expression)

Statement

* The value of the expression after the while is evaluated, and if the value is true, the loop body is executed

* Once the loop body is executed, the expression after the while is evaluated again, and its value is true to continue the loop body, whose value is false, exits the loop.

* First expression is false, then one loop does not execute

* The loop body has multiple statements, enclosed in {}.

* The loop body should have the end of the statement, to avoid the cycle of death

* Loop initial value, accumulation is usually set to 0, multiplicative is set to 1

//enter several characters, if uppercase, convert to lowercase, if not, enter 0 program end#include<stdio.h>intMain () {Charch;  while((Ch=getchar ())! =0) {ch= ((ch>='A'&&ch<='Z')? (ch+ +): CH); printf ("%c", CH); }          }

Do-while

Format:

Do

{

Statement

}while (expression)

* Executes the loop body statement after do, evaluates the expression, resumes execution of the loop body if the expression is true, and exits the loop if the expression is false.

*while must be added later;

//calculation: 1+1/2+1/4+....+1/50#include<stdio.h>intMain () {intI=2; floatsum=1;  while(i<= -) {sum= sum+1.0/i; I+=2; } printf ("sum=%f\n", sum); return 0;}

The For Loop

for (expression 1; expression 2; expression 3)

Circulation body;

* First evaluates expression 1, evaluates expression 2, if its value is true, executes the loop body, executes expression 3, and then resumes the loop. If the value is false, exit the loop.

Four break statements and continue statements

The break statement

Format: break;

* Terminates execution of a switch statement or Loop statement, and then moves to the following statement.

Continue statement

Format: Continue;

The function of *continue is to end this cycle, which is to skip the remaining statements in this loop, and then make the next loop decision.

* Unlike the break statement, executing the CONTINUE statement does not terminate the entire loop.

//the factorial n! of a positive integer n , where n is entered by the user#include<stdio.h>intMain () {floatfact; intN,i; printf ("Please enter an integer: \ n"); scanf ("%d",&N);  for(i=1, fact=1.0; i<=n; i++) {fact= fact*i; } printf ("fact=%f\n", fact); return 0;}

Five-pointer programming

01 The meaning of the pointer variable: A pointer variable is a type of variable that accesses the address of another variable or function.

02 Definition of pointer variable:

Base type * pointer variable name "= initial value"

03 Assignment of the pointer variable:

* With the address operator & assignment, you can assign the address of a variable to a pointer variable.

such as: float f,*p;

p=&f;

* Define the simultaneous assignment of pointer variables

such as: float f,*p=&f;

* Assign values with other pointers

such as: int i,*p1=&i,*p2;

P2=P1;

Note: The data types referred to by the two pointer types must be equal

//output 99 multiplication formula#include <stdio.h>intMain () {inti,j;  for(i=1; i<=9; i++)    {         for(j=1; j<=i; J + +) {printf ("%d*%d=%-3d", j,i,i*j); } printf ("\ n"); }                    return 0;}

04 pointer operator

The pointer operator * is the monocular operator that can be used to access the data in the storage unit pointed to.

such as: int x,*p;

p=&x;

Note that pointer p points to x,x is the object that P points to, and you can use *p to reference X.

//enter characters continuously from the keyboard and count the number of uppercase letters in them until the end of the Enter word wrap character#include<stdio.h>intMain () {Charch; intsum =0;  while(1) {ch=GetChar (); if(ch=='\ n')        {             Break; }        if(ch>='A'&&ch<='Z') {sum++; }} printf ("%d\n", sum); return 0;}
//Enter 30 characters from the keyboard and count the number of characters#include<stdio.h>intMain () {Charch; inti,sum=0;  for(i=1; i<= -; i++) {ch=GetChar (); if(ch<'0'|| Ch>'9')        {            Continue; } Sum++; } printf ("%d\n", sum); return 0;}
//Enter a positive integer from the keyboard to find the highest digit#include<stdio.h>intMain () {inti,*P; P=&i; printf ("Please enter a positive integer: \ n"); scanf ("%d",&i);  while(*p>=Ten)    {        *p= *p/Ten; } printf ("maximum number of digits is:%d\n",*q); return 0;}

08-Dark Horse programmer------C language Learning notes---the cyclic structure of C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.