Decision Control Structures
Decision Control Structures
C Decision Making
C conditional statements allow you to make a decision, based upon the result of a
condition. These statements are called Decision Making Statements or Conditional
Statements.
So far, we have seen that all set of statements in a C program gets executed
sequentially in the order in which they are written and appear. This occurs when there is
no jump based statements or repetitions of certain calculations. But some situations
may arise where we may have to change the order of execution of statements
depending on some specific conditions. This involves a kind of decision making from a
set of calculations. It is to be noted that C language assumes any non-zero or non-null
value as true and if zero or null, treated as false.
This type of structure requires that the programmers indicate several conditions for
evaluation within a program. The statement(s) will get executed only if the condition
becomes true and optionally, alternative statement or set of statements will get
executed if the condition becomes false.
The flowchart of the Decision-making technique in C can be expressed as:
C languages have such decision-making capabilities within its program by the use of
following the decision making statements:
Conditional Statements in C
• If statement
o if statement
o if-else statement
o Nested if-else statement
o else if-statement
• goto statement
• switch statement
• Conditional Operator
C if statements
If statements in C is used to control the program flow based on some condition, it's used
to execute some statement code block if the expression is evaluated to true. Otherwise,
it will get skipped. This is the simplest way to modify the control flow of the program.
The if statement in C can be used in various forms depending on the situation and
complexity.
• Simple if Statement
• if-else Statement
• Nested if-else Statement
• else-if Ladder
main()
{
int a = 15, b = 20;
if (b & gt; a) {
printf("b is greater");
}
}
Program Output:
Example:
#include<stdio.h>
main()
{
int number;
printf( & quot; Type a number: & quot;);
scanf( & quot; % d & quot;, & amp; number);
C if-else Statements
If else statements in C is also used to control the program flow based on some
condition, only the difference is: it's used to execute some statement code block if the
expression is evaluated to true, otherwise executes else statement code block.
The basic format of if else statement is:
Syntax:
if(test_expression)
{
//execute your code
}
else
{
//execute your code
}
Figure - Flowchart of if-else Statement:
Outline:
main()
{
int a, b;
if (a & gt; b) {
printf("\n a is greater");
} else {
printf("\n b is greater");
}
}
Program Output:
Example:
#include<stdio.h>
main() {
int num;
printf("Enter the number:");
scanf("%d", num);
Outline:
main()
{
int x=20,y=30;
if(x==20)
{
if(y==30)
{
printf("value of x is 20, and value of y is 30.");
}
}
}
Execution of the above code produces the following result.
Output:
C else-if Statements
else-if statements in C is like another if condition, it's used in a program when if
statement having multiple decisions.
The basic format of else if statement is:
Syntax:
if(test_expression)
{
//execute your code
}
else if(test_expression n)
{
//execute your code
}
else
{
//execute your code
}
Outline:
main()
{
int a, b;
if (a & gt; b)
{
printf("\n a is greater than b");
}
else if (b & gt; a)
{
printf("\n b is greater than a");
}
else
{
printf("\n Both are equal");
}
}
Program Output:
C goto Statement
So far we have discussed the if statements and how it is used in C to control statement
execution based on some decisions or conditions. The flow of execution also depends on
other statements which are not based on conditions that can control the flow.
C supports a unique form of a statement that is the goto Statement which is used to
branch unconditionally within a program from one point to another. Although it is not a
good habit to use the goto statement in C, there may be some situations where the use
of the goto statement might be desirable.
The goto statement is used by programmers to change the sequence of execution of a C
program by shifting the control to a different part of the same program.
goto label;
A label is an identifier required for goto statement to a place where the branch is to be
made. A label is a valid variable name which is followed by a colon and is put
immediately before the statement where the control needs to be jumped/transferred
unconditionally.
Syntax:
goto label;
- - -- - -
- - - - - - - -
label:
statement - X;
/* This the forward jump of goto statement */
or
label:
- - -- - -
- - - - - - - -
goto label;
void main()
{
int age;
g: //label name
printf("you are Eligible\n");
s: //label name
printf("you are not Eligible");
case n:
//execute your code
break;
default:
//execute your code
break;
}
After the end of each block it is necessary to insert a break statement because if the
programmers do not use the break statement, all consecutive blocks of codes will get
executed from every case onwards after matching the case block.
Outline:
main()
{
int a;
printf("Please enter a no between 1 and 5: ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("You chose One");
break;
case 2:
printf("You chose Two");
break;
case 3:
printf("You chose Three");
break;
case 4:
printf("You chose Four");
break;
case 5:
printf("You chose Five.");
break;
default :
printf("Invalid Choice. Enter a no between 1 and 5");
break;
}
}
Program Output:
When none of the cases is evaluated to true, the default case will be executed,
and break statement is not required for default statement.