C Language Growth Series 2

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

              ||                          or   

Logical operators & & and || have lower precedence than relational operators, so
Equivalent to
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("enter choice:");
		scanf("%d",&choice);//Enter the value of the expression
		break;//Jump out of for loop with break
		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
		}//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

#include <stdio.h>
int main(){
	int fee,y1,y2,time;
	//y1 y2 is mileage and pause charges 
	double l;//mileage 
	printf("enter l:");
	printf("enter time:");
	}else if(l<=10){
	int a;//Number of Charges
	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);
	 	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.

Tags: C

Posted on Sat, 02 Oct 2021 12:43:11 -0400 by Pavlos1316