0% found this document useful (0 votes)
7 views

Decision Control Structures

decision control

Uploaded by

asenathderlene27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Decision Control Structures

decision control

Uploaded by

asenathderlene27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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.

There are four different types of if statement in C. These are:

• Simple if Statement
• if-else Statement
• Nested if-else Statement
• else-if Ladder

The basic format of if statement is:


Syntax:
if(test_expression)
{
statement 1;
statement 2;
...
}
'Statement n' can be a statement or a set of statements, and if the test expression is
evaluated to true, the statement block will get executed, or it will get skipped.
Figure - Flowchart of if Statement:

Example of a C Program to Demonstrate if Statement


Example:
#include<stdio.h>

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);

/* check whether the number is negative number */ if (number & lt; 0) {


/* If it is a negative then convert it into positive. */
number = -number;
printf( & quot; The absolute value is % d\ n & quot;, number);
}
grtch();
}
Program Output:

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:

# Example of a C Program to Demonstrate if-else Statement


# if-else Statements in C - Video Tutorial

Example of a C Program to Demonstrate if-else Statement


Example:
#include<stdio.h>

main()
{
int a, b;

printf("Please enter the value for a:");


scanf("%d", & amp; a);

printf("\nPlease the value for b:");


scanf("%d", & amp; 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);

/* check whether the number is negative number */ if (num < 0)


printf("The number is negative.");
else
printf("The number is positive.");
}
Program Output:

C Nested if-else Statements


Nested if else statements play an important role in C programming, it means you can
use conditional statements inside another conditional statement.
The basic format of Nested if else statement is:
Syntax:
if(test_expression one)
{
if(test_expression two) {
//Statement block Executes when the boolean test expression two is true.
}
}
else
{
//else statement block
}

Outline:

# Example of a C Program to Demonstrate Nested if-else Statement


# Nested if-else Statements in C - Video Tutorial

Example of a C Program to Demonstrate Nested if-else Statement


Example:
#include<stdio.h>

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:

value of x is 20, and value of y is 30.

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:

# Example of a C Program to Demonstrate else if Statement


# else if Statements in C - Video Tutorial

Example of a C Program to Demonstrate else if Statement


Example:
#include<stdio.h>

main()
{
int a, b;

printf("Please enter the value for a:");


scanf("%d", & amp; a);

printf("\nPlease enter the value for b:");


scanf("%d", & amp; 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.

The general form of the goto statement is:


Syntax:

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;

/*This is the backward jump of goto statement */


Outline:

# An Example of a C Program to Demonstrate goto Statement


# goto Statement in C - Video Tutorial

An Example of a C Program to Demonstrate goto Statement


Example:
#include<stdio.h>

void main()
{
int age;

g: //label name
printf("you are Eligible\n");
s: //label name
printf("you are not Eligible");

printf("Enter you age:");


scanf("%d", &age);
if(age>=18)
goto g; //goto label g
else
goto s; //goto label s
getch();
}
C switch Statements
C switch statement is used when you have multiple possibilities for the if statement.
Switch case will allow you to choose from multiple options. When we compare it to a
general electric switchboard, you will have many switches in the switchboard but you
will only select the required switch, similarly, the switch case allows you to set the
necessary statements for the user.
The basic format of the switch statement is:
Syntax:
switch(variable)
{
case 1:
//execute your code
break;

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:

# Example of a C Program to Demonstrate Switch Statement


# switch Statements in C - Video Tutorial

Example of a C Program to Demonstrate Switch Statement


Example:
#include<stdio.h>

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.

You might also like