Decision making in C
Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
An if statement consists of a Boolean expression followed by one or more statements.
Syntax
The syntax of an ‘if’ statement in C programming language is −
if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ }
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.
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
Syntax
The syntax of an if…else statement in C programming language is −
if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ }
If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else block will be executed.
#include <stdio.h> int main() { int age; printf("Enter your age:"); scanf("%d",&age); if(age >=18) { printf("You are eligible for voting"); } else { printf("You are not eligible for voting"); } return 0; }
Output:
Enter your age:14
You are not eligible for voting
switch statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
Syntax
The syntax for a switch statement in C programming language is as follows −
switch(expression) { case constant-expression : statement(s); break; /* optional */ case constant-expression : statement(s); break; /* optional */ /* you can have any number of case statements */ default : /* Optional */ statement(s); }
The following rules apply to a switch statement −
- The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
- You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
- The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
- When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
- When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
- Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
- A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
#include <stdio.h> int main() { int num = 8; switch (num) { case 7: printf("Num variable Value is 7"); break; case 8: printf("Num variable Value is 8"); break; case 9: printf("Num variable Value is 9"); break; default: printf("Out of range"); break; } return 0; }
Output:
Num variable Value is 8
Another switch statement example using char datatype.
#include <stdio.h> int main() { char ch='b'; switch (ch) { case 'd': printf("Case is d "); break; case 'b': printf("Case is b "); break; case 'c': printf("Case is c "); break; default: printf("Default ecase"); } return 0; }
Output:
Case is b
When the above code is compiled and executed, it produces the following result −
Well done Your grade is B
It is possible to have a switch as a part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.
Syntax
The syntax for a nested switch statement is as follows −
switch(ch1) { case 'A': printf("This A is part of outer switch" ); switch(ch2) { case 'A': printf("This A is part of inner switch" ); break; case 'B': /* case code */ } break; case 'B': /* case code */ }
Example
#include <stdio.h> int main () { /* local variable definition */ int a = 100; int b = 200; switch(a) { case 100: printf("This is part of outer switch\n", a ); switch(b) { case 200: printf("This is part of inner switch\n", a ); } } printf("Exact value of a is : %d\n", a ); printf("Exact value of b is : %d\n", b ); return 0; }
When the above code is compiled and executed, it produces the following result −
This is part of outer switch This is part of inner switch Exact value of a is : 100 Exact value of b is : 200
The Conditional( ? : )Operator
conditional operator ? : can be used to replace if…else statements. It has the following general form −
Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.
The value of a ? expression is determined like this −
- Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression.
- If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.
#include<stdio.h> int main() { float num1, num2, max; printf("Enter two numbers: "); scanf("%f %f", &num1, &num2); max = (num1 > num2) ? num1 : num2; printf("Maximum of between %.2f and %.2f is = %.2f", num1, num2, max); return 0; }
Output:-
Enter two numbers: 12.5 10.5
Maximum of between 12.50 and 10.50 is = 12.50