C Language Learning Tutorial Chapter III-C language programming preliminary (6)

Source: Internet
Author: User
Tags arithmetic case statement constant continue execution integer min printf

Switch statement

The C language also provides another switch statement for multiple branch selections, in the general form of:
switch (expression) {
case constant Expression 1: statement 1;
Case constant Expression 2: statement 2;
...
case constant Expression N: statement n;
Default: Statement n+1;
}
Its semantics are: Evaluate the value of an expression. And one after another, compared to the constant expression value, when the value of the expression is equal to the value of a constant expression, the following statement is executed, and then no longer judged, and the statement after all subsequent case is executed. If the value of an expression is different from the constant expression after all the case, the statement after the default is executed.
void Main () {
int A;
printf ("Input integer Number:");
scanf ("%d", &a);
Switch (a) {
Case 1:printf ("monday\n");
Case 2:printf ("tuesday\n");
Case 3:printf ("wednesday\n");
Case 4:printf ("thursday\n");
Case 5:printf ("friday\n");
Case 6:printf ("saturday\n");
Case 7:printf ("sunday\n");
default:printf ("error\n");
}
}

This procedure is required to enter a number, output an English word. However, when 3 is entered, the CASE3 and all subsequent statements are executed, outputting the Wednesday and all subsequent words. This is of course not desirable. Why is this happening? This just reflects a feature of the switch statement. In a switch statement, a case constant expression is equivalent to a statement label, and the value of an expression is shifted to the label execution, but it does not automatically jump out of the entire switch statement after the statement is executed, so there is a situation where you continue to execute all of the subsequent cases statements. This is completely different from the IF statement described earlier and should be given special attention. To avoid this, the C language also provides a break statement that is designed to jump out of a switch statement with only a keyword break and no parameters. It will be described in more detail later. Modify the example program, after each case statement to increase the break statement, so that after each execution can jump out of the switch statements, so as to avoid output undesirable results.
void Main () {
int A;
printf ("Input integer Number:");
scanf ("%d", &a);
Switch (a) {
Case 1:printf ("monday\n");
Case 2:printf ("tuesday\n"); Break
Case 3:printf ("wednesday\n");
Case 4:printf ("thursday\n");
Case 5:printf ("friday\n");
Case 6:printf ("saturday\n");
Case 7:printf ("sunday\n");
default:printf ("error\n");
}
}
The following points should also be noted when using the switch statement:
1. The values of the constant expressions after the case cannot be the same, otherwise an error occurs.
2. After the case, allow multiple statements, you can not use {} enclosed.
3. The sequence of case and default clauses can be varied without affecting the outcome of the execution of the program.
4.default clauses can be omitted without. Program examples
Enter three integers, outputting the maximum number and the decimal.
void Main () {
int a,b,c,max,min;
printf ("Input three numbers:");
scanf ("%d%d%d", &a,&b,&c);
if (a>b)
{max=a;min=b;}
Else
{max=b;min=a;}
if (max<c)
Max=c;
Else
if (min>c)
Min=c;
printf ("max=%d\nmin=%d", max,min);
}

In this program, first compare the size of the input a,b, and put the large number into Max, decimal into Min, and then compared with C, if Max is less than C, then the C to max; If C is less than min, then C gives Min. So Max is always the largest number, and Min is always the decimal. Finally, the max and Min values are output. Calculator program. The user inputs the operands and the arithmetic character, outputs the computation result.
void Main () {
float a,b,s;
char c;
printf ("Input expression:a+ (-,*,/) b \ n");
scanf ("%f%c%f", &a,&c,&b);
Switch (c) {
Case ' + ': printf ("%f\n", a+b);
Case '-': printf ("%f\n", a-b);
Case ' * ': printf ("%f\n", a*b);
Case '/': printf ("%f\n", A/b);
default:printf ("Input error\n");
}
}

This example can be used for arithmetic evaluation. The switch statement is used to determine the operator and then output the operation value. An error message is given when the input operator is not a +,-, *,/.

Loop structure Program

The circular structure is a very important structure in the program. It is characterized by repeated execution of a section of a program when a given condition is established, until the condition is not tenable. Given conditions called cyclic conditions, repeated execution of the program segments is called the loop body. C language provides a variety of loop statements, which can form a variety of different forms of the loop structure.

While statement

The general form of a while statement is a while (expression) statement in which the expression is a loop condition and the statement is a loop body.
The semantics of the while statement is to evaluate the value of the expression and execute the Loop body statement when the duty is true (not 0). The execution process can be expressed in figure 3-4. Counts the number of line characters entered from the keyboard.
#include <stdio.h>
void Main () {
int n=0;
printf ("Input a string:\n");
while (GetChar ()!= ' \ n ') n++;
printf ("%d", n);
}
The loop condition in this example program is GetChar ()!= ' \ n ', which means that as long as the character entered from the keyboard is not a carriage return, the loop continues. The loop body n++ completes the count of the number of input characters. The program implements a count of the number of characters entered for a single line of characters.
The following points should be noted when using the while statement:
An expression in a 1.while statement is typically a relational expression or a logical expression, and can continue looping as long as the value of the expression is true (not 0).
void Main () {
int a=0,n;
printf ("\ n input N:");
scanf ("%d", &n);
while (n--)
printf ("%d", a++*2);
}
This example program will perform n cycles, each time, n value minus 1. The loop body outputs the value of an expression a++*2. The expression is equivalent to (a*2;a++)
2. If the circulation body includes more than one statement, it must be enclosed in {} to form a compound statement.
3. Attention should be paid to the choice of cyclic conditions to avoid dead loops.
void Main () {
int a,n=0;
while (a=5)
printf ("%d", n++);
}
In this case, the loop condition of the while statement is the assignment expression a=5, so the value of the expression is always true, and the loop body has no other means of stopping the loop, so the loop will go on endlessly, creating a dead loop. 4. The loop body of the while statement is allowed to be a while statement, thus creating a double loop.

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.