Prog. With C & C++ (Unit-II) - 1
Prog. With C & C++ (Unit-II) - 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.
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.
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.
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)
{
………….
}
}
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
#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;
2. Looping Statements
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.
Output:
do…while loop:
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.
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
condition is false then comes out from the block and executes the statements outside
the block.
Output:
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
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.
continue:
The continue statement skips the current iteration of for, while and do…while
loops and continue the flow of execution.
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.
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.
Output:
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).
Output: