You are on page 1of 31

DAY – 3

BASICS OF C
What will you learn?
 Control Statements
 Selection Statements
 Looping Statements
 Nested Loops
 Control Transfer Statements
WHY CONTROL STRUCTURES ARE ?
 C program code is executed sequentially from the first line to the
last line without ignoring any of the statements in the program.

 But sometimes, it is required to execute only selected statements


or to execute the same block of code multiple times.

 Such type of execution can be done with the help of the decision
making statements.

 Therefore, to alter the sequential flow of execution, decision


making statements are used.
CONTROL STATEMENTS
Control Statements are Classified as:

1. Selection Statements
2. Looping Statements
SELECTION STATEMENTS
SELECTION - MAKING DECISIONS
‘C’ language supports decision making using the following
statements known as control or decision making statements. They are
listed as follows:

1. if statement

2. if – else statement

3. else if ladder statement

4. Switch statement
IF STATEMENT
IF statement is powerful decision making statement and is used to control the flow of execution of
statements. IF statement is used when we execute a single or set of statements based on some
conditional expression.

SIMPLE IF STATEMENT (NULL ELSE STATEMENT):


The general form of simple if statement is
if (conditional expression)
{
Statement block;
}
Statement - x ;

The statement block may be a single statement or a group of statements. If the conditional
expression is true then the statement block will be executed. Otherwise the statement block will be
skipped and the execution will jump to the statement that follows the if statement
EXAMPLE:
if(category == “sports” OR category=“NSS” OR category=“NCC”)

marks = marks + bonus marks;

If the student belongs to the sports category then additional

bonus marks are added to his marks before they are printed. For

others bonus marks are not added.


IF – ELSE STATEMENT
The if - else statement is an extension of the simple if statement. It is a composite

statement used to make decision between two alternatives. The general form is

if (test expression)

true-block statements;

else

false-block statements;

statement – x;
EXAMPLE:
If (age>=18)

printf(“”Major”);

Else

printf(“Minor”);

st-x;

 Here if the code is equal to ‘1’ the statement boy=boy+1; is executed and
the control is transferred to the statement st-x, after skipping the else part.

 If code is not equal to ‘1’ the statement boy =boy+1; is skipped and the
statement in the else part girl =girl+1; is executed before the control
reaches the statement st-x.
MULTI WAY SELECTION –ELSE IF LADDER
A multi path decision is chain of if’s in which the statement associated with each else is an

if. This statement is used when it is required to make a selection among multiple

alternatives. It takes the following general form.

if (condition1)

Statement –1;

else if (condition2)

Statement –2;

else if (condition 3)

Statement –3;

…………………

else

default – statement;

Statement–x;
EXAMPLE:
if (code = = 1)
Color = “red”;
else if ( code = = 2)
Color = “green”;
else if (code = = 3)
Color = “white”;
else
Color = “yellow”

If code number is other than 1, 2 and 3 then color is yellow.


MULTI WAY SELECTION – SWITCH
Instead of else – if ladder, ‘C’ has a built-in multi-way decision statement known
as a switch. The general form of the switch statement is as follows.
switch (expression)
{
case label1: block1;
break;
case label2: block 2;
break;
…………….
…………….
default: default block;
break;
}
statement – x;
EXAMPLE:
switch (number)
{
case 1 : printf(“Monday”);
break;
case 2 : printf(“Tuesday”);
break;
case 3 : printf(“Wednesday”);
break;
case 4 : printf(“Thursday”);
break;
case 5 : printf(“Friday”);
break;
default: printf(“Holiday”);
break;
}
LOOPING STATEMENTS
REPETITION OR LOOPING STATEMENTS
Looping Statements are meant for the purpose of execution a single or
a block of statements repeatedly as many times as required.

In C, there are three loop statements:

1. while

2. do-while

3. for
WHILE STATEMENT
This type of loop is also called an entry controlled loop construct. It is
also called as pretest loop. As it is pretest loop, it will test the expression before
every iteration of the loop. The expression is executed and if is true then the
body of the loop is executed this process is repeated until the Boolean expression
becomes false. Ones it becomes false the control is a transferred out of the loop.

The general form of the while statement is

while(condition expression)

Body of the loop

}
EXAMPLE:
i = 1 // initialization

while (i<=5) // condition

printf(“%d”,i);

i++; // update

}
DO-WHILE STATEMENT
This type of loop is also called an exit controlled loop statement.
i.e., the Boolean expression evaluated at the bottom of the loop and if it
is true then body of the loop is executed again and again until the
Boolean expression becomes false. Once it becomes false the control is
transferred out of the loop executing the next statement.

The general form of do-while statement is

do

body of the loop

} while(condition);
EXAMPLE:
i = 1; // initialization

do

printf(“%d”,i);

i++; // update

} while (i<=5); // condition


FOR STATEMENT
The for loop is another entry controlled loop that provides a more concise loop
control structure the general form of the for loop is:
for(i=1;i<=5;i++)
{
printf(“%d”, i);
}
 Here initialization is used to initialize some variable that controls the condition of the
loop. If that condition is true the body of the loop is executed, otherwise the loop is
terminated.
 Once the body of the loop is executed then the loop control variable is updated and
again tested with the loop condition.
 If the condition is satisfied the body of the loop is again executed.
 This process continues until the value of the control variable becomes false.
NESTED LOOPS
There are several situations where we might have one loop
to be contained in another loop. Such loops are called nested loops.
If one for loop contains another for loop then it is called nested for
loop.
EXAMPLE
for(i=1;i<=5;i++)
{
printf(“%d”,i);
for(j=1;j<=4;j++)
{
printf(“%d”,j);
}
}
EXAMPLE:

for( i = 1 ; i <=5; i ++)

printf(“%d”,i);

}
CONTROL TRANSFER
STATEMENTS
BREAK STATEMENT
 The break statement can be used to exit the loop forcefully even
though loop condition is true.

 When break is encountered inside a loop, the loop is immediately


exited and the program continues with the statement which is
followed by the loop.

 If loops are nested then the break statement terminates the inner
loop and the control transfers to the outer loop.
EXAMPLE:
for (i=1; i<=5; i++)
{
if( i == 4)
break;
printf(“%d”, i);
}
CONTINUE STATEMENT

The continue statement is used to terminate the current


iteration and continue the execution of the loop with the
next iteration.
EXAMPLE:

for (i=1; i<=5; i++)

if( i == 3)

continue;

printf(“%d”,i);

}
PROBLEMS
1. Write a program to generate right Triangle using for statement
2. Write a C Program to find the Fibonacci series for given number.
3. Write a C Program to check whether the given number is
palindrome or not.
4. Write a C Program to check whether the given number is Strong
or not.
5. Write a C Program to generate prime numbers between 1 to n
6. Write a C Program to add digits and multiplication of N
numbers
7. Write a C Program to print sum of digits of a number
8. Program to build a Calculator
9. Program to find smallest and largest in a set of N numbers
10.Program to calculate Mn

You might also like