0% found this document useful (0 votes)
10 views16 pages

Prog. With C & C++ (Unit-II) - 1

The document provides an overview of control statements in C programming, which alter the sequential execution of statements. It categorizes control statements into conditional statements, looping statements, and jump statements, detailing their syntax and functionality. Examples are provided for each type, illustrating how they can be used to control program flow.

Uploaded by

konad47993
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)
10 views16 pages

Prog. With C & C++ (Unit-II) - 1

The document provides an overview of control statements in C programming, which alter the sequential execution of statements. It categorizes control statements into conditional statements, looping statements, and jump statements, detailing their syntax and functionality. Examples are provided for each type, illustrating how they can be used to control program flow.

Uploaded by

konad47993
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/ 16

Page No: 1

CONTROL STATEMENTS

Introduction:
As usually the statements in any C program will be executed one by one in
sequence order starting from main function. But we can change the order of execution
of the statements using control statements. That is control statements are used to
execute the statements randomly as per our requirement.
Control statements are used to control the execution of the statements in the
program. In C programming language; there are 3 categories of control statements as
discussed below;
1. Conditional statements (Decision making statements)
2. Looping statements (Iterative statements)
3. Jump statements (Branching statements)

1. Conditional Statements

Conditional statements are again classified into several types. Mainly used
statements are; simple if, if…else, nested if, nested if…else and switch statements. Let
us discuss in brief about all.

Simple if statement: Flow chart:


Syntax: if(condition) condition false
{
Block of statements; true
} if block
Statements-X;

Outside of the if
block

In the above syntax; first of all tests the condition. Condition may be either true or
false. Block of statements (if block) and Statements-X (outside of if block) are executed if
condition is true otherwise if block won’t be executed only statements-X are executed.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 2

Let us take an example program;

Output: first run-

Output: second run-

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 3

if…else statement: Flow chart:


Syntax: if(condition)
{
If block statements; true condition false
}
Else if block else block
{
Else block statements;
}

In the above syntax; first of all tests the condition. If the condition is true then
executes if block only. If the condition is false then executes else block only.

Let us take an example program;

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 4

Output: first run-

Output: second run-

Nested if statement:
If we write an if statement isnside another if statement, then those statements
are called nested if statements.
Syntax: if(condition-1)
{
if(condition-2)
{
………….
}
}

Let us take an example program;


#include <stdio.h> //header file section
void main()
{
int a = 40, b = 30, c = 20;
if (a > b)
{
if (a > c)
{
printf("The value a is greater than b and c ");
}
}
printf("\nThis is normal flow ");
}

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 5

Nested if…else statement:


If we write if…else statement inside another if…else then those statements are
called nested if…else statements.
Syntax: if(condition-1)
{
if(condition-2)
{
………
}
else
{
………
}
}
else
{
if(condition-3)
{
………
}
else
{
………
}
}

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 6

Let us take an example program;

// A C program to find biggest of three numbers;


#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
clrscr();
printf(“Enter three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf(“ %d is big ’’,a);
}
else
{
printf(“ %d is big ”,c);
}
}
else
{
if(b>c)
{
printf(“ %d is big “,b);
}
else
{
printf(“ %d is big”,c);
}
}

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 7

switch statement:
Syntax: switch(expression)
{
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
…………
case value3:
//code to be executed;
break;
default:
// code to be executed if all cases are not matched;
}

Flow chart:

Expression

Matched
Case-1 Statements-1 break

Unmatched
Matched
Case-2 Statements-2 break

Unmatched
Matched
Case-n Statements-n break

Unmatched

Default default statements

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 8

In the above syntax;


 switch, case, break and default are the keywords.
 Case values may be integers or characters.
 If the expression is matching with any case values, then that case statements will
be executed.
 Every case statement should be followed by break keyword.
 After executing any case statements, program terminates logically because of
break statement.
 If no case value is matching with expression, then default statements will be
executed.

Let us take an example program;

#include<stdio.h>
void main()
{
int n;
printf("Please enter a number between 1 and 5: ");
scanf("%d",&n);
switch(n)
{
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");
break;
}
}

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 9

2. Looping Statements

Looping statements are used to execute the statements repeatedly. Looping


statements are again classified as while, do…while and for loop statements.
Note: The block of statements which are executing repeatedly is called as a loop.

while loop:
Syntax: initialization; Flow chart:
while(condition)
{ Condition false
while block statements;
increment/decrement; true
} while blockstatements
Statements-X
Statements-X

In the above syntax; first of all condition will be checked. If condition is true, then
while block statements executed. After completing while block statements it again
checks for condition. If it is true, again while block statements will execute. Like the
same scenario; while block executes repeatedly until the condition get false. Once the
condition is false, then it executes statements-X (outside of while block) without
entering into while block.

Let us take an example program;

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 10

Output:

In the above program; while block is executed 100 times.

do…while loop:

Syntax: initialization; Flow chart: do-block statements


do
{
do-block statements;
increment/decrement; true while condition
} while(condition);
false

In the above syntax; first of all do-block will be executed and then while condition
is checked. If condition is true, then again do-block statements executed. After
completing do-block statements it again checks condition. If it is true, again do-block
statements will execute. Like the same scenario; do-block executes repeatedly until the
while condition get false. Once the condition is false, then it executes outside of do-
block without entering into do-block.

Let us take an example program;

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 11

Output:

for loop:
Syntax: for(initialization; condition; updation) Flow chart:
{ initialization
block of statements;
}
Condition False

true
updation block of statements

In the above syntax; first of all initialization part will be executed and then checks
for condition. If condition is true then block of statements will execute. After executing
block of statements, updation will be executed. After updating the value, again checks
for condition. If it is true, again block of statements will be executed. Like the same
scenario, the block will be executed repeatedly until the condition gets false. Once the

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 12

condition is false then comes out from the block and executes the statements outside
the block.

Let us take an example program;

Output:

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 13

3. Jump Statements/Branching Statements:

Jump statements are used to execute the statements randomly. These jump
statements are again classified as break, continue, goto and return.

break:
In C, break statement can be used in two ways;
First; it can be used to terminate a statement sequence in switch
Second; it can be used to exit a loop as in for loop

Let us take an example program;

Output:

In the above program; whenever the variable i reaches the value 5, then if block
will be executed which contains break. So, program will be terminated logically. That is
control comes out from for loop.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 14

continue:
The continue statement skips the current iteration of for, while and do…while
loops and continue the flow of execution.

Let us take an example program;

Output:

In the above program; when the variable i reaches the value 5, then if block is
executed which contains continue statement. So, then iteration is skipped for i=5 and
continues the flow of execution.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 15

goto:
The goto statement is used to transfer the control from one location to other
location.
The goto statement contains a lable name. Whenever goto is executed then the
control directly goes to that lable and executes respective statements.

Let us take an example program;

Output:

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 16

return:
The return statement is used to return (come out) from the called method. That
is, the execution control is transferred from called method to calling method (caller).

Let us take an example program;

Output:

B.Com (Comp. Applications) I Year, SEMESTER-II

You might also like