C Decision
# C Decisions
Decision structures require the programmer to specify one or more conditions to be evaluated or tested, along with a statement(s) to be executed if the condition is determined to be **true**, and optionally, a statement(s) to be executed if the condition is determined to be **false**.
C language assumes any **non-zero** and **non-null** value as **true**, and if it is either **zero** or **null**, then it is assumed as **false**.
Following is the general form of a typical decision structure found in most of the programming languages:

## Decision Statements
C language provides the following types of decision-making statements. Click the links to view details for each statement.
| Statement | Description |
| --- | --- |
| (#) | An **if statement** consists of a boolean expression followed by one or more statements. |
| [if...else statement](#) | An **if statement** can be followed by an optional **else statement**, which executes when the boolean expression is false. |
| (#) | You can use one **if** or **else if** statement inside another **if** or **else if** statement. |
| (#) | A **switch** statement allows testing a variable for equality against a list of values. |
| (#) | You can use one **switch** statement inside another **switch** statement. |
## ? : Operator (Ternary Operator)
We covered the **conditional operator ? :** in previous chapters, which can be used to replace **if...else** statements. Its general form is:
Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Please note the usage and placement of the colon.
The value of a ? expression is determined by Exp1. If Exp1 is true, then Exp2 is evaluated and becomes the value of the entire expression. If Exp1 is false, then Exp3 is evaluated and becomes the value of the entire expression.
!(#)
### Example
The following example determines whether a number is odd or even by entering a number:
## Example
#includeint main(){int num; printf("Enter a number : "); scanf("%d",&num); (num%2==0)?printf("Even number"):printf("Odd number"); }
### 2 Notes β Write a Note
1. #0 kevintcl
299***2513@qq.com [](#)1198 **Here is an example of the ternary operator:**
```c
#include
int A=10;
int B=20;
char buy;
int sum,number;
int main(){
printf("Here are the products and prices of our store:n Product A: Ten yuan each;n Product B: Twenty yuan each;nn");
printf("Please enter the product you need (A or B):");
scanf("%c",&buy);
printf("Please enter the quantity needed:");
scanf("%d",&number);
sum=buy=='A'?A*number:B*number;
printf("nThe total cost for %d %c product(s) is %d yuan.n",number,buy,sum);
return 0;
}
(javascript:;)kevintcl
299***2513@qq.com 9 years ago (2017-10-21)
2. #0 Helen
QQ9***13813@163.com
(https://blog.csdn.net/ccmaa/article/details/51000422) [](#)846 **General form of the switch statement:**
```c
switch(expression){
case constant-expression1: statement1;
case constant-expression2: statement2;
...
default: statement n+1;
}
This means the expression is first evaluated, then compared one by one with the constant expressions after each case. If they are not equal, it continues to compare with the next one. If none match, the statement after **default** is executed. If it matches a certain constant expression, execution starts from the statement after that expression and continues through all subsequent case statements.
The difference from **if** statements: In an **if** statement, when the condition is true, only the statement following that condition is executed, and then it exits the **if** block without executing other **if** statements. However, a **switch** statement does not exit after executing the matching case; instead, it continues executing all subsequent case statements. By adding a **break** statement after each case, you can exit the **switch** statement after each execution, thus avoiding unintended output.
```c
#include
int main(){
int a;
printf("input integer number: ");
scanf("%d",&a);
switch(a){
case 1:printf("Mondayn"); break;
case 2:printf("Tuesdayn"); break;
case 3:printf("Wednesdayn"); break;
case 4:printf("Thursdayn"); break;
case 5:printf("Fridayn"); break;
case 6:printf("Saturdayn"); break;
case 7:printf("Sundayn"); break;
default:printf("errorn");
}
}
(javascript:;)Helen
QQ9***13813@163.com
(https://blog.csdn.net/ccmaa/article/details/51000422) 8 years ago (2018-04-18)
YouTip