# The fourth chapter is the summary of branch structure programming

Keywords: C

4.1 cited examples

[example 4.1] input the three sides of the triangle to judge whether it can form a triangle. If so, calculate and output the area of the triangle, otherwise an error prompt will be given.

```#include<stdio.h>
#include<math.h>
int main()
{
int a, b, c;
float area;
double s;
printf("Please enter the three sides of the triangle:");
scanf_s("%d,%d,%d",&a,&b,&c);
s = (a + b + c) / 2.0;
if (a + b <= c || b + c <= a || a + c <= b)
printf("Does not form a triangle\n");
else
{
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("The area of the triangle is:%f\n", area);
}
return 0;
}``` [example 4.2]

```#include<stdio.h>
int main()
{
int x; float y;
printf("input x Value of:");
scanf_s("%d", &x);
if (x != 0)
y = 1.0 / x;
else
y = 0;
printf("%d,%f",x,y);
return 0;
}```  4.2 relational operators, logical operators and conditional operators

4.2.1 relational operators and expressions

1. Relational operator: the relational operator is used to judge the size relationship between two operands. be careful:

(1) Relational operators are binocular operators, and their combination direction is left combination.

(2) Relational operators take precedence over arithmetic operators but over assignment operators.

(3) In relation operators, >, <, > =, < = have the same priority, = == The priority of "> =" is higher than "= =".

2. Relational expression: use relational operators to return the size of two expressions to a logical value.

Function: compare the size of two expressions and return a logical value.

be careful:

(1) Note the distinguishing operators' = 'and' = = '. "=" is an assignment operator and "= =" is a relational operator.

(2) Judging the equality of real numbers may not get the correct result.

(3) Assignment operators can appear in relational expressions. "1" means "true" and "0" means "false".

[example 4.3] example of relational operation.

```#include<stdio.h>
int main()
{
char c = 'm';
int i = 10, j = 20, k = 30;
float x = 13e+5, y = 10.85;
printf("%d,%d,", 'a' + 5 < c, -i - 2 * j >= k + 1);
printf("% d, % d,", 1 < j < 5, x - 5.25 <= x + y);
printf("%d,%d\n", i + j + k == -2 * j, k == j == i + 5);
return 0;
}``` 4.2.2 logical operators and logical expressions

1. Logical operator: relational expression can only describe a single condition.

Note: and operator & & and or operator 𞓜 are binocular operators, not operators! Is a monocular operator.

2. Logical expression [example 4.5] example of logical operation.

```#include<stdio.h>
int main()
{
int a = 3, b = 3, c = 2, d;
printf("%d,",!a && !b && c);
printf("%d,", !a || !b || c);
printf("% d\n", a >= b && b > c);
d = c-- || (b = 6) && a++;
printf("a=%d,b=%d,c=%d,d=%d\n", a, b, c, d);
return 0;
}``` 4.2.3 conditional operators and conditional expressions

1. Conditional operator: the conditional operator is composed of two symbols "?" and ":". It is used for conditional evaluation. It is a ternary operator and requires three operands. The conditional operator has lower priority than the logical operator and higher priority than the assignment operator. It is right combined.

2. Conditional expression: the formula connecting three expressions by conditional operators is called conditional expression.

[example 4.6] find the larger of two numbers with conditional expression.

```#include<stdio.h>
int main()
{
int x, y;
scanf_s("%d,%d", &x, &y);
printf("The maximum number of two integers is:%d\n", x > y ? x : y);
return 0;
}``` 4.3 implementing branch structure using if else

1. If else statement

[example 4.7] use the double branch if statement to find the larger of the two numbers.

```#include<stdio.h>
int main()
{
int a, b;
scanf_s("%d%d", &a, &b);
if (a > b)
printf("The maximum number of two integers is:%d\n", a);
else
printf("The maximum number of two integers is:%d\n", b);
return 0;
}```  [example 4.8]

```#include<stdio.h>
#include<math.h>
int main()
{
double x, y;
printf("input x Value of:");
scanf_s("%lf",&x);
if (x <= 1)
y = exp(x);
else
y = pow(x, 2) - 1;
printf("f(%f)=%.2f\n", x, y);
return 0;
}```  A double branch if statement can be replaced by a conditional expression.

2. Single branch statement

[example 4.9] write a program to input a real number arbitrarily and output its absolute value.

```#include<stdio.h>
int main()
{
float x;
scanf_s("%f", &x);
if (x < 0)
x = -x;
printf("%f", x);
return 0;
}``` [example 4.10]

```#include<stdio.h>
int main()
{
int x, y;
printf("x=");
scanf_s("%d", &x);
if (x)
y = 1;
else
y = -1;
printf("y=%d", y);
return 0;
}```  [example 4.11] input three real numbers and output them in descending order.

```#include<stdio.h>
int main()
{
float a, b, c, t;
scanf_s("%f,%f,%f",&a,&b,&c);
if(a>b)
{t = a; a = b; b = t;}
if(a>c)
{t = a; a = c; c = t;}
if(b>c)
{t = b; b = c; c = t;}
printf("%5.2f,%5.2f,%5.2f\n", a, b, c);
return 0;
}``` 4.3.2 nesting of if statements

[example 4.12] enter three arbitrary integers to find the maximum number.

```#include<stdio.h>
int main()
{
int a, b, c, max;
scanf_s("%d%d%d",&a,&b,&c);
if (a > b)
if (a > c)
max = a;
else
max = c;
else
if (b > c)
max = b;
else
max = c;
printf("max=%d\n", max);
return 0;
}```   [example 4.13]

```#include<stdio.h>
int main()
{
int x, y;
scanf_s("%d", &x);
if (x > 0)
y = 1;
else
if (x == 0)
y = 0;
else
y = -1;
printf("y=%d\n", y);
return 0;
}``` 4.3.3 multi branch if statement

[example 4.14]

```#include<stdio.h>
#include<math.h>
int main()
{
float x, y;
printf("input x Value of:");
scanf_s("%f", &x);
if (x < 2)
y = 2 * x;
else if (x <= 10)
y = 7 - 3 * x;
else
y = 1 - cos(x);
printf("y=%0.6f\n", y);
return 0;
}``` [example 4.15]

```#include<stdio.h>
int main()
{
char c1, c2;
c1 = getchar();
if (c1 >= 'a' && c1 <= 'z')
c2 = c1 - 32;
else if (c1 >= 'A' && c1 < 'Z')
c2 = c1 + 32;
else
c2 = c1 + 1;
putchar(c2);
return 0;
}```  4.4 using switch statement to realize branch structure

Notes on switch statements:

(1) The expression after switch can be any expression, and its value can only be one of integer type, character type and enumeration type.

(2) The values of constant expressions behind each case are different from each other. Otherwise, there will be contradictions.

(3) The order of occurrence of each case and default does not affect the execution result.

(4) Multiple case s can share a set of execution statements.

(4) switch statements allow nested use.

```#include<stdio.h>
int main()
{
int month;
printf("Enter month:");
scanf_s("%d", &month);
switch ((month - 1) / 3)
{
case 0:
printf("%d Month is the first quarter\n", month);
break;
case 1:
printf("%d Month is the second quarter\n", month);
break;
case 2:
printf("%d Month is the third quarter\n", month);
break;
case 3:
printf("%d April is the fourth quarter\n", month);
break;
default:
printf("Input error\n");
break;
}
return 0;
}``` 2. Use the break statement after some statements in the switch statement format

[example 4.7] the function of this program is to output the days of the year and month according to the year and month entered on the keyboard.

```#include<stdio.h>
int main()
{
int year, month, days;
scanf_s("%d,%d",&year,&month);
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if (year % 400 == 0 || year % 4 == 0 && year%100 != 0)
days = 29;
else
days = 28;
break;
}
printf("%d year%d The number of days in the month is%d day\n", year, month, days);
return 0;
}``` [example 4.18] use the switch statement to write the program of example 4.14.

```#include<stdio.h>
#include<stdio.h>
int main()
{
int expression; float x, y;
printf("input x Value of:");
scanf_s("%f", &x);
expression = (int)(1 * (x < 2) + 2 * (x >= 2 && x <= 10) + 3 * (x > 10));
switch (expression)
{
case 1:
y = 2 * x; break;
case 2:
y = 7 - 3 * x; break;
case 3:
y = 1 - cos(x); break;
}
printf("y=%0.5f\n", y);
return 0;
}``` 3. Do not use the break statement in the switch statement format: when the break statement is not used in the switch statement, the program will not jump out of the executing switch statement after executing the corresponding statement, but will continue to execute all subsequent statements.

4.5 example of branch structure programming

[example 4.19]

```#include<stdio.h>
#include<math.h>
int main()
{
float x, y;
scanf_s("%f", &x);
if (x > +0)
y = sqrt(x);
else
y = pow(x, 5) + 2 * x + 1 / x;
printf("x=%.2f,y=%.2f\n", x, y);
return 0;
}``` [example 4.20] write a program to judge whether the entered year is a leap year. It is required to use the standard format, nested format and multi branch format of if statement respectively.

1. Standard format implementation

```#include<stdio.h>
int main()
{
int year, leap;
printf("Enter year:");
scanf_s("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
leap = 1;
else
leap = 0;
if (leap)
printf("%d It's a leap year\n", year);
else
printf("%d Not a leap year\n", year);
return 0;
}``` 2. Nested format implementation

```#include<stdio.h>
int main()
{
int year, leap;
printf("Enter year:");
scanf_s("%d", &year);
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
leap = 1;
else
leap = 0;
}
else
leap = 1;
}
else
leap = 0;
if (leap)
printf("%d It's a leap year\n", year);
else
printf("%d Not a leap year\n", year);
return 0;
}``` 3. Implementation of multi branch format

```#include<stdio.h>
int main()
{
int year, leap;
printf("Enter year:");
scanf_s("%d", &year);
if(year % 4 != 0)
leap = 0;
else if (year % 100 != 0)
leap = 1;
else if (year % 400 != 0)
leap = 0;
else
leap = 1;
if (leap)
printf("%d It's a leap year\n", year);
else
printf("%d Not a leap year\n", year);
return 0;
}``` [example 4.21] input a 3-digit integer from the keyboard to judge whether the number is in ascending order. If the input is not 3 digits, output "Enter error". Ascending refers to the number in which the high-order number is successively smaller than its low-order number. For example, 359 is the ascending order number.

```#include<stdio.h>
int main()
{
int n, a, b, c;
scanf_s("%d", &n);
if (n < 100 || n>999)
printf("Input error!\n");
else
{
a = n / 100;
b = n / 10 % 10;
c = n % 10;
if (a < b && b < c)
printf("%d Is the ascending ordinal number\n", n);
else
printf("%d Not an ascending number\n", n);
}
return 0;
}``` Posted by dar-k on Sat, 20 Nov 2021 17:08:18 -0800