Add the title "More information " to the article (#22288)
This commit is contained in:
@ -16,6 +16,7 @@ if(boolean_expression)
|
|||||||
//Block of Statements executed when boolean_expression is true
|
//Block of Statements executed when boolean_expression is true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
```C
|
```C
|
||||||
int a = 100;
|
int a = 100;
|
||||||
@ -24,13 +25,13 @@ if(a < 200)
|
|||||||
printf("a is less than 200\n" );
|
printf("a is less than 200\n" );
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Result
|
#### Result
|
||||||
|
|
||||||
`a is less than 200`
|
`a is less than 200`
|
||||||
|
|
||||||
|
|
||||||
## 2. if...else statement
|
## 2. if...else statement
|
||||||
If the Boolean expression evaluates to **true**, then the if block will be executed, otherwise, the else block will be executed.
|
If the Boolean expression evaluates to **true**, then the if block will be executed, otherwise, the else block will be executed.
|
||||||
|
|
||||||
#### Syntax
|
#### Syntax
|
||||||
```C
|
```C
|
||||||
if(boolean_expression)
|
if(boolean_expression)
|
||||||
@ -42,6 +43,7 @@ else
|
|||||||
//Block of Statements executed when boolean_expression is false
|
//Block of Statements executed when boolean_expression is false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
```C
|
```C
|
||||||
int a = 300;
|
int a = 300;
|
||||||
@ -54,8 +56,8 @@ else
|
|||||||
printf("a is more than 200\n");
|
printf("a is more than 200\n");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
#### Result
|
|
||||||
|
|
||||||
|
#### Result
|
||||||
`a is more than 200`
|
`a is more than 200`
|
||||||
|
|
||||||
## 3. if...else if...else statement
|
## 3. if...else if...else statement
|
||||||
@ -83,6 +85,7 @@ else
|
|||||||
//Block of Statements executed when all boolean_expression_1, boolean_expression_2 and boolean_expression_3 are false
|
//Block of Statements executed when all boolean_expression_1, boolean_expression_2 and boolean_expression_3 are false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
```C
|
```C
|
||||||
int a = 300;
|
int a = 300;
|
||||||
@ -103,12 +106,13 @@ else
|
|||||||
printf("a is more than 300\n");
|
printf("a is more than 300\n");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
#### Result
|
|
||||||
|
|
||||||
|
#### Result
|
||||||
`a is equal to 300`
|
`a is equal to 300`
|
||||||
|
|
||||||
## 4. Nested if statement
|
## 4. Nested if statement
|
||||||
It is always legal in C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).
|
It is always legal in C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).
|
||||||
|
|
||||||
#### Syntax
|
#### Syntax
|
||||||
```C
|
```C
|
||||||
if(boolean_expression_1)
|
if(boolean_expression_1)
|
||||||
@ -136,11 +140,11 @@ if(a == 100)
|
|||||||
|
|
||||||
```
|
```
|
||||||
#### Result
|
#### Result
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
a is equal to 100
|
a is equal to 100
|
||||||
b is equal to 200
|
b is equal to 200
|
||||||
```
|
```
|
||||||
|
|
||||||
## 5. Switch Case Statement
|
## 5. Switch Case Statement
|
||||||
The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.
|
The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.
|
||||||
|
|
||||||
@ -162,6 +166,7 @@ switch (n)
|
|||||||
// code to be executed if n doesn't match any constant
|
// code to be executed if n doesn't match any constant
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case.
|
When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case.
|
||||||
|
|
||||||
In the above pseudocode, suppose the value of n is equal to constant2. The compiler will execute the block of code associate with the case statement until the end of switch block, or until the break statement is encountered.
|
In the above pseudocode, suppose the value of n is equal to constant2. The compiler will execute the block of code associate with the case statement until the end of switch block, or until the break statement is encountered.
|
||||||
@ -169,7 +174,7 @@ In the above pseudocode, suppose the value of n is equal to constant2. The compi
|
|||||||
The break statement is used to prevent the code running into the next case.
|
The break statement is used to prevent the code running into the next case.
|
||||||
|
|
||||||
### Example:
|
### Example:
|
||||||
```C
|
```
|
||||||
// Program to create a simple calculator
|
// Program to create a simple calculator
|
||||||
// Performs addition, subtraction, multiplication or division depending the input from user
|
// Performs addition, subtraction, multiplication or division depending the input from user
|
||||||
|
|
||||||
@ -202,11 +207,6 @@ int main()
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case '/':
|
case '/':
|
||||||
if(secondNumber==0){
|
|
||||||
printf("division with zero is not allowed\n");
|
|
||||||
break;
|
|
||||||
//Avoid runtime error of division with zero
|
|
||||||
}
|
|
||||||
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/secondNumber);
|
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/secondNumber);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -218,6 +218,7 @@ int main()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Output
|
### Output
|
||||||
```
|
```
|
||||||
Enter an operator (+, -, *,): -
|
Enter an operator (+, -, *,): -
|
||||||
@ -235,32 +236,14 @@ Finally, the break statement ends the switch statement.
|
|||||||
|
|
||||||
If break statement is not used, all cases after the correct case is executed.
|
If break statement is not used, all cases after the correct case is executed.
|
||||||
|
|
||||||
## finding the Bigger among two numbers using if else statement.
|
|
||||||
```C
|
|
||||||
int a,b;
|
|
||||||
printf("Enter the first number: \n");
|
|
||||||
scanf("%d",&a);
|
|
||||||
printf("Enter the second number: \n");
|
|
||||||
scanf("%d",&b);
|
|
||||||
//comparing the numbers
|
|
||||||
if(a>b)
|
|
||||||
{
|
|
||||||
printf("A is the Bigger number");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("B is the bigger number");
|
|
||||||
}
|
|
||||||
```
|
|
||||||
## 6. Ternary operation
|
## 6. Ternary operation
|
||||||
|
|
||||||
The ternary operator (AKA conditional operator) is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison , and the third is the result upon a flase comparison .It can be thought of as a shortened way of writing an if-else statement. It is often used to to assign variables based on the result of a comparison.
|
The ternary operator (AKA conditional operator) is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison , and the third is the result upon a flase comparison .It can be thought of as a shortened way of writing an if-else statement. It is often used to to assign variables based on the result of a comparison.
|
||||||
|
|
||||||
#### Syntax
|
#### Syntax
|
||||||
```C
|
```C
|
||||||
v = (conditional_statement) ? value_if_true : value_if_false
|
v = (conditional_statement) ? value_if_true : value_if_false
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
```C
|
```C
|
||||||
int a, b = 10, c = 100;
|
int a, b = 10, c = 100;
|
||||||
@ -269,5 +252,11 @@ printf("%d", a);
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Result
|
#### Result
|
||||||
|
|
||||||
`2`
|
`2`
|
||||||
|
|
||||||
|
### More Information
|
||||||
|
https://www.dotnettricks.com/learn/c/conditional-statements-if-else-switch-ladder
|
||||||
|
https://www.programiz.com/c-programming/c-if-else-statement
|
||||||
|
http://www.tutorialspoint.com/ansi_c/c_control_statements.htm
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user