PSTC - MODULE -2
• Statements: Declarations, Compound statements, Decision Making
using conditional statements, Branching, simple-if, if-else, elseif
ladder, switch, break and continue. Iterative statements: While
construct, Do-while Construct, For construct, Nested Loops
TOPIC 1 : Decision Making using conditional statements (or) Contro
Structures
• Normally statements in C program are executed sequentially i.e. in the order in
which they are written. This is called sequential execution. We can transfer the
control point to a desired location is possible with control structures.
• C allows many kinds of control statements.
• [Link] (decision) control structures
• · if · if –else · if-elseif – else · nested if
• [Link] (branching) control structures
• · goto · break continue
• [Link] (iterative) control structures
• · for loop · while loop · do - while loop
• [Link] way conditional (case) control structures
• · switch- case
1. Decision Making Statements / Conditional Statements:
• Decision making is about deciding the order of execution of statements based
on certain conditions or repeat a group of statements until certain specified
conditions are met.
• These statements are called as 'Decision Making Statements' or 'Conditional
Statements.'
• Followings are the different conditional statements used in C.
I. If Statement
II. If-Else Statement
III. Else if ladder
IV. Nested If-Else Statement
I) If Statement
• This is a conditional statement used in C to check condition or to control the flow
of execution of statements. This is also called as 'decision making statement or
control statement.' The execution of a whole program is done in one direction
only.
• Syntax:
if(condition)//true
True Statements;
}
• Program :
Write a c program of simple if statements
check class note book ,skill and practical questions related if statements
programs
ii) If-Else Statement
• This is also one of the most useful conditional statements used in C to check
conditions.
Syntax:
if(condition)//true
True Statements;
}
• Program :
Write a c program of if else statements
check class note book ,skill and practical questions related if statements
programs
iii) else if ladder statement
• In this statement statements in loops gets executed when the corresponding conditions are true. Statements in final else
block gets executed when all other conditions are false.
• We can use any no. of else-if blocks between if and else.
• Syntax:
if (condition1) //True1 if false
{
statements
}
else if(condition2)//True2 if false
{
statements
}
else if(condition3)//True3 false then
{
statements
} … ….
else//False
{
Statements
• Program :
Write a c program of else if ladder statements
check class note book ,skill and practical questions related if statements
programs
IV) Nested If-Else Statement
• It is a conditional statement which is used when we want to check more than
1 condition at a time in a same program.
• The conditions are executed from top to bottom checking each condition
whether it meets the conditional criteria or not.
• If it found the condition is true then it executes the block of associated
statements of true part else it goes to next condition to execute.
• Syntax:
if(condition)//true1
{
if(condition)//true2
{
statements;
}
else//false2
{
statements;
}
}
Else//false1
{
statements;
}
}
• Program :
Write a c program of nested if statements
check class note book ,skill and practical questions related if statements
programs
TOPIC 2: Definite and indefinite Iterative statements
(or) Looping statements
• A loop statement allows us to execute a statement or group of statements
multiple times. 'A loop' is a part of code of a program which is executed
repeatedly. The repetition is done until condition becomes false.
• Types of looping statements :
• Basically, the type of looping statements depends on the condition checking
mode. Condition checking can be made in two ways as : Before loop and after
loop. So, there are 2(two) types of looping statements.
1. Entry controlled loop
2. Exit controlled loop
• 1. Entry controlled loop: In such type of loop, the test condition is checked first
before the loop is executed. Some common examples of these looping statements
are:
• while loop
• for loop
• 2. Exit controlled loop: In such type of loop, the loop is executed first. Then
condition is checked after block of statements are executed. The loop executed at
least one time compulsorily.
• Some common example of this looping statement is :
• do-while loop
• 1. While loop:
• This is an entry controlled looping statement. It is used to repeat a block of
statements until condition becomes true.
Write a c program to print 1 to 5 numbers using while loop statement
• 2. do-while loop :
• This is an exit controlled looping statement.
• Sometimes, there is need to execute a block of statements first then to check
condition. At that time do while such type of a loop is used. In this, block of
statements are executed first and then condition is checked.
Syntax:
do
{
statements;
….
}while(condition);
Write a c program to print 1 to 5 numbers using do while loop statement
• 3. for loop :
• This is an entry controlled looping statement. In this loop structure, more than
one variable can be initialized. One of the most important features of this loop
is that the three actions can be taken at a time like variable initialization,
condition checking and increment/decrement.
• The for loop can be more concise and flexible than that of while and do-while
loops.
Write a c program to print 1 to 5 numbers using for loop statement
• 4. Nested Loops:
• You can use one or more loops inside any other while, for,
or do...while loop. Looping in a loop is called Nesting of Loops.
Syntax:
for (loop)
{
while (loop)
{
Statements;
}
Statements;
}
1. Write a c program to demonstrate the nested for loop statement
# include <stdio.h>
int main()
{
int n, k, j ; output :
Enter any number 5
printf(“Enter any number “) ; 1
scanf(“%d”, &n) ; 12
123
for(k=1; k<=n; k++) 1234
{ 12345
printf(“\n “) ;
for(j=1; j<=k; j++)
printf(“%d “, j) ;
}
retrun 0;
}
TOPIC 3: C Jumping Statements
• [Link] Statement :
• Sometimes, it is necessary to exit immediately from a loop as soon as the
condition is satisfied.
• When break statement is used inside a loop, then it can cause to terminate
from a loop.
• The statements after break statement are skipped.
• Syntax :
break;
Write a c program to print 1 to 5 using break statement
• 2. Continue Statement :
• The working structure of 'continue' is similar as that of that break statement but
difference is that it cannot terminate the loop.
• It causes the loop to be continued with next iteration after skipping
statements in between. Continue statement simply skipps statements and
continues next iteration.
• Syntax : continue;
Write a c program to print 1 to 5 using continue statement
TOPIC :4
• Multi way conditional (case) control structures
· switch- case
This is a multiple or multi-way branching decision making statement. When
we use nested if-else statement to check more than 1 conditions then the
complexity of a program increases in case of a lot of conditions.
• Thus, the program is difficult to read and maintain. So to overcome this
problem, C provides 'switch case'.
• Switch case checks the value of a expression against a case values,
if condition matches the case values then the control is transferred to that
point.
Syntax:
switch(expression)//true
{
case expr1:
statements; break;
case expr2:
statements;
break;
………………
……………………
case exprn:
statements;
break;
default://false
statements; Write a c program to demonstrate the switch statement
}