Module 2 Decision Making
Module 2 Decision Making
The code in c program is executed sequentially from the first line of the program to its
last line, i.e., the second statement is executed after the first, the third statement is
executed after the second and so on..
C support two types of decision control statements that can alter the flow of sequence of
instructions. These include conditional and unconditional branching.
Selection /branching
statements
Conditional Unconditional
type type
Conditional statements:
The conditional branching statements help to jump from one part of the program to
another depending on whether a particular condition is satisfied or not.
1. if statement
3. if-else ladder
Principles of Programming using C Module 2
4. Nested if
5. Switch
1. if statement:
The if statement is the simplest form of decision control statements that is frequently used in
decision making.
Syntax:
Test false
if ( test Expression) expression
{
Statement1;
}
true
Statement2;
Statement block 1
Statement X
The if structure may include one statement or n statements enclosed within curly
brackets. First the test expression is evaluated.
If the test expression is true, the statement of if block (statement 1 to n)are executed,
otherwise these statements will be skipped and the execution will jump to statement.
The statement in an if construct is any valid c language statement and the test expression
is any valid C language expression that may include logical operators. Note that there is
no semicolon after the test expression .This is because the condition and statement should
be placed together as a single statement.
Example:
#include <stdio.h>
int main () {
int a = 10;
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
return 0;
}
Output:
2. if-else statement:
The if-else statement is an extension of simple if statement.
The test expression is evaluated, if the result is true, the statement followed by the
expression is executed else if the expression is false, the statement is skipped by the
compiler.
Syntax:
Test
expression
if (test Expression)
{
Statement1; true-block
}
else
{
Statement2; true-block Statement block 1 Statement block 1
}
Statement X;
Statement block X
Principles of Programming using C Module 2
If the Expression is true (or non-zero) then Statement1 will be executed; otherwise if
it is false (or zero),then Statement2 will be executed.
In this case either true block or false block will be executed, but not both.
Example:
#include <stdio.h>
int main () {
return 0;
}
Output:
3. Nested if .. else statement: When a series of decisions are involved, we have to use more than
oneif..else statement in nested form as shown below in the general syntax.
Syntax:
Principles of Programming using C Module 2
if (Expression1)
{
if(Expression2)
{
Statement1;
}
else
{
Statement2;
}
}
else if(Expression3)
{
Statement3;
}
else
{
Statement4;
}
Once we start nesting if ..else statements, we may encounter a classic problem known as
dangling else.
C solution to this problem is a simple rule “always pair an else to most recent
unpaired if in the currentblock”.
Example:
Principles of Programming using C Module 2
#include <stdio.h>
int main()
int age;
scanf("%d",&age);
if ( age < 18 )
else
else
}
Principles of Programming using C Module 2
return 0;
4. if –else ladder:
There is another way of putting ifs together when multipath decisions are involved. A multi path
decision is a chain of ifs in which the statement associated with eachelse is an if.
syntax:
if (Expression1)
{
Statement1;
}
else if(Expression2)
{
Statement2;
}
else if(Expression3)
{
Statement3;
}
else
{
Statement 4;
}
Next Statement ;
Principles of Programming using C Module 2
The conditions are evaluated from the top (of the ladder), downwards. As soon as true
condition is found, the statement associated with it is executed and control transferred to
the Next statement skipping the rest of the ladder.
When all conditions are false then the final else containing the default Statement4 will be
executed.
It is not necessary that every if statement should have an else block as C supports simple if
statements. After the first test expression or the first if branch, the programmer can have as
many else-if branches as he wants depending on the expressions that have to be tested.
Example:
Principles of Programming using C Module 2
#include<stdio.h>
void main( )
{
int a=20, b=5,c=3;
if((a>b) && (a>c))
printf(“A is greater\n”);
else if((b>a) && (b>c))
printf(“B is greater\n”);
Output:
A is greater
switch(choice)
{
case label1: block1;
break;
case label2: block2;
break;
case label3: block-3;
break;
default: default-block;
break;
}
Here switch, case, break and default are built-in C language words.
Principles of Programming using C Module 2
If the choice matches to label1 then block1 will be executed else if it evaluates to label2
then block2will be executed and so on.
If choice does not matches with any case labels, then default block will be executed.
Each of these case labels should be unique within the switch statement. block1,
block2, block3,are statement lists and may contain zero or more statements.
There is no need to put braces around these blocks. Note that case labels end with colon
(:).
Break statement at the end of each block signals end of a particular case and
causes an exit fromswitch statement.
The default is an optional case when present, it will execute if the value of the choice
does not matchwith any of the case labels
Principles of Programming using C Module 2
Example:
} printf(“Result=%d\n”,res);
}
In this program if ch=1 case ‘1’ gets executed and if ch=2, case ‘2’ gets executed and so on.
Iterative statements:
Iterative statements are used to repeate the execution of a list of statements,depending on the
value of an integer expression. C language supports 3 types of iterative statements also know as
“looping statements”.They are
1. While
2. Do-while loop
3. For loop
The while loop provides a mechanism to repeate one or more statements while a
particular condition is true.
In while loop the condition is tested before any of the statements in the statement block is
executed.If the condition is true , only the the statements will be executed otherwise if the
condition is false,the control will jump to statement Y ,which is the immediate statement
outside the while loop block.
From the flow chart diagram ,it is clear that we nee dto constantly update the condition of
the while loop.’
Note that if the condition is never update and the condition never becomes false then the
computer will run into an infinite loop which is never desirable.
A while loop is also referred to as a top-checking loop since the contol condition is
placed as the first line of the code.If the control condition evaluates to false ,then the
statements enclosed in the loop are never executed.
Principles of Programming using C Module 2
Statement X
Syntax:
Statementx;
while (condition)
{ Update the conditio
statement-block; condition n
}
Statement Y;
Statement block false true
Statement Y
Example:
#include <stdio.h>
int main () {
return 0;
}
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
Principles of Programming using C Module 2
value of a: 18
value of a: 19
do-while: It is a post-test loop (also called exit controlled loop) it has two keywords do and
while. The General syntax:
Statement X
do
{
statement-block;
}while(condition);
Statement Y
Statement X
Statement block
Update the
condition
expression
true
condition
false
Statement Y
The do-while loop is similar to the while loop. The only difference is that in do-while
loop, the test condition is tested at the end of the loop. Now that the test condition is
tested at the end, this clearly means that the body of the loop gets executed at least once.
Principles of Programming using C Module 2
Note that the test condition is enclosed in parentheses and followed by a semicolon. The
statement blocks are enclosed within curly brackets. The curly bracket is optional if there
is only one statement in the body of the do-while loop.
The major disadvantage of using a do-while loop is that it always executes at least once,
even if the user enters some invalid data, the loop will execute. One complete execution
of the loop takes place before the first comparison is actually done.
Example:
#include <stdio.h>
int main () {
/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
for loop:
Similar to the while and do-while loops, the for loop provides a mechanism to repeat a
task until a particular condition is true.
Principles of Programming using C Module 2
For loop is usually known as determinate or definite loop because the programmer knows
exactly how many times the loop will repeat.
The number of times the loop has to be executed can be determined mathematically by
checking the logic of the loop.
When a for loop is used, the loop variable is initialized only once. With every iteration of
the loop, the value of the loop variable is updated and the condition is checked. If the
condition is true, the statement block of the loop is executed, else the statements
comprising the statement block of the for loop are skipped and the control jumps to the
immediate statement following the for loop body.
In the syntax of for loop, initialization of the loop variable allows the programmer to give
it a value. Second, the condition specifies that while the condition expression is TRUE
the loop should continue to repeat itself. Every iteration of the loop must make the
condition near to approachable. So, with every iteration the loop variable must be
updated.
Syntax:
false
Controlling
condition for
true
Statement block
Statement Y
Example:
#include <stdio.h>
int main () {
int a;
return 0;
}
Principles of Programming using C Module 2
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
:
Principles of Programming using C Module 2
Nested loop:
Loop that can be placed inside other loops. Although this feature will work with any loop
such as while, do-while and for but it is most commonly used with the for loop, because
it this is easiest to control.
A for loop can be used to control the number of times that a particular set of statements
will be executed. Another outer loop could be used to control the number of times that a
whole loop is repeated.
Syntax:
The syntax for a nested for loop statement in C is as follows −
for ( init; condition; increment ) {
The syntax for a nested while loop statement in C programming language is as follows −
Principles of Programming using C Module 2
while(condition) {
while(condition) {
statement(s);
}
statement(s);
}
The syntax for a nested do...while loop statement in C programming language is as follows −
do {
statement(s);
do {
statement(s);
}while( condition );
}while( condition );
NOTE: A final note on loop nesting is that you can put any type of loop inside any other type of
loop
Example:
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf(“%d”, &a[i][j])
}
}
Principles of Programming using C Module 2
Jumping statements:
Break and continue: break and continue are unconditional control construct.
1. Break
In switch statement if the break statement is missing then every case from the matched case label till
the end of the switch, including the default is executed.
Syntax Example
#include<stdio.h>
while(condition) void main( )
{ {
Statements; int i;
if(condition) for(i=1; i<=5; i++)
{
break;
if(i==3)
Statements;
break;
} printf(“%d”, i)
OR }
for(){ }
statements;
if (condition) OUTPUT 12
break;
statements;
}
OR
do{
if (condition)
break;
}while(condition);
2. Continue
Similar to the break statement the continue statement can only appear in the body of a
loop. When the compiler encounters a continue statement then the rest of the statements
Principles of Programming using C Module 2
in the loop are skipped and the control is unconditionally transferred to the loop-
continuation portion of the nearest enclosing loop.
Syntax Flowchart
#include<stdio.h>
while(condition) void main( )
{ {
Statements; int i;
if(condition) for(i=1; i<=5; i++)
continue; {
Statements; if(i==3)
} continue;
OR printf(“%d”, i)
for(){ }
statements; }
if (condition)
continue; OUTPUT 1 2 4 5
statements;
}
OR
do{
if (condition)
continue;
}while(condition);
GOTO statement:
The goto statement is used to transfer control to a specified label. However the label must
reside in the same function and can appear only before one statement in the same
function.
Here, label is an identifier that specifies the place where the branch is to be made. Label
can be any valid variable name that is followed by a colon (:). The label is placed
immediately before the statement where the control has to be transferred.
The label can be placed anywhere in the program either before or after the goto
statement. Whenever the goto statement is encountered the control is immediately
transferred to the statements following the label. Therefore goto statement breaks the
normal sequential execution of the program.
If the label is placed after the goto statement , then it is called a forward jump and in case
Principles of Programming using C Module 2
Syntax Example
goto void main( )
{
label; int a=5,
b=7;
statement; goto end;
a=a+1;
statement; b=b+1;
end: printf(“a=%d b=%d”, a,b);
label: }
OUTPUT: 5,7