SlideShare a Scribd company logo
Subject: Programming In C Language
C. P. Divate
CONTROL STATEMENTS
C language supports the following statements known as
control or decision making statements.
1. if statement
2. if…..else statement
3. Nesting of if…..else statement
4. if ……else ladder
5. switchstatement
6. conditional operatorstatement
7. Go tostatement
IF( ) STATEMENT
 if statement is the most simple decision making statement.
 It is used to decide whether a certain statement or block of
statements will be executed or not.
 i.e if a certain condition is true then a block of statement is
executed otherwise not.
 Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
 Example:
if(bank balance is zero)
Borrow money
IF( ) STATEMENT
 Here, condition after evaluation
will result either true or false.
 if statement condition results in
boolean values.
 if the value is true then it will
execute the block of statements
below it otherwise not.
 If we do not provide the curly
braces ‘{‘ and ‘}’ after
if(condition) then by default if
statement will consider the first
immediately below statement to
be inside its block.
IF( ) STATEMENT Example
// C program to illustrate If ()
#include <stdio.h>
int main()
{
int i = 10;
if (i > 15)
{
printf("10 is less than 15");
}
printf("I am Not in if");
}
Output:
I am Not in if
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
}
Output1:
Variable x is less than y
IF( ) STATEMENT Example
Output1:
Enter a number:4
4 is positive number
// C program to check that no is positive
#include<stdio.h>
int main()
{
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number >= 0)
{
printf("%d is positive number",number);
}
}
Output1:
Enter a number: -10
IF( ) STATEMENT Example
// C program to check that no is even
#include<stdio.h>
int main()
{
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
}
Output1:
Enter a number:4
4 is even number
Output2:
Enter a number:5
IF( ) STATEMENT Example
// C program to check that no is divisible by 7
#include<stdio.h>
int main()
{
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number % 7==0)
{
printf("%d is divisible by 7",number);
}
}
Output1:
Enter a number:49
49 is divisible by 7
Output2:
Enter a number:55
IF( ) STATEMENT Example
// C to calculate fine in library
#include<stdio.h>
int main()
{
int days;
float fine=0;
printf("Enter no of days:");
scanf("%d",&days);
if(days >=8)
{
fine=(days-8) * 2;
}
printf(“Fine = %f for Total days=%d”, fine, (days-8));
}
Output1:
Enter no of days:10
Fine = 4 for Total days=2
Output1:
Enter no of days:7
Fine = 0 for Total days= -1
Multiple IF( ) STATEMENT Example
// C Example of multiple if statements
#include <stdio.h>
int main()
{
int x, y;
printf("enter the value of x:");
scanf("%d", &x);
printf("enter the value of y:");
scanf("%d", &y);
if (x>y)
{
printf("x is greater than yn");
}
if (x<y)
{
printf("x is less than yn");
}
Output1:
enter the value of x:30
enter the value of y:20
x is greater than y
End of Program
Output2:
enter the value of x:20
enter the value of y:20
x is equal to y
End of Program
if (x==y)
{
printf("x is equal to yn");
}
printf("End of Program");
return 0;
}
IF( ) STATEMENT Home work Example
 Write a C program to check income is greater than 30000
 Write a C program to calculate income_tax if income is greater than 30000
(formula income_tax=30 % of income)
 Calculate result of student based on following conditions.
if percentage < 35 then fail
if percentage >= 35 then pass
IF( ) … else ….STATEMENT
 if statement is the decision making statement.
 An if statement can be followed by an optional else statement, which
executes when the Boolean expression is false.
 i.e if a certain condition is true then a block of statement is executed
otherwise else block statement is executed.
 Syntax:
if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
 Here, condition after evaluation
will result either true or false.
 if statement condition results in
boolean values.
 if the value is true then it will
execute the true block of if
statements below it otherwise it
will execute the false block(else
block of if statements .
IF( ) … else ….STATEMENT
IF( ) … else ….STATEMENT Example
IF( ) … else ….STATEMENT Example
// C program to check given number is odd or even number
#include<stdio.h>
int main()
{
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
else
{
printf("%d is odd number", number);
}
printf(“End of Program");
}
Output1:
Enter a number:4
4 is even number
Output2:
Enter a number:11
11 is odd number
IF( ) … else ….STATEMENT Example
// C Program to check whether a person is eligible to vote or not.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}
Output1:
Enter your age?18
You are eligible to vote...
Output2:
Enter your age?13
Sorry ... you can't vote
IF( ) … else ….STATEMENT Example
// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: “); scanf(“%d”, &number);
if (number >= 0)
{
printf("You entered a positive integer: %d”, number );
}
else
{
printf(" "You entered a negative integer : %d”, number );
}
cout << "This line is always printed.";
}
Output1:
Enter an integer: 4
You entered a positive integer: 4.
This line is always printed.
Output2:
Enter an integer: - 4
You entered a negative integer: -4.
This line is always printed.
LOGICAL OPERATORS IN C
LOGICAL OPERATORS IN C
Note: a and b are both relational expression here
NESTING OF IF…..ELSE STATEMENTS
⚫When a series of decisionsare involved, we may have to
use more than one if….else statements, in nested form
as follows
Here, if the condition 1 is false then it
skipped tostatement 3.
But if the condition 1 is true, then it
testscondition 2.

If condition 2 is true then it executes
statement 1 and if false then it executes
statement 2.
 Then the control is transferred to the
statement x.
This can also be shown by the following
flowchart,
NESTING OF IF…..ELSE STATEMENTS
Program
/*Selecting the largestof threevalues*/
main()
{
float A, B, C;
printf(“Enter three values n”);
scanf(“|%f %f %f”,&A, &B, &C);
printf(“nLargest value is:”);
if(A > B)
{
if(A > C)
printf(“%f n”,A);
else
printf(“%f n”,C);
}
else
{
if(C > B)
printf(“%f n”,C);
else
printf(“%f n”,B);
}
}
OUTPUT
Enter three values: 5 8 24
Largest value is 24
 This construct is known as the else if ladder. The conditions are
evaluated from the top, downwards. This can be shown by the
following flowchart
The else if ladder
The else if ladder
 When a multipathdecision is involved then we use else if ladder.
 A multipath decision is a chain of ifs in which the statement associated
witheach else is an if.
 Ittakes the following general form,
 This construct is known as the else if ladder. The conditions are
evaluated from the top, downwards. This can be shown by the
following flowchart
The else if ladder
THE SWITCH STATEMENT
⚫Switch statement is used for complex programs when
the numberof alternatives increases.
⚫The switch statement tests the value of the given
variable against the list of case values and when a
match is found, a block of statements associated with
thatcase is executed.
Switch case flowchart
SWITCH STATEMENT
⚫ Thegeneral form of switch statement is
switch(expression)
{
casevalue-1:
block-1 break;
casevalue-2:
block-2 break;
…….
…….
default: default-block break;
}
statement-x;
Example:
index = marks / 10;
switch(index)
{
case 10:
case 9:
case 8:
grade = “Honours”; break;
case 7:
case 6:
grade = “firstdivision”; break;
case 5:
grade = “second
division”;
break;
case 4:
grade = “third division”;
break;
default:
grade = “first division”; break
}
printf(“%s n”,grade);
………….
THE ?: OPERATOR
⚫ The C language has an unusual operator, useful for making two-
waydecisions.
⚫ This operator is a combination of ? and : and takes three
operands.
⚫ It isof the form exp1?exp2:exp 3
⚫ Here exp1 is evaluated first. If it is true then the expression exp2 is
evaluated and becomes thevalue of theexpression.
⚫ If exp1 is false then exp3 is evaluated and its value becomes the
valueof theexpression.
Eg:
if(x < 0)
flag = 0;
else
flag = 1;
can be written as
flag = (x < 0)? 0 : 1;
UNCONDITIONAL STATEMENTS - THE GOTO STATEMENT
C supports the goto statement to
branchunconditionally from one point of the
program toanother.
The goto requires a label in order to identify
the place where the branch is to be made.
A label is any valid variable name and
must be followed byacolon.
DECISION MAKING AND LOOPING
 In looping, a sequence of statements are executed until some
conditions for terminationof the loopare satisfied.
 In looping, a sequence of statements are executed until some
conditions for terminationof the loopare satisfied.
 A program loop therefore consists of two segments, one known
as the body of the loop and the other known as the control
statements.
Cont..
Depending on the position of the control statements in
the loop, a control structure may be classified either as an
entry-controlled loop or as the exit-controlled loop.
Loops In C
⚫The C language provides for three loop constructs for
performing loopoperations.
⚫Theyare:
The whilestatement
The dostatement
The forstatement
THE WHILE STATEMENT
⚫The basic formatof the while statement is
while(testcondition)
{
bodyof the loop
}
⚫ The while isan entry–controlled loop statement.
⚫ The test-condition is evaluated and if the condition is true, then the
bodyof the loop is executed.
⚫ After execution of the body, the test-condition is once again evaluated and if
it is true, the body is executed onceagain.
⚫ This process of repeated execution of the body continues until the test-
condition finally becomes false and the control is transferred out of the loop.
Example of WHILE Loop
Bodyof the loop test
condn?
while(testcondition)
{
bodyof the loop
}
sum = 0;
n = 1;
while(n <= 10)
{
sum = sum + n* n;
n = n + 1;
}
printf(“sum = %d n”,sum);
While loop
THE DO STATEMENT
⚫In while loop the body of the loop may not be executed at
all if the condition is not satisfied at the very first attempt.
⚫Such situations can be handled with the help of the do
statement.
do
{
body of the loop
}
while(test condition);
Cont..
⚫Since the test-condition is evaluated at the bottom of the loop,
the do…..while construct provides an exit-controlled loop and
therefore the body of the loop is always executed at least once.
Eg:
do
{
printf(“Inputa numbern”);
number= getnum();
}
while(number> 0);
Difference between While( ) loop and Do….while() loop
THE FOR STATEMENT
⚫The for loop is another entry-controlled loop that
provides a more concise loop control structure
⚫Thegeneral form of the for loop is
for(initialization ; test-condition ; increment/Decrement)
{
body of the loop
}
THE FOR STATEMENT
for(initialization ; test-condition ; increment/Decrement)
{
body of the loop
}
Cont..
⚫ The execution of the forstatement isas follows:
 Initialization of the control variables is done first.
 Thevalue of thecontrol variable is tested using the test-condition.
 If the condition is true, the body of the loop is executed; otherwise
the loop is terminated and the execution continues with the
statementthat immediately follows the loop.
 When the body of the loop is executed, the control is transferred
back to the for statement after evaluating the last statement in
the loop.
 Now, the control variable is either incremented or decremented as
perthecondition.
Cont..
⚫Eg 2)
sum = 0;
for(i = 1; i < 20; ++i)
{
sum =sum + i;
printf(“%d %d n”,sum);
}
for(initialization ; test-condition ; increment/Devrement)
{
bodyof the loop
}
For Statement
⚫Eg 1)
for(x = 0; x <= 9; x = x + 1)
{
printf)”%d”
,x);
}
printf(“n”);
⚫ The multiplearguments in the incrementsectionare possible
and separated by commas.
Nesting of For Loops
⚫C allowsone forstatementwithin anotherfor
statement.
Cont..
⚫ Eg:
⚫
⚫
⚫ for(row = 1; row <= ROWMAX; ++row)
⚫ {
⚫ for(column = 1; column < = COLMAX; ++column)
⚫ {
⚫ y = row * column;
⚫ printf(“%4d”
, y);
⚫ }
⚫ printf(“n”);
⚫ }
⚫
JUMPS IN LOOPS
 C permits a jump from one statement to another within a
loop as well as the jumpoutof a loop.
Jumping outof a Loop
 An early exit from a loop can be accomplished by using the
break statementor thegoto statement.
 When the break statement is encountered inside a loop, the
loop is immediately exited and the program continues with
thestatement immediately following the loop.
 When the loops are nested, the break would only exit from
the loop containing it. That is, the break will exit only a
single loop.
Skipping a part of a Loop
⚫Like the break statement, C supports another similar
statementcalled thecontinue statement.
⚫However, unlike the break which causes the loop to be
terminated, the continue, as the name implies, causes the
loop to be continued with the next iteration after skipping
any statements in between.
⚫The continue statement tells the compiler,
FOLLOWING STATEMENTS AND CONTINUE WITH
ITERATION”.
⚫The formatof thecontinue statement is simply
“SKIP THE
THE NEXT
continue;
Bypassing and continuing I Loops
References
1. http://www.computer-books.us/c_0008.php
2. http://www.computer-books.us/c_0009
3. http://www.computer-books.us/c_2.php
4. www.tutorialspoint.com/cprogramming/cprogramming_pdf.
5. Programming in C by yashwant kanitkar
6. ANSI C by E.balagurusamy- TMG publication
7. Computer programming and Utilization by sanjay shah Mahajan Publication
8. www.cprogramming.com/books.html

More Related Content

Similar to Basics of Control Statement in C Languages

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdf
mounikanarra3
 
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiM2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
rakshithatan
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
thuhiendtk4
 

Similar to Basics of Control Statement in C Languages (20)

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
C PROGRAMMING p-3.pptx
C PROGRAMMING p-3.pptxC PROGRAMMING p-3.pptx
C PROGRAMMING p-3.pptx
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdf
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiM2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
What is c
What is cWhat is c
What is c
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 

More from ChandrakantDivate1

More from ChandrakantDivate1 (20)

Web Technology LAB MANUAL for Undergraduate Programs
Web Technology  LAB MANUAL for Undergraduate ProgramsWeb Technology  LAB MANUAL for Undergraduate Programs
Web Technology LAB MANUAL for Undergraduate Programs
 
UNIVERSAL HUMAN VALUES- Harmony in the Nature
UNIVERSAL HUMAN VALUES- Harmony in the NatureUNIVERSAL HUMAN VALUES- Harmony in the Nature
UNIVERSAL HUMAN VALUES- Harmony in the Nature
 
Study of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block DiagramStudy of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block Diagram
 
Computer System Output Devices Peripherals
Computer System Output  Devices PeripheralsComputer System Output  Devices Peripherals
Computer System Output Devices Peripherals
 
Computer system Input Devices Peripherals
Computer system Input  Devices PeripheralsComputer system Input  Devices Peripherals
Computer system Input Devices Peripherals
 
Computer system Input and Output Devices
Computer system Input and Output DevicesComputer system Input and Output Devices
Computer system Input and Output Devices
 
Introduction to COMPUTER’S MEMORY RAM and ROM
Introduction to COMPUTER’S MEMORY RAM and ROMIntroduction to COMPUTER’S MEMORY RAM and ROM
Introduction to COMPUTER’S MEMORY RAM and ROM
 
Introduction to Computer Hardware Systems
Introduction to Computer Hardware SystemsIntroduction to Computer Hardware Systems
Introduction to Computer Hardware Systems
 
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2
 
Fundamentals of Internet of Things (IoT)
Fundamentals of Internet of Things (IoT)Fundamentals of Internet of Things (IoT)
Fundamentals of Internet of Things (IoT)
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Fundamentals of Structure in C Programming
Fundamentals of Structure in C ProgrammingFundamentals of Structure in C Programming
Fundamentals of Structure in C Programming
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
 
Programming in C - Fundamental Study of Strings
Programming in C - Fundamental Study of  StringsProgramming in C - Fundamental Study of  Strings
Programming in C - Fundamental Study of Strings
 
Features and Fundamentals of C Language for Beginners
Features and Fundamentals of C Language for BeginnersFeatures and Fundamentals of C Language for Beginners
Features and Fundamentals of C Language for Beginners
 
Basics of Programming Algorithms and Flowchart
Basics of Programming Algorithms and FlowchartBasics of Programming Algorithms and Flowchart
Basics of Programming Algorithms and Flowchart
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Computer Graphics Three-Dimensional Geometric Transformations
Computer Graphics Three-Dimensional Geometric TransformationsComputer Graphics Three-Dimensional Geometric Transformations
Computer Graphics Three-Dimensional Geometric Transformations
 
Computer Graphics - Windowing and Clipping
Computer Graphics - Windowing and ClippingComputer Graphics - Windowing and Clipping
Computer Graphics - Windowing and Clipping
 

Recently uploaded

Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
Kamal Acharya
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
Kamal Acharya
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 

Recently uploaded (20)

Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
 
Pharmacy management system project report..pdf
Pharmacy management system project report..pdfPharmacy management system project report..pdf
Pharmacy management system project report..pdf
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientist
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdfONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 

Basics of Control Statement in C Languages

  • 1. Subject: Programming In C Language C. P. Divate
  • 2. CONTROL STATEMENTS C language supports the following statements known as control or decision making statements. 1. if statement 2. if…..else statement 3. Nesting of if…..else statement 4. if ……else ladder 5. switchstatement 6. conditional operatorstatement 7. Go tostatement
  • 3. IF( ) STATEMENT  if statement is the most simple decision making statement.  It is used to decide whether a certain statement or block of statements will be executed or not.  i.e if a certain condition is true then a block of statement is executed otherwise not.  Syntax: if(condition) { // Statements to execute if // condition is true }  Example: if(bank balance is zero) Borrow money
  • 4. IF( ) STATEMENT  Here, condition after evaluation will result either true or false.  if statement condition results in boolean values.  if the value is true then it will execute the block of statements below it otherwise not.  If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if statement will consider the first immediately below statement to be inside its block.
  • 5. IF( ) STATEMENT Example // C program to illustrate If () #include <stdio.h> int main() { int i = 10; if (i > 15) { printf("10 is less than 15"); } printf("I am Not in if"); } Output: I am Not in if #include <stdio.h> int main() { int x = 20; int y = 22; if (x<y) { printf("Variable x is less than y"); } } Output1: Variable x is less than y
  • 6. IF( ) STATEMENT Example Output1: Enter a number:4 4 is positive number // C program to check that no is positive #include<stdio.h> int main() { int number=0; printf("Enter a number:"); scanf("%d",&number); if(number >= 0) { printf("%d is positive number",number); } } Output1: Enter a number: -10
  • 7. IF( ) STATEMENT Example // C program to check that no is even #include<stdio.h> int main() { int number=0; printf("Enter a number:"); scanf("%d",&number); if(number%2==0) { printf("%d is even number",number); } } Output1: Enter a number:4 4 is even number Output2: Enter a number:5
  • 8. IF( ) STATEMENT Example // C program to check that no is divisible by 7 #include<stdio.h> int main() { int number=0; printf("Enter a number:"); scanf("%d",&number); if(number % 7==0) { printf("%d is divisible by 7",number); } } Output1: Enter a number:49 49 is divisible by 7 Output2: Enter a number:55
  • 9. IF( ) STATEMENT Example // C to calculate fine in library #include<stdio.h> int main() { int days; float fine=0; printf("Enter no of days:"); scanf("%d",&days); if(days >=8) { fine=(days-8) * 2; } printf(“Fine = %f for Total days=%d”, fine, (days-8)); } Output1: Enter no of days:10 Fine = 4 for Total days=2 Output1: Enter no of days:7 Fine = 0 for Total days= -1
  • 10. Multiple IF( ) STATEMENT Example // C Example of multiple if statements #include <stdio.h> int main() { int x, y; printf("enter the value of x:"); scanf("%d", &x); printf("enter the value of y:"); scanf("%d", &y); if (x>y) { printf("x is greater than yn"); } if (x<y) { printf("x is less than yn"); } Output1: enter the value of x:30 enter the value of y:20 x is greater than y End of Program Output2: enter the value of x:20 enter the value of y:20 x is equal to y End of Program if (x==y) { printf("x is equal to yn"); } printf("End of Program"); return 0; }
  • 11. IF( ) STATEMENT Home work Example  Write a C program to check income is greater than 30000  Write a C program to calculate income_tax if income is greater than 30000 (formula income_tax=30 % of income)  Calculate result of student based on following conditions. if percentage < 35 then fail if percentage >= 35 then pass
  • 12. IF( ) … else ….STATEMENT  if statement is the decision making statement.  An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.  i.e if a certain condition is true then a block of statement is executed otherwise else block statement is executed.  Syntax: if (condition) { // block of code if condition is true } else { // block of code if condition is false }
  • 13.  Here, condition after evaluation will result either true or false.  if statement condition results in boolean values.  if the value is true then it will execute the true block of if statements below it otherwise it will execute the false block(else block of if statements . IF( ) … else ….STATEMENT
  • 14. IF( ) … else ….STATEMENT Example
  • 15. IF( ) … else ….STATEMENT Example // C program to check given number is odd or even number #include<stdio.h> int main() { int number=0; printf("enter a number:"); scanf("%d",&number); if(number%2==0) { printf("%d is even number",number); } else { printf("%d is odd number", number); } printf(“End of Program"); } Output1: Enter a number:4 4 is even number Output2: Enter a number:11 11 is odd number
  • 16. IF( ) … else ….STATEMENT Example // C Program to check whether a person is eligible to vote or not. #include <stdio.h> int main() { int age; printf("Enter your age?"); scanf("%d",&age); if(age>=18) { printf("You are eligible to vote..."); } else { printf("Sorry ... you can't vote"); } } Output1: Enter your age?18 You are eligible to vote... Output2: Enter your age?13 Sorry ... you can't vote
  • 17. IF( ) … else ….STATEMENT Example // Program to check whether an integer is positive or negative // This program considers 0 as a positive number #include <stdio.h> int main() { int number; printf("Enter an integer: “); scanf(“%d”, &number); if (number >= 0) { printf("You entered a positive integer: %d”, number ); } else { printf(" "You entered a negative integer : %d”, number ); } cout << "This line is always printed."; } Output1: Enter an integer: 4 You entered a positive integer: 4. This line is always printed. Output2: Enter an integer: - 4 You entered a negative integer: -4. This line is always printed.
  • 19. LOGICAL OPERATORS IN C Note: a and b are both relational expression here
  • 20. NESTING OF IF…..ELSE STATEMENTS ⚫When a series of decisionsare involved, we may have to use more than one if….else statements, in nested form as follows
  • 21. Here, if the condition 1 is false then it skipped tostatement 3. But if the condition 1 is true, then it testscondition 2.  If condition 2 is true then it executes statement 1 and if false then it executes statement 2.  Then the control is transferred to the statement x. This can also be shown by the following flowchart, NESTING OF IF…..ELSE STATEMENTS
  • 22. Program /*Selecting the largestof threevalues*/ main() { float A, B, C; printf(“Enter three values n”); scanf(“|%f %f %f”,&A, &B, &C); printf(“nLargest value is:”); if(A > B) { if(A > C) printf(“%f n”,A); else printf(“%f n”,C); } else { if(C > B) printf(“%f n”,C); else printf(“%f n”,B); } } OUTPUT Enter three values: 5 8 24 Largest value is 24
  • 23.  This construct is known as the else if ladder. The conditions are evaluated from the top, downwards. This can be shown by the following flowchart The else if ladder
  • 24. The else if ladder  When a multipathdecision is involved then we use else if ladder.  A multipath decision is a chain of ifs in which the statement associated witheach else is an if.  Ittakes the following general form,
  • 25.  This construct is known as the else if ladder. The conditions are evaluated from the top, downwards. This can be shown by the following flowchart The else if ladder
  • 26. THE SWITCH STATEMENT ⚫Switch statement is used for complex programs when the numberof alternatives increases. ⚫The switch statement tests the value of the given variable against the list of case values and when a match is found, a block of statements associated with thatcase is executed.
  • 28. SWITCH STATEMENT ⚫ Thegeneral form of switch statement is switch(expression) { casevalue-1: block-1 break; casevalue-2: block-2 break; ……. ……. default: default-block break; } statement-x;
  • 29. Example: index = marks / 10; switch(index) { case 10: case 9: case 8: grade = “Honours”; break; case 7: case 6: grade = “firstdivision”; break; case 5: grade = “second division”; break; case 4: grade = “third division”; break; default: grade = “first division”; break } printf(“%s n”,grade); ………….
  • 30. THE ?: OPERATOR ⚫ The C language has an unusual operator, useful for making two- waydecisions. ⚫ This operator is a combination of ? and : and takes three operands. ⚫ It isof the form exp1?exp2:exp 3 ⚫ Here exp1 is evaluated first. If it is true then the expression exp2 is evaluated and becomes thevalue of theexpression. ⚫ If exp1 is false then exp3 is evaluated and its value becomes the valueof theexpression. Eg: if(x < 0) flag = 0; else flag = 1; can be written as flag = (x < 0)? 0 : 1;
  • 31. UNCONDITIONAL STATEMENTS - THE GOTO STATEMENT C supports the goto statement to branchunconditionally from one point of the program toanother. The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name and must be followed byacolon.
  • 32. DECISION MAKING AND LOOPING  In looping, a sequence of statements are executed until some conditions for terminationof the loopare satisfied.  In looping, a sequence of statements are executed until some conditions for terminationof the loopare satisfied.  A program loop therefore consists of two segments, one known as the body of the loop and the other known as the control statements.
  • 33. Cont.. Depending on the position of the control statements in the loop, a control structure may be classified either as an entry-controlled loop or as the exit-controlled loop.
  • 34. Loops In C ⚫The C language provides for three loop constructs for performing loopoperations. ⚫Theyare: The whilestatement The dostatement The forstatement
  • 35. THE WHILE STATEMENT ⚫The basic formatof the while statement is while(testcondition) { bodyof the loop } ⚫ The while isan entry–controlled loop statement. ⚫ The test-condition is evaluated and if the condition is true, then the bodyof the loop is executed. ⚫ After execution of the body, the test-condition is once again evaluated and if it is true, the body is executed onceagain. ⚫ This process of repeated execution of the body continues until the test- condition finally becomes false and the control is transferred out of the loop.
  • 36. Example of WHILE Loop Bodyof the loop test condn? while(testcondition) { bodyof the loop } sum = 0; n = 1; while(n <= 10) { sum = sum + n* n; n = n + 1; } printf(“sum = %d n”,sum);
  • 38.
  • 39. THE DO STATEMENT ⚫In while loop the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt. ⚫Such situations can be handled with the help of the do statement. do { body of the loop } while(test condition);
  • 40. Cont.. ⚫Since the test-condition is evaluated at the bottom of the loop, the do…..while construct provides an exit-controlled loop and therefore the body of the loop is always executed at least once. Eg: do { printf(“Inputa numbern”); number= getnum(); } while(number> 0);
  • 41. Difference between While( ) loop and Do….while() loop
  • 42.
  • 43.
  • 44.
  • 45. THE FOR STATEMENT ⚫The for loop is another entry-controlled loop that provides a more concise loop control structure ⚫Thegeneral form of the for loop is for(initialization ; test-condition ; increment/Decrement) { body of the loop }
  • 46. THE FOR STATEMENT for(initialization ; test-condition ; increment/Decrement) { body of the loop }
  • 47. Cont.. ⚫ The execution of the forstatement isas follows:  Initialization of the control variables is done first.  Thevalue of thecontrol variable is tested using the test-condition.  If the condition is true, the body of the loop is executed; otherwise the loop is terminated and the execution continues with the statementthat immediately follows the loop.  When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop.  Now, the control variable is either incremented or decremented as perthecondition.
  • 48. Cont.. ⚫Eg 2) sum = 0; for(i = 1; i < 20; ++i) { sum =sum + i; printf(“%d %d n”,sum); } for(initialization ; test-condition ; increment/Devrement) { bodyof the loop }
  • 49. For Statement ⚫Eg 1) for(x = 0; x <= 9; x = x + 1) { printf)”%d” ,x); } printf(“n”); ⚫ The multiplearguments in the incrementsectionare possible and separated by commas.
  • 50. Nesting of For Loops ⚫C allowsone forstatementwithin anotherfor statement.
  • 51. Cont.. ⚫ Eg: ⚫ ⚫ ⚫ for(row = 1; row <= ROWMAX; ++row) ⚫ { ⚫ for(column = 1; column < = COLMAX; ++column) ⚫ { ⚫ y = row * column; ⚫ printf(“%4d” , y); ⚫ } ⚫ printf(“n”); ⚫ } ⚫
  • 52. JUMPS IN LOOPS  C permits a jump from one statement to another within a loop as well as the jumpoutof a loop. Jumping outof a Loop  An early exit from a loop can be accomplished by using the break statementor thegoto statement.  When the break statement is encountered inside a loop, the loop is immediately exited and the program continues with thestatement immediately following the loop.  When the loops are nested, the break would only exit from the loop containing it. That is, the break will exit only a single loop.
  • 53. Skipping a part of a Loop ⚫Like the break statement, C supports another similar statementcalled thecontinue statement. ⚫However, unlike the break which causes the loop to be terminated, the continue, as the name implies, causes the loop to be continued with the next iteration after skipping any statements in between. ⚫The continue statement tells the compiler, FOLLOWING STATEMENTS AND CONTINUE WITH ITERATION”. ⚫The formatof thecontinue statement is simply “SKIP THE THE NEXT continue;
  • 55. References 1. http://www.computer-books.us/c_0008.php 2. http://www.computer-books.us/c_0009 3. http://www.computer-books.us/c_2.php 4. www.tutorialspoint.com/cprogramming/cprogramming_pdf. 5. Programming in C by yashwant kanitkar 6. ANSI C by E.balagurusamy- TMG publication 7. Computer programming and Utilization by sanjay shah Mahajan Publication 8. www.cprogramming.com/books.html