PPS UNIT II Control Statements
PPS UNIT II Control Statements
C CONTROLSTATEMENTS
Conditional Statements
Conditional Statements in C programming are used to make
decisions based on the conditions.
These statements are also known as decision making statements (or)
selection statements
There are the following conditional statement in C language.
1)If statement
2)If-else statement
3)Nested if statement
4) else-if ladder
5.Switch statement
1)If Statement:
The if statement is used to check some given condition and perform
some operations depending upon the correctness of that condition.
The single if statement in C language is used to execute the code if a
condition is true. It is also called one-way selection statement.
The syntax of the if statement is given below.
if(expression (or) condition)
{
Statement block;
} Entry
Statement x; False
Flowchart of if statement in C: condition
True
Statement -block
Statement -X
How "if" statement works..
• If the expression is evaluated to nonzero (true) then if block statement(s)
are executed.
• If the expression is evaluated to zero (false) then Control passes to the next
statement following it.
Ex:
#include <stdio.h>
int main ()
{
int a = 10;
if( a < 20 )
{
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
2)If -else statement:
The if-else statement in C language is used to execute the code if condition is
true or false. It is also called two-way selection statement.
An if statement can be followed by an optional else statement, which executes
when the Boolean expression is false.
Syntax
The syntax of an if...else statement in C programming language is −
if(boolean_expression)
{
true block statements:
//statement(s) will execute if the boolean expression is true
}
else
{
false block statements;
//statement(s) will execute if the boolean expression is false
}
Statement –x;
conditio
n
Statement x
How "if..else" statement works:
If the expression is evaluated to nonzero (true) then if block statement(s) are
executed.
If the expression is evaluated to zero (false) then else block statement(s) are executed.
Ex:
#include<stdio.h>
void main()
{
int n;
printf("enter the number");
scanf("%d“,&n);
if(n%2==0)
{
printf("%d is an even integer.",n);
}
else
{
printf("%d is an odd number ",n);
}
}
3)Nested if statement:
The nested if...else statement is used when a program requires more than one
test expression. It is also called a multi-way selection statement.
Syntax: if( condition 1 )
{
if( condition2)
{
statement-block1;
}
else
{
statement-block 2;
}
}
else
{
statement-block 3;
}
#include<stdio.h>
void main( )
{
int a,b,c;
printf("Please Enter three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("a is greatest");
}
else
{
printf("c is greatest");
}
}
else
{
if(b>c)
{
printf("b is greatest");
}
else
{
printf("c is greatest"); }
}
4.else- If ladder:
The if-else-if statement is used to execute one code from multiple conditions. It is also
called multipath decision statement. It is a chain of if..else statements in which
each if statement is associated with else if statement and last would be an else
statement.
• Syntax
if(condition1)
{
//statement1;
}
else if(condition2)
{
//statement2 ;
}
else if(condition3)
{
//statement3 ;
}
else
{ //statement4 ;}
#include<stdio.h>
int main()
{
int marks=83;
if(marks>75)
{
printf("First class");
}
Else if(marks>65)
{
printf("Second class");
}
else if(marks>55)
{
printf("Third class");
}
else
{
printf("Fourth class");
}}
5.Switch statement:
The switch statement is a multiway branch statement. It provides an easy way to dispatch
execution to different parts of code based on the value of the expression.
Switch statement in C tests the value of a variable and compares it with multiple cases. Once
the case match is found, a block of statements associated with that particular case is
executed.
Syntax:
switch( expression(or) variable)
{
case value-1:
statement Block-1;
break;
case value-2:
statement Block-2;
break;
.
.
default:
default statements;
}
How does the switch statement work?
1.The switch expression must be of an integer or character
type.
2.The case value must be an integer or character constant.
3.Case labels always end with a colon ( : ). Each of these
cases is associated with a block.
4.If there is a match, the corresponding statements after the
matching label are executed.
5.If there is no match, the default statements are executed.
6.If we do not use break, all statements after the matching
label are executed.
7.By the way, the default clause inside the switch statement
is optional.
// Program to create a simple calculator
#include <stdio.h>
int main()
{
char operator;
int n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%d %d",&n1, &n2);
switch(operator)
{
case '+':
printf("%d + %d = %d",n1, n2, n1+n2);
break;
case '-':
printf("%d- %d= %d",n1, n2, n1-n2);
break;
case '*':
printf("%d* %d = %d",n1, n2, n1*n2);
break;
case '/':
printf("%d/ %d = %d",n1, n2, n1/n2);
break;
// operator doesn't match any case constant +, -, *, /
default:
printf("Error! operator is not correct");
}
return 0;
}
LOOPS
In computer programming, a loop is a sequence of instructions that is
repeated until a certain condition is reached.
(OR)
The looping can be defined as repeating the same process multiple
times until a specific condition satisfies.
Why use loops in C language?
The looping simplifies the complex problems into the easy ones. It
enables us to alter the flow of the program so that instead of writing
the same code again and again, we can repeat the same code for a
finite number of times.
Advantage of loops in C:
• It provides code reusability.
• Using loops, we do not need to write the same code again and again.
• Using loops, we can traverse over the elements of data structures
(array or linked lists).
Types of C Loops:
There are three types of loops in C language
1.For loop
2.While loop
3.do while loop
There are mainly two types of loops:
• Entry Controlled loops: In this type of loops the test
condition is tested before entering the loop body. For
Loop and While Loop are entry controlled loops.
• Exit Controlled Loops: In this type of loops the test
condition is tested or evaluated at the end of loop body.
Therefore, the loop body will execute atleast once,
irrespective of whether the test condition is true or
false. do – while loop is exit controlled loop.
1.For loop:
The for loop is used in the case where we need to execute some
part of the code until the given condition is satisfied. The for
loop is also called as a pre-tested loop.