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

UNIT-3 Programming in C

The document discusses different types of control statements in C language including decision control statements like if and switch statements, looping statements like while and do-while, and branching statements like goto. It provides syntax and examples for each statement type and explains how they work.

Uploaded by

Manda Vaishnavi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

UNIT-3 Programming in C

The document discusses different types of control statements in C language including decision control statements like if and switch statements, looping statements like while and do-while, and branching statements like goto. It provides syntax and examples for each statement type and explains how they work.

Uploaded by

Manda Vaishnavi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT - 3

DECISION MAKING AND BRANCHING

Decision control and looping statements are used to execute a statement and group of
statement conditionally and/or repeatedly. These are used in the following situations:
 To execute statements based on a condition
 To execute a set of statements repeatedly until a condition is satisfied
 To branch the execution sequence from one statement to another statement.

In ‘C’ language, the control statements are classified into 3 types. They are as follows:

DECISION CONTROL STATEMENTS / CONDITIONAL BRANCHING


STATEMENTS
These statements are used to execute a statement or group of statements
depending on the given condition. There are mainly two types of conditional statements.
They are
1. if or if…else statement
2. switch statement

if statement:-
Flowchart:
The ‘if’ statement is a conditional execution
statement that executes the specified set of instructions
based on the condition.
Syntax:
T
if (condition) statement-block ;
condition

F Statement-block
In the above syntax, the condition is a relational/logical/boolen expression that must be
enclosed within parenthesis. If the condition is true then the statements in the statement-block
are executed, otherwise they are not executed. To specify more than one statement in a
statement-block, they must be enclosed within a pair of flower braces ({}).
Examples:
1. if (avg >= 75) printf(“grade is distinction”);
2. if (a < 0) a++;

’if…else’ statement:
It is also conditional execution statement that executes the statements based on the
condition. If the condition is true then it executes one group of instructions otherwise it
executes another group of statements.

Flowchart:

F T

condition

Syntax:
Statement-block2 Statement-block1

if (condition)
statement-block1 ;
else
statement-block2 ;

Here, if the condition is true then statement-block1 is executed otherwise statement-block2 is


executed.
Example-1:
if (a % 2 = = 0)
Printf(“Even number”);
else
Printf(“Odd Number”);

Nested ‘if’ Statement/elseif ladder:


When the ‘if’ statement is used within another ‘if’ statement then it is called “nested
if” statement. The general format is as follows:
Syntax:

if (condition1)
{
if (condition2)
statement-block1 ;
else
statement-block2 ;
}
else
{
if (condition3)
statement-block3 ;
else
statement-block4 ;
}

Flowchart:

T F
Cond-1

T F T F
Cond-2 Cond-3

Statement-block1 Statement-block2 Statement-block3 Statement-block4

In the above syntax,

i. If ‘condition1’ and ‘condition2’ are true then stament-block1 is executed


ii. If ‘condition1’ is true and ‘condition2’ is false then stament-block2 is executed
iii. If ‘condition1’ is false and ‘condition3’ is true then stament-block3 is executed
iv. If ‘condition1’ and ‘condition3’ are false then stament-block4 is executed
Example:
If (x= =y)

if (a= =b)

printf(“x is equal to y and a is equal to b”);

else

printf(“x is equal to y, but a is not equal to b”);

else

if (a= =b)

printf(“x is not equal to y, but a is equal to b”);

else

printf(“x is not equal to y and a is not equal to b”);

Switch Statement:

The ‘switch’ statement is a multiple branching statement. It can be used to make a


decision from the number of choices. It is an extension of ‘if…else’ statement.
The general format of switch statement is as follows:

Syntax:

switch (op)
{
case V1 : statement-block1;
[break;]
case V2 : statement-block2;
[break;]
: :
: :
case Vn : statement-block-n;
[break;]
[default : statement-block-d;]
}
In the above syntax, ‘op’ is either a constant, variable (or) expression that results an integer
value. The switch statement uses the value of ‘op’ to execute the statement-blocks.
V1, V2, …, Vn are the values that represent the value of ‘op’. These are the constants
either integer or character type. These values must be unique.

When statement-block contains many statements then they must be enclosed within a
pair of braces ({}).

Working: The switch statement searches for a match of switch variable (op) and case
constants (V1, V2, …, Vn). If a value matches to the ‘op’, the switch statement executes all the
statements in that case-block. If there is ‘break’ statement in that block then it terminates the
execution of switch statement. If ‘break’ is not present then all the statements of remaining
blocks are executed.

If any value doesn’t match to the ‘op’ then the ‘default’ block is executed, if it is
specified in the switch statement.

Example:
main( )

int a;

clrscr( );

printf(“Enter a single digit number”);

scanf(“%d”,&a);

switch (a)

case 1: printf(“Sunday”);

break;

case 2: printf(“Monday”);

break;

case 3: printf(“Tuesday”);

break;

case 4: printf(“Wednesday”);

break;

case 5: printf(“Thursday”);

break;
Goto Statement:
case 6: printf(“Friday”);

break;

case 7: printf(“Saturday”);

break;

default: printf(“Wrong Value”);


The ‘goto’ statement is an unconditional branching statement used to transfer the
program control from one place to another place within a program.

Syntax:

goto label ;

Here, the label is an identifier used to name the target location to which the control is
transferred. The target statement must be named with label name followed by a colon as
follows:

label :
statement ;

The label names must be unique within a program.

Working: While program execution, when ‘goto’ statement is reached then it branches the
execution control to the specified label in the program and it continues execution from that
point.

Example: Forward Jump Example: Backward Jump

main( ) main( )

{ int num; { int num;

printf(“Enter a value”); input: printf(“Enter a value”);

scanf(“%d”,&num); scanf(“%d”,&num);

if (num < 0 ) if (num < 0 )

goto end; goto input;

printf(“Value = %d”,num); printf(“Value = %d”,num);

end : printf(“End of program); }

}
Flowchart:

Switch (op)

T Statement-block1
case V1

T Statement-block2
case V2

T Statement-block-n
case Vn

do…while Statement:

This statement is used to execute one or more statements repeatedly as long as the
specified condition is satisfied. It is an exit control loop structure. The do…while structure
executes at least one time even initially the condition is false.

Flowchart:
Syntax:

do Statement-block
{
statement-block;
} while (condition) ; T
condition

F
In the above syntax,

1. The ‘condition’ is a relational expression that must be enclosed within parenthesis.


2. The statement-block specifies one or more statements. When there is more than one
statement, they must be enclosed in braces ({}).

Working: The ‘do…while’ statement executes all the statements in the statement-block first
and then tests the condition. When the condition true (non-zero) then the loop is repeated
otherwise the loop is terminated.

Example: A program to print first ‘n’ natural numbers.

#include<stdio.h>

main( )

int a=1, n;

clrscr( );

printf(“Enter n value”);

scanf(“%d”,&n);

do

printf(“%d \t”, a);

a++;
} while (a<=n);

You might also like