Chapter III Branch Structure
The main branch structures are if-else statement nesting, else-if statement, switch statement.
Use if-else statement to calculate water fee for residents
Understanding character data
1) Enter in the scanf() function with%c.
2) ASCII character set: Each character has a unique ordinal value, which is the ASCII code.
3) Differentiation: 1 is integer data,'1'is character.
4) Characters can also be ch=getchar(), putchar() input and output, note that they can only input and output one character.
Logical operation
Logical operators
operator | Name |
---|---|
! | wrong |
&& | and |
|| or
Logical operators & & and || have lower precedence than relational operators, so
(ch>='a')&&(ch<='z')
Equivalent to
ch>='a'&&ch<='z'
Output leap years using logical operations
The role of getchar()
In programs that count characters
scanf("%d",&n);//Total nwie characters getchar();//Read in and discard line breaks
The scanf() function should have its own line breaks that affect character statistics. getchar() has the ability to read in and delete line breaks.
switch Statements
/*Books inquire about prices in vending machines*/ #include <stdio.h> int main() { int choice,i; double price; /*Show menu*/ printf("[1]select crisps\n"); printf("[2]select popcorn\n"); printf("[3]select chocolate\n"); printf("[4]select cola\n"); printf("[0]exit\n"); for(i=1;i<=5;i++){ printf("enter choice:"); scanf("%d",&choice);//Enter the value of the expression if(choice==0) break;//Jump out of for loop with break switch(choice){ case 1:price=3.0;break;//'1'is a constant expression case 2:price=2.5;break; case 3:price=4.0;break; case 4:price=3.5;break; default:price=0.0;break;//The form of a switch statement } printf("price=%0.1f\n",price); }//for Loop End return 0}
Execute the process (dashed lines indicate no break)
1) In a switch statement, expressions and constant expressions generally have integer or character values, and all constant expressions cannot have the same value. Each statement segment can contain one or more statements, or it can be empty.
2) Default can be omitted, but default is omitted. When the value of an expression is not equal to the value of any constant expression, nothing is executed.
3) In a switch statement without break, the case constant expression and default act as statement labels. When the value of the expression matches them, not only the corresponding statement segments are executed, but also all subsequent statements are executed sequentially.
(So the result without break is that the user enters the selection and outputs the message that the user does not need.)
Finish the exercises
/*charge*/ #include <stdio.h> int main(){ int fee,y1,y2,time; //y1 y2 is mileage and pause charges double l;//mileage printf("enter l:"); scanf("%lf",&l); printf("enter time:"); scanf("%\d",&time); if(l<=3){ y1=3; }else if(l<=10){ y1=(l-3)*2+3; }else{ y1=(l-10)*3+24; } int a;//Number of Charges if(a=time/5.0){ y2=2*a; } fee=y1+y2; printf("fee=%d element",fee); return 0; }
//Determine whether it is a triangle #include <stdio.h> #include <math.h> int main(){ double x1,x2,x3,y1,y2,y3;//Coordinates of three points double a,b,c,s;//Trilateral length double area;//The measure of area printf("enter A:");scanf("%lf%lf",&x1,&y1); printf("enter B:");scanf("%lf%lf",&x2,&y2); printf("enter C:");scanf("%lf%lf",&x3,&y3); a=sqrt(pow(x1-x2,2)+pow(y1-y2,2)); b=sqrt(pow(x1-x3,2)+pow(y1-y3,2)); c=sqrt(pow(x2-x3,2)+pow(y2-y3,2)); s=(a+b+c)/2; if((a+b>c)||(a+c>b)||(b+c>a)){ area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("Triangle area%.2f",area); } else{printf("imppossible"); } return 0; }
Nested if-else statements
Matching criteria for else and if: else and the closest if that does not match another else.