C Short notes-MODULEII-2
C Short notes-MODULEII-2
Control Structures
Control Statements are of two types. These statements alter the flow of execution of the programs.
1. Decision -making statements
A. if statement
if statement is executed when we require to execute or skip a statement depending on a
certain condition. The statements are executed only when test expression evaluates to
nonzero (true) value.
Syntax: if(test expression)
{
Statements;
}
Ex: int main( )
{
int i;
printf(“enter a number\n”);
scanf(“%d”,&i);
if(i%3==0)
{
printf(“The number is divisible by zero\n”);
}
}
B. if-else statement
The statement under if executes only if the test condition is true and the statement under
the else is executed when test condition is false.
Syntax: if (test expression) {
// statements to be executed if the test expression is true
}
else {
// statements to be executed if the test expression is false
}
C. switch statement
A switch statement allows the user to choose a statement(or a block of statements)
among several alternatives. The switch statement tests the value of a given variable
against a list of case values and when a match is found, a block of statements associated
with that case is executed.
Syntax: switch(expression)
{
case value_1:
block_1;
break;
case value_2:
block_2;
break;
default:
default_block;
break;
}
statement_x;
The expression is an integer expression or characters. The possible values of the expression value_1,
value_2 are constants or constant expressions and known as case labels. Each of these values should be
unique within the switch statement. block_1 , block_2 are set of statements to be executed when a match
is found for the test expression with the case values value_1, value_2. The break statement at the end of
each block causes exit from the switch statement, transfers control to statement_x. The default is an
optional case. It will be executed if the value of the expression does not match with any of the case values.
Ex:
………….
………….
int choice;
printf(“1.Mercury\n”);
printf(“2.Venus\n”);
printf(“Enter a number corresponding to your choice\n”);
scanf(“%i”, &choice);
switch(choice)
{
case1:
puts(“Mercury is closest to sun”);
break;
case2:
puts(“Venus is second planet from the sun”);
break;
default:
puts(“Your choice is incorrect\n”);
break;
}
Loop Constructs (Iteration statements in C)
Loops in C causes a ‘sequence of statements’ are executed until some conditions for termination of the
loop are satisfied. A program loop consists of two segments, body of the loop and the control statement.
The control statement tests certain conditions and then directs the repeated execution of the statements
contained in the body of the loop. There are three kinds of loops in C.
a)for
b)while
c)do-while
Depending on the position of the control statement in the loop, a control structure may be classified
either as the entry-controlled loop or as exit-controlled loop. In the entry-controlled loop, the control
conditions are tested before the start of the loop execution. If the conditions are not satisfied, then the
body of the loop will not be executed. In the case of the exit- controlled loop, the test is performed at the
end of the body of the loop and therefore the body is executed unconditionally for the first time.
for loop
The for loop is an entry-controlled loop that provides a more concise loop control structure. The general
form of the for loop is
for(initialization expression ;test-expression; update expression)
{
body of the loop
}
i)Initialization expression: The control variable is initialized. This part is executed once before executing
the body of the loop.
ii)Test-expression: The value of the control variable is tested, and if found true, the body of the loop
executed. otherwise the loop is terminated. This expression is evaluated before every execution of the loop
body.
iii)Update- expression: After executing the last statement in the for loop, the control is transferred back to
the for statement. The control variable is updated using an expression such as j++ and the test expression
is evaluated to see if it is true before executing the body of the loop.
Ex: int main( )
{
int i, sum=0;
for(i=1; i<=10; i++)
{
sum=sum+i;
printf(“%d”, sum);
}
return 0;
}
while loop
The while loop is an entry-controlled loop. The test condition is evaluated and if the test condition is true,
then the body of the loop is executed. The repeated execution of the body continues until the test-condition
finally becomes false and the control is transferred out of the loop.
Syntax: while(test-condition)
{
body of the loop
}
Example: int main( )
{
sum=0;
n=1;
while(n<=10)
{
sum=sum+n*n;
n=n+1;
}
printf(“%d”,sum);
return 0;
}
do….while loop
The do….while loop is an exit-controlled loop since the test-condition is evaluated at the bottom
of the loop . Hence, the body of the loop is executed at least once.
Syntax: do
{
body of the loop
}while(test-condition);
Ex:
…………
int number;
do
{
printf(“enter a number\n”);
scanf(“%d”, &number);
}while(number>0);
………………
This do…while loop reads a number from the keyboard until a zero or a negative number is keyed in.
Jumping statements in C
1. break statement
A break statement terminates the execution of the loop and the control is transferred to the statement
immediately following the loop.
Ex: A program to input and add the positive numbers . It exits the loop whenever the user enter a zero or
a negative number.
int main( )
{
int num, i;
while(1)
{
printf(“enter the number”);
scanf(“%d”,&num);
if(num<=0)
break;
sum=sum+num;
printf(“The sum of the positive numbers entered %d”,sum);
}
return 0;
}
2. continue statement
The continue statement is used to bypass the remainder of the current pass through a loop. The
loop does not terminate when a continue statement is encountered. Instead, the remaining loop
statements are skipped and the computational proceeds directly to the next pass through the loop.
Ex:
int main( )
{
int i=1, num, sum=0;
for (i=0; i<5; i++)
{
printf(“enter the integern”);
scanf(“%d”, &num);
if(num <0)
{
puts(“You have entered a negative number\n”);
continue;
}
sum=sum+num;
}
printf(“The sum of the positive integers entered= %d”, sum);
return 0;
}
3. goto statement
The goto statement is used to alter the normal sequence of the program execution by
unconditionally transferring control to some other part of the program. The goto statement is a
jump statement which is sometimes also referred to as unconditional jump statement.
#include <stdio.h>
int main()
{
int sum=0;
for(int i = 0; i<=10; i++){
sum = sum+i;
if(i==5){
goto addition;
}
}
addition:
printf("%d", sum);
return 0;
}
Output:15
In this example, we have a label addition and when the value of i (inside loop) is equal to 5 then
we are jumping to this label using goto statement. The sum is displayed till 5 even though the
loop is set to run from 0 to 10.
else
{
if(c>b)
printf("The greatest number is %d",c);
}
}
else
{
if(b>c)
printf("The greatest number is %d",b);
else
printf("The greatest number is %d",c);
}
return 0;
}
2.Nested loops
Syntax of nested loop
outer_loop
{
inner_loop
{
// Inner loop statement/s
}