Add the title "More information " to the article (#22288)

This commit is contained in:
Muralidharan Sekar
2018-11-20 19:10:29 +05:30
committed by Tom
parent 248788a335
commit 77246a193d

View File

@ -1,273 +1,262 @@
--- ---
title: Conditional Statements title: Conditional Statements
--- ---
# Conditional Statements in C # Conditional Statements in C
Conditional Statements are also known as Branching Statements. They are so called because the program chooses to follow one branch or another. Conditional Statements are also known as Branching Statements. They are so called because the program chooses to follow one branch or another.
## 1. if statement ## 1. if statement
This is the most simple form of the conditional statements. It consists of a Boolean expression followed by one or more statements. If the Boolean expression evaluates to **true**, then the block of code inside the 'if' statement will be executed. If the Boolean expression evaluates to **false**, then the first set of code after the end of the 'if' statement (after the closing curly brace) will be executed. This is the most simple form of the conditional statements. It consists of a Boolean expression followed by one or more statements. If the Boolean expression evaluates to **true**, then the block of code inside the 'if' statement will be executed. If the Boolean expression evaluates to **false**, then the first set of code after the end of the 'if' statement (after the closing curly brace) will be executed.
C programming language **_assumes any non-zero and non-null values as true_** and if it is **_either zero or null, then it is assumed as false_** value. C programming language **_assumes any non-zero and non-null values as true_** and if it is **_either zero or null, then it is assumed as false_** value.
#### Syntax #### Syntax
```C ```C
if(boolean_expression) if(boolean_expression)
{ {
//Block of Statements executed when boolean_expression is true //Block of Statements executed when boolean_expression is true
} }
``` ```
#### Example
```C #### Example
int a = 100; ```C
if(a < 200) int a = 100;
{ if(a < 200)
printf("a is less than 200\n" ); {
} printf("a is less than 200\n" );
``` }
#### Result ```
`a is less than 200` #### Result
`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
```C #### Syntax
if(boolean_expression) ```C
{ if(boolean_expression)
//Block of Statements executed when boolean_expression is true {
} //Block of Statements executed when boolean_expression is true
else }
{ else
//Block of Statements executed when boolean_expression is false {
} //Block of Statements executed when boolean_expression is false
``` }
#### Example ```
```C
int a = 300; #### Example
if(a < 200) ```C
{ int a = 300;
printf("a is less than 200\n"); if(a < 200)
} {
else printf("a is less than 200\n");
{ }
printf("a is more than 200\n"); else
} {
``` printf("a is more than 200\n");
#### Result }
```
`a is more than 200`
#### Result
## 3. if...else if...else statement `a is more than 200`
When using if...else if..else statements, there are few points to keep in mind -
- An **if** can have **zero or one else**'s and it **must come after any else if**'s. ## 3. if...else if...else statement
- An **if** can have **zero to many else if**'s and they **must come before the else**. When using if...else if..else statements, there are few points to keep in mind -
- Once an **else if** succeeds, none of the remaining else if's or else's will be tested. - An **if** can have **zero or one else**'s and it **must come after any else if**'s.
- An **if** can have **zero to many else if**'s and they **must come before the else**.
#### Syntax - Once an **else if** succeeds, none of the remaining else if's or else's will be tested.
```C
if(boolean_expression_1) #### Syntax
{ ```C
//Block of Statements executed when boolean_expression_1 is true if(boolean_expression_1)
} {
else if(boolean_expression_2) //Block of Statements executed when boolean_expression_1 is true
{ }
//Block of Statements executed when boolean_expression_1 is false and boolean_expression_2 is true else if(boolean_expression_2)
} {
else if(boolean_expression_3) //Block of Statements executed when boolean_expression_1 is false and boolean_expression_2 is true
{ }
//Block of Statements executed when both boolean_expression_1 and boolean_expression_2 are false and boolean_expression_3 is true else if(boolean_expression_3)
} {
else //Block of Statements executed when both boolean_expression_1 and boolean_expression_2 are false and boolean_expression_3 is true
{ }
//Block of Statements executed when all boolean_expression_1, boolean_expression_2 and boolean_expression_3 are false else
} {
``` //Block of Statements executed when all boolean_expression_1, boolean_expression_2 and boolean_expression_3 are false
#### Example }
```C ```
int a = 300;
if(a == 100) #### Example
{ ```C
printf("a is equal to 100\n"); int a = 300;
} if(a == 100)
else if(a == 200) {
{ printf("a is equal to 100\n");
printf("a is equal to 200\n"); }
} else if(a == 200)
else if(a == 300) {
{ printf("a is equal to 200\n");
printf("a is equal to 300\n"); }
} else if(a == 300)
else {
{ printf("a is equal to 300\n");
printf("a is more than 300\n"); }
} else
``` {
#### Result printf("a is more than 300\n");
}
`a is equal to 300` ```
## 4. Nested if statement #### Result
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). `a is equal to 300`
#### Syntax
```C ## 4. Nested if statement
if(boolean_expression_1) 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).
{
//Executed when boolean_expression_1 is true #### Syntax
if(boolean_expression_2) ```C
{ if(boolean_expression_1)
//Executed when both boolean_expression_1 and boolean_expression_2 are true {
} //Executed when boolean_expression_1 is true
} if(boolean_expression_2)
{
``` //Executed when both boolean_expression_1 and boolean_expression_2 are true
#### Example }
```C }
int a = 100;
int b = 200; ```
if(a == 100) #### Example
{ ```C
printf("a is equal to 100\n" ); int a = 100;
if(b == 200) int b = 200;
{ if(a == 100)
printf("b is equal to 200\n"); {
} printf("a is equal to 100\n" );
} if(b == 200)
{
``` printf("b is equal to 200\n");
#### Result }
}
```bash
a is equal to 100 ```
b is equal to 200 #### Result
``` ```bash
## 5. Switch Case Statement a is equal to 100
The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand. b is equal to 200
```
### Syntax of switch case
``` ## 5. Switch Case Statement
switch (n) The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.
{
case constant1: ### Syntax of switch case
// code to be executed if n is equal to constant1; ```
break; switch (n)
{
case constant2: case constant1:
// code to be executed if n is equal to constant2; // code to be executed if n is equal to constant1;
break; break;
.
. case constant2:
. // code to be executed if n is equal to constant2;
default: break;
// 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. default:
// code to be executed if n doesn't match any constant
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. }
```
The break statement is used to prevent the code running into the next 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.
### Example:
```C 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.
// Program to create a simple calculator
// Performs addition, subtraction, multiplication or division depending the input from user The break statement is used to prevent the code running into the next case.
# include <stdio.h> ### Example:
```
int main() // Program to create a simple calculator
{ // Performs addition, subtraction, multiplication or division depending the input from user
char operator; # include <stdio.h>
double firstNumber,secondNumber;
int main()
printf("Enter an operator (+, -, *, /): "); {
scanf("%c", &operator);
char operator;
printf("Enter two operands: "); double firstNumber,secondNumber;
scanf("%lf %lf",&firstNumber, &secondNumber);
printf("Enter an operator (+, -, *, /): ");
switch(operator) scanf("%c", &operator);
{
case '+': printf("Enter two operands: ");
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber); scanf("%lf %lf",&firstNumber, &secondNumber);
break;
switch(operator)
case '-': {
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber); case '+':
break; printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber); case '-':
break; printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
break;
case '/':
if(secondNumber==0){ case '*':
printf("division with zero is not allowed\n"); printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
break; break;
//Avoid runtime error of division with zero
} case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/secondNumber); printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/secondNumber);
break; break;
// operator is doesn't match any case constant (+, -, *, /) // operator is doesn't match any case constant (+, -, *, /)
default: default:
printf("Error! operator is not correct"); printf("Error! operator is not correct");
} }
return 0; return 0;
} }
``` ```
### Output
``` ### Output
Enter an operator (+, -, *,): - ```
Enter two operands: 32.5 Enter an operator (+, -, *,): -
12.4 Enter two operands: 32.5
32.5 - 12.4 = 20.1 12.4
``` 32.5 - 12.4 = 20.1
The '-' operator entered by the user is stored in operator variable. And, two operands 32.5 and 12.4 are stored in variables firstNumber and secondNumber respectively. ```
The '-' operator entered by the user is stored in operator variable. And, two operands 32.5 and 12.4 are stored in variables firstNumber and secondNumber respectively.
Then, control of the program jumps to
``` Then, control of the program jumps to
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber); ```
``` printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
Finally, the break statement ends the switch statement. ```
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 ## 6. Ternary operation
int a,b; 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.
printf("Enter the first number: \n");
scanf("%d",&a); #### Syntax
printf("Enter the second number: \n"); ```C
scanf("%d",&b); v = (conditional_statement) ? value_if_true : value_if_false
//comparing the numbers ```
if(a>b)
{ #### Example
printf("A is the Bigger number"); ```C
} int a, b = 10, c = 100;
else a = (b > c) ? 1 : 2;
{ printf("%d", a);
printf("B is the bigger number"); ```
}
``` #### Result
## 6. Ternary operation `2`
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. ### More Information
https://www.dotnettricks.com/learn/c/conditional-statements-if-else-switch-ladder
#### Syntax https://www.programiz.com/c-programming/c-if-else-statement
```C http://www.tutorialspoint.com/ansi_c/c_control_statements.htm
v = (conditional_statement) ? value_if_true : value_if_false
```
#### Example
```C
int a, b = 10, c = 100;
a = (b > c) ? 1 : 2;
printf("%d", a);
```
#### Result
`2`