Module-2 Operators & Decision Control Statements
Module-2 Operators & Decision Control Statements
The basic decision statement in the computer is the two way selection.
The decision is described to the computer as conditional statement that can be answered TRUE or FALSE.
If the answer is TRUE, one or more action statements are executed.
If answer is FALSE, the different action or set of actions are executed.
Regardless of which set of actions is executed, the program continues with next statement.
1. if statement: The general form of simple if statements is shown below.
if (Expression)
{
Statement1;
}
Statement2;
The Expression is evaluated first, if the value of Expression is true (or non zero) then Statement1
will be executed; otherwise if it is false (or zero), then Statement1 will be skipped and the
execution will jump to the Statement2.
Remember when condition is true, both the Statement1 and Statement2 are executed in sequence.
This is illustrated in Figure
Expression True
False Statement1
Statement2
Example:
#include<stdio.h>
void main( )
{
int a=20, b=11;
if (a >b)
{
printf(“A is greater\n”);
}
}
Output: A is greater
2. if..else statement: The if..else statement is an extension of simple if statement.
if (Expression)
{
Statement1; true-block
}
else
{
Statement2; true-block
}
Statement3;
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.
This is illustrated in Figure 2. In both the cases, the control is transferred subsequently to the Statement3.
False True
Expression
Statement2 Statement1
Statement3
If Expression1 is true, check for Expression2, if it is also true then Statement1 is executed.
If Expression1 is true, check for Expression2, if it is false then Statement2 is executed.
If Expression1 is false, then Statement3 is executed.
Once we start nesting if .. else statements, we may encounter a classic problem known as dangling else.
This problem is created when no matching else for every if.
C solution to this problem is a simple rule “always pair an else to most recent unpaired if in the
currentblock”.
Solution to the dangling else problem, a compound statement.
In compound statement, we simply enclose true actions in braces to make the second if a
compoundstatement.
}
else
{
printf(“C is greater\n”);
}
}
else
{
if(b>c)
{
printf(“B is greater\n”);
}
else
{
printf(“C is greater\n”);
}
}
}
OUTPUT:
A is greater
4. else-if ladder or cascaded if else: 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
each else is an if. It takes the following form.
if (Expression1)
{
Statement1;
}
else if(Expression2)
{
Statement2;
}
else if(Expression3)
{
Statement3;
}
else
{
Statement4;
}
Next Statement;
This construct is known as the else if ladder.
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.
Output: A is greater
5. Switch Statement:
C language provides a multi-way decision statement so that complex else-if statements can be
easilyreplaced by it.
C language‟s multi-way decision statement is called switch.
General syntax of switch statement is as follows:
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.
If the choice matches to label1 then block1 will be executed else if it evaluates to label2 then block2
will be executed and so on.
If choice does not matches with any case labels, then default block will be executed.
In this program if ch=1 case „1‟ gets executed and if ch=2, case „2‟ gets executed and so on.
Iterative Statements
Definition of Loop: It is a programming structure used to repeatedly carry out a particular
instruction/statement until a condition is true. Each single repetition of the loop is known as an iteration
of the loop.
Loops can be classified into two types based on the placement of test-condition.
If the test-condition is given in the beginning such loops are called pre-test loops (also known as entry-
controlled loops).
Otherwise if test condition is given at the end of the loop such loops are termed as post-test loops (or exit
controlled loops).
Note Figure 1:
1. Here condition is at the beginning of loop. That is why it is called pre-test loop
2. It is also called as entry controlled loop because condition is tested before entering into the loop.
3. while is a keyword which can be used here.
Note Figure 2:
1. Here condition is at the end of the loop. That is why it is called post-test loop.
2. It is also called as exit controlled loop because condition is tested after body of the loop is
executed at least once.
3. do and while are keywords which can be used here.
LOOPS IN C
C language provides 3 looping structures namely:
1. while loop
2. do….while loop
3. for loop
When we know in advance exactly how many times the loop will be executed, we use a
counter-controlled loop. A counter controlled loop is sometimes called definite repetition loop.
In a sentinel-controlled loop, a special value called a sentinel value is used to change the loop
controlexpression from true to false. A sentinel-controlled loop is often called indefinite repetition loop.
i. while loop:
It is a pre-test loop (also known as entry controlled loop).
This loop has following syntax:
Statement x;
while (condition)
{
statement-block;
}
Statement y;
In the syntax given above „while‟ is a key word and condition is at beginning of the loop.
If the test condition is true the body of while loop will be executed.
After execution of the body, the test condition is once again evaluated and if it is true, the body is
executed once again.
This process is repeated until condition finally becomes false and control comes out of the body of
theloop.
FLOWCHART:
Here is an example program using while loop for finding sum of 1 to 10.
Example: WAP to find sum of 1 to 10 using while.
#include<stdio.h>
void main()
{
int i=1, sum=0;
while (i<=10)
{
sum=sum+i;
i++;
}
printf(“%d”, sum);
}
Statement x;
do
{
Statement-block;
} while (condition);
Statement y;
In this loop the body of the loop is executed first and then test condition is evaluated.
If the condition is true, then the body of loop will be executed once again. This process continues as
long as condition is true.
When condition becomes false, the loop will be terminated and control comes out of the loop.
FLOWCHART:
Example: WAP to find sum of 1 to 10 using do… while
#include<stdio.h>
void main()
{
int i=1, sum=0;
do
{
sum=sum+i;
i=i++;
} while (i<=5)
printf(“%d”, sum);
}
FLOWCHART:
Example: WAP to find the sum of 10 numbers using for loop.
#include<stdio.h>
void main( )
{
int i, sum=0;
for (i=1; i<=10; i++)
{
sum=sum+i;
}
printf(“%d”, sum);
}
Note: In for loops whether both i++ or ++i operations will be treated as pre-increment only.
If all the expressions are omitted then there must be 2 semicolons inside a for loop
Don‟t put the semicolon at the end of for loop
Multiple initializations must be separated with a colon operator
If there is no initialization to be done, then initialization statement can be skipped by
giving semicolon
The loop variable is updated in the statement block
Multiple conditions in the test expressions can be tested by using operators (&& and ||)
Multiple statements inside the for loop separated with comma operator
FLOWCHART: `
Example: for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf(“%d”, &a[i][j])
}
}
Note: If updation is not present in loops then, it will execute infinite times.
If initialization is not given then, program prints nothing.
1. break statement:
OUTPUT 12
Transfers control out of the do-while loop Transfers control out of the for loop
for(initialization;condition;updation) while(condition)
{ {
………………. ……………….
for(initialization;condition;Updation) if(condition)
{ break;
………………. ……………….
if(condition) }
break; ……………….
……………….
}
……………….
}
Transfers control out of the inner-for loop Transfers control out of the while loop
2. continue statement
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
;printf(“%d”, i)
}
}
OUTPUT 124 5
Transfers the control to the condition Transfers the control to the condition
expression of the do-while loop expression of the for loop
for(initialization;condition;updation) while(condition)
{ {
………………. ……………….
for(initialization;condition;Updation) if(condition)
{ continue;
………………. ……………….
if(condition) }
continue; ……………….
……………….
}
……………….
}
Transfers the control to the condition Transfers the control to the condition
expression of the inner-for loop expression of the while loop
GOTO Statement
goto statement is used to transfer the control to a specified label.
goto is an unconditional branching statement.
The syntax of goto is as follows:
Syntax Example
goto label; void main( )
{
statement1; int a=5, b=7;
goto end;
statement2; a=a+1;
b=b+1;
label: end: printf(“a=%d b=%d”, a,b);
}