Professional Documents
Culture Documents
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.
Such type of execution can be done with the help of the decision
making statements.
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
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.
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”)
bonus marks are added to his marks before they are printed. For
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
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”
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.
while(condition expression)
}
EXAMPLE:
i = 1 // initialization
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.
do
} while(condition);
EXAMPLE:
i = 1; // initialization
do
printf(“%d”,i);
i++; // update
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.
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
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