SlideShare a Scribd company logo
© 2015UPESJuly 2015 Department. Of Civil Engineering
DECISION CONTROL AND
LOOPING STATEMENTS
© 2015UPESJuly 2015 Department. Of Civil Engineering
DECISION CONTROL STATEMENTS
 Decision control statements are used to alter the flow of a sequence of instructions.
 These statements help to jump from one part of the program to another depending on whether
a particular condition is satisfied or not.
 These decision control statements include:
If statement
If else statement
If else if statement
Switch statement
© 2015UPESJuly 2015 Department. Of Civil Engineering
IF STATEMENT
 If statement is the simplest form of decision control statements that is frequently used in
decision making. The general form of a simple if statement is shown in the figure.
 First the test expression is evaluated. If the test expression is true, the statement of if block
(statement 1 to n) are executed otherwise these statements will be skipped and the execution
will jump to statement x.
SYNTAX OF IF STATEMENT
if (test expression)
{
statement 1;
..............
statement n;
}
statement x;
Test
Expression
Statement Block 1
TRUE
Statement x
FALSE
© 2015UPESJuly 2015 Department. Of Civil Engineering
IF ELSE STATEMENT
 In the if-else construct, first the test expression is evaluated. If the expression is true, statement block 1 is
executed and statement block 2 is skipped. Otherwise, if the expression is false, statement block 2 is
executed and statement block 1 is ignored. In any case after the statement block 1 or 2 gets executed the
control will pass to statement x. Therefore, statement x is executed in every case.
SYNTAX OF IF STATEMENT
if (test expression)
{
statement_block 1;
}
else
{
statement_block 2;
}
statement x;
Test Expression
Statement Block 1
TRUE
Statement x
FALSE
Statement Block 2
© 2015UPESJuly 2015 Department. Of Civil Engineering
// Program to demonstrate the use of if-statement
#include<stdio.h>
int main()
{
int x=10;
if ( x>0)
x++;
printf("n x = %d", x);
return 0;
}
PROGRAMS TO DEMONSTRATE THE USE OF
IF STATEMENT
© 2015UPESJuly 2015 Department. Of Civil Engineering
 // PROGRAM TO FIND WHETHER A NUMBER IS EVEN OR ODD
 #include<stdio.h>
 main()
 {
 int a;
 printf("n Enter the value of a : ");
 scanf("%d", &a);
 if(a%2==0)
 printf("n %d is even", a);
 else
 printf("n %d is odd", a);
 return 0;
 }
PROGRAMS TO DEMONSTRATE THE USE OF
if elseSTATEMENT
© 2015UPESJuly 2015 Department. Of Civil Engineering
IF ELSE IF STATEMENT
 C language supports if else if statements to test additional conditions apart from the initial test
expression. The if-else-if construct works in the same way as a normal if statement.
SYNTAX OF IF-ELSE STATEMENT
if ( test expression 1)
{
statement block 1;
}
else if ( test expression 2)
{
statement block 2;
}
...........................
else if (test expression N)
{
statement block N;
}
else
{
Statement Block X;
}
Statement Y;
Test Expression 1
Statement Block 1
TRUE FALSE
Test Expression 2
Statement Block 2
TRUE
FALSE
Statement Block X
Statement Y
© 2015UPESJuly 2015 Department. Of Civil Engineering
// PROGRAM TO CLASSIFY A NUMBER AS POSITIVE, NEGATIVE OR
ZERO
#include<stdio.h>
main()
{
int num;
printf("n Enter any number : ");
scanf("%d", &num);
if(num==0)
printf("n The value is equal to zero");
else if(num>0)
printf("n The number is positive");
else
printf("n The number is negative");
return 0;
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
SWITCH CASE
 A switch case statement is a multi-way decision statement. Switch statements are used:
1. When there is only one variable to evaluate in the expression. 2. When many conditions are
being tested for.
 Switch case statement advantages include:1. Easy to debug, read, understand and maintain. 2.
Execute faster than its equivalent if-else construct .
switch(grade)
{case 'A':
printf("n Excellent");
break;
case 'B':
printf("n Good");
break;
case 'C':
printf("n Fair");
break;
default:
printf("n Invalid Grade");
break; }
© 2015UPESJuly 2015 Department. Of Civil Engineering
 int number = 10;
if(number == 1)
{
printf("Given number is 1n");
}
else if((number == 2)
{
printf("Given number is 2n");
}
else if((number ==3)
{
printf("Given number is 3n");
}
else if((number ==4)
{
printf("Given number is 4n");
}
else if (number ==5)
{
printf("Given number is 5n");
}
else
{
if(number<0)
printf("Given number is negativen");
else
printf("Given number is greater than 5n");
© 2015UPESJuly 2015 Department. Of Civil Engineering
© 2015UPESJuly 2015 Department. Of Civil Engineering
 // PROGRAM TO PRINT THE DAY OF THE WEEK
 #include<stdio.h>
 int main( )
 { int day;
 printf(“n Enter any number from 1 to 7 : “);
 scanf(“%d”,&day);
 switch(day)
 { case 1: printf(“n SUNDAY”); break;
 case 2 : printf(“n MONDAY”); break;
 case 3 : printf(“n TUESDAY”); break;
 case 4 : printf(“n WEDNESDAY”); break;
 case 5 : printf(“n THURSDAY”); break;
 case 6 : printf(“n FRIDAY”); break;
 case 7 : printf(“n SATURDAY”); break;
 default: printf(“n Wrong Number”);
 } return 0; }
© 2015UPESJuly 2015 Department. Of Civil Engineering
The ?: Operator
 ?:- is known as conditional operator and is useful for making two way
decisions.
 It takes three arguments.
 Conditional expression? Expression1:exprssion2
 The conditional expression is evaluated first. If the result is nonzero,
expression1 is evaluated and is returned as the value of conditional expression.
Otherwise expression2 is evaluated and its value is returned.
If (x<0)
flag=0;
else
flag=1; can be written as flag=(x<0)?: 0:1;
© 2015UPESJuly 2015 Department. Of Civil Engineering
Programming Exercises
 WAP to determine whether a given number is ODD or EVEN and print the
message NUMBER IS EVEN or NUMBER IS ODD
 Write a program to find the biggest of 3 numbers using conditional
operator/ternary operator?
 Write a program to find the roots of a quadratic equation?
 Program to find the average of students marks, when fail in any subject then
result is ‘FAIL’ otherwise print the grade as pass /first class/distinction
 Suppose the postal rates for mailing letters are as follows:
Rs.0.50 per 10 grams for the first 50 grams.
Rs.0.40 per 10 grams for the next 100 grams.
Rs.0.25 per 10 grams for the next 250 grams and
Rs25 per kilogram for the letters weighing more than 400 grams.
Write a program that prompts for the weight of a letter and prints the postage to
be paid.
© 2015UPESJuly 2015 Department. Of Civil Engineering
ITERATIVE STATEMENTS
 Iterative statements are used to repeat the execution of a list of statements, depending on the
value of an integer expression. In this section, we will discuss all these statements.
While loop
Do-while loop
For loop
WHILE LOOP
• The while loop is used to repeat one or more statements while a particular condition is true.
• In the while loop, the condition is tested before any of the statements in the statement block is executed.
• If the condition is true, only then the statements will be executed otherwise the control will jump to the
immediate statement outside the while loop block.
• We must constantly update the condition of the while loop.
while (condition)
{
statement_block;
}
statement x;
Statement x
Condition
Statement y
FALSE
Update the condition
expression
Statement Block
TRUE
© 2015UPESJuly 2015 Department. Of Civil Engineering
© 2015UPESJuly 2015 Department. Of Civil Engineering
DO WHILE LOOP
 The do-while loop is similar to the while loop. The only difference is that in a do-while loop, the
test condition is tested at the end of the loop.
 The body of the loop gets executed at least one time (even if the condition is false).
 The do while loop continues to execute whilst a condition is true. There is no choice whether to
execute the loop or not. Hence, entry in the loop is automatic there is only a choice to continue it
further or not.
 The major disadvantage of using a do while loop is that it always executes at least once, so even
if the user enters some invalid data, the loop will execute.
 Do-while loops are widely used to print a list of options for a menu driven program.
Statement x;
do
{
statement_block;
} while (condition);
statement y;
Statement x
Statement y
Statement Block
Update the condition expression
Condition
FALSE
TRUE
© 2015UPESJuly 2015 Department. Of Civil Engineering
© 2015UPESJuly 2015 Department. Of Civil Engineering
FOR LOOP
 For loop is used to repeat a task until a particular condition is true.
 The syntax of a for loop
for (initialization; condition; increment/decrement/update)
{
statement block;
}
Statement Y;
 When a for loop is used, the loop variable is initialized only once.
 With every iteration of the loop, the value of the loop variable is updated and the condition is
checked. If the condition is true, the statement block of the loop is executed else, the
statements comprising the statement block of the for loop are skipped and the control jumps to
the immediate statement following the for loop body.
 Updating the loop variable may include incrementing the loop variable, decrementing the loop
variable or setting it to some other value like, i +=2, where i is the loop variable.
© 2015UPESJuly 2015 Department. Of Civil Engineering
PROGRAM FOR FOR-LOOP
Look at the code given below which print first n numbers using a for loop.
#include<stdio.h>
int main()
{
int i, n;
printf(“n Enter the value of n :”);
scanf(“%d”, &n);
for(i=0; i<= n; i++)
printf(“n %d”, i);
return 0;
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
© 2015UPESJuly 2015 Department. Of Civil Engineering
Nested Loop
 In many cases we may use loop statement inside another looping
statement. This type of looping is called nested loop. In nested loop
the inner loop is executed first and then outer.
© 2015UPESJuly 2015 Department. Of Civil Engineering
Jumps in Loops
 Sometimes, while executing a loop, it becomes
necessary to skip a part of the loop or to leave the loop
as soon as certain condition becomes true, that is called
jumping out of loop. C language allows jumping from
one statement to another within a loop as well as
jumping out of the loop.
© 2015UPESJuly 2015 Department. Of Civil Engineering
Break Statement
 The break statement is a jump instruction and can be
used inside a switch construct, for loop, while loop and
do-while loop. The execution of break statement
causes immediate exit from the concern construct and
the control is transferred to the statement following the
loop. In the loop construct the execution of break
statement terminates loop and further execution of the
program is reserved with the statement following the
body of the loop.
© 2015UPESJuly 2015 Department. Of Civil Engineering
© 2015UPESJuly 2015 Department. Of Civil Engineering
CONTINUE STATEMENT
• continue statement is a jump statement. The continue
statement can be used only inside for loop, while loop and
do-while loop. Execution of these statement does not cause
an exit from the loop but it suspend the execution of the
loop for that iteration and transfer control back to the loop
for the next iteration.
© 2015UPESJuly 2015 Department. Of Civil Engineering
© 2015UPESJuly 2015 Department. Of Civil Engineering
GOTO STATEMENT
 The goto statement is used to transfer control to a specified label.
 Here label is an identifier that specifies the place where the branch is to be made. Label can be any valid variable name that is
followed by a colon (:).
 Note that label can be placed anywhere in the program either before or after the goto statement. Whenever the goto statement is
encountered the control is immediately transferred to the statements following the label.
 Goto statement breaks the normal sequential execution of the program.
 If the label is placed after the goto statement then it is called a forward jump and in case it is located before the goto statement, it
is said to be a backward jump.
 int num, sum=0;
 read: // label for go to statement
 printf("n Enter the number. Enter 999 to end : ");
 scanf("%d", &num);
 if (num != 999)
 {
 if(num < 0)
 goto read; // jump to label- read
 sum += num;
 goto read; // jump to label- read
 }
 printf("n Sum of the numbers entered by the user is = %d", sum);

More Related Content

What's hot

Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
Manash Kumar Mondal
 
201707 CSE110 Lecture 10
201707 CSE110 Lecture 10 201707 CSE110 Lecture 10
201707 CSE110 Lecture 10
Javier Gonzalez-Sanchez
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Manojkumar C
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
cherrybear2014
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
Niloy Saha
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
Hossain Md Shakhawat
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
Raj Parekh
 
C++ programming Unit 5 flow of control
C++ programming Unit 5 flow of controlC++ programming Unit 5 flow of control
C++ programming Unit 5 flow of control
AAKASH KUMAR
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Control structures i
Control structures i Control structures i
Control structures i
Ahmad Idrees
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
Kathirvel Ayyaswamy
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
Andy Juan Sarango Veliz
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Soran University
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
It Academy
 
10. switch case
10. switch case10. switch case
10. switch case
Way2itech
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
jyoti_lakhani
 
javase-1.4.2-docs-guide-lang-assert
javase-1.4.2-docs-guide-lang-assertjavase-1.4.2-docs-guide-lang-assert
javase-1.4.2-docs-guide-lang-assert
lukebonham
 

What's hot (20)

Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
201707 CSE110 Lecture 10
201707 CSE110 Lecture 10 201707 CSE110 Lecture 10
201707 CSE110 Lecture 10
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 
C++ programming Unit 5 flow of control
C++ programming Unit 5 flow of controlC++ programming Unit 5 flow of control
C++ programming Unit 5 flow of control
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Control structures i
Control structures i Control structures i
Control structures i
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Branching statements
Branching statementsBranching statements
Branching statements
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
10. switch case
10. switch case10. switch case
10. switch case
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
javase-1.4.2-docs-guide-lang-assert
javase-1.4.2-docs-guide-lang-assertjavase-1.4.2-docs-guide-lang-assert
javase-1.4.2-docs-guide-lang-assert
 

Similar to Control flow stataements

Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
Rakesh Roshan
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
C Programming Lesson 3.pdf
C Programming Lesson 3.pdfC Programming Lesson 3.pdf
C Programming Lesson 3.pdf
Rajeev Mishra
 
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
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
teach4uin
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in c
vampugani
 
Introduction to C Programming
Introduction to C Programming Introduction to C Programming
Introduction to C Programming
vampugani
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
JavvajiVenkat
 
Module 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdfModule 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
IRJET- Switch Case Statements in C
IRJET-  	  Switch Case Statements in CIRJET-  	  Switch Case Statements in C
IRJET- Switch Case Statements in C
IRJET Journal
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Chandrakant Divate
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
University of Potsdam
 

Similar to Control flow stataements (20)

Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
C Programming Lesson 3.pdf
C Programming Lesson 3.pdfC Programming Lesson 3.pdf
C Programming Lesson 3.pdf
 
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
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in c
 
Introduction to C Programming
Introduction to C Programming Introduction to C Programming
Introduction to C Programming
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Module 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdfModule 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdf
 
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
 
IRJET- Switch Case Statements in C
IRJET-  	  Switch Case Statements in CIRJET-  	  Switch Case Statements in C
IRJET- Switch Case Statements in C
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 

More from Mitali Chugh

Loc and function point
Loc and function pointLoc and function point
Loc and function point
Mitali Chugh
 
Unit1
Unit1Unit1
Unit 2 ppt
Unit 2 pptUnit 2 ppt
Unit 2 ppt
Mitali Chugh
 
Module 4
Module 4Module 4
Module 4
Mitali Chugh
 
Blogs
BlogsBlogs
Module 5 and 6
Module 5 and 6Module 5 and 6
Module 5 and 6
Mitali Chugh
 
Types of computer
Types of computerTypes of computer
Types of computer
Mitali Chugh
 
Module 2 os
Module 2 osModule 2 os
Module 2 os
Mitali Chugh
 
Os ppt
Os pptOs ppt
Os ppt
Mitali Chugh
 
Upes ppt template
Upes ppt templateUpes ppt template
Upes ppt template
Mitali Chugh
 
Functions
FunctionsFunctions
Functions
Mitali Chugh
 
Structures
StructuresStructures
Structures
Mitali Chugh
 
Functions
FunctionsFunctions
Functions
Mitali Chugh
 
Strings
StringsStrings
Strings
Mitali Chugh
 
Arrays
ArraysArrays
Arrays
Mitali Chugh
 
Unit 2 l1
Unit 2 l1Unit 2 l1
Unit 2 l1
Mitali Chugh
 
Unit1
Unit1Unit1
How to compile and run a c program on ubuntu linux
How to compile and run a c program on ubuntu linuxHow to compile and run a c program on ubuntu linux
How to compile and run a c program on ubuntu linux
Mitali Chugh
 
Blogs
BlogsBlogs
Module 4
Module 4Module 4
Module 4
Mitali Chugh
 

More from Mitali Chugh (20)

Loc and function point
Loc and function pointLoc and function point
Loc and function point
 
Unit1
Unit1Unit1
Unit1
 
Unit 2 ppt
Unit 2 pptUnit 2 ppt
Unit 2 ppt
 
Module 4
Module 4Module 4
Module 4
 
Blogs
BlogsBlogs
Blogs
 
Module 5 and 6
Module 5 and 6Module 5 and 6
Module 5 and 6
 
Types of computer
Types of computerTypes of computer
Types of computer
 
Module 2 os
Module 2 osModule 2 os
Module 2 os
 
Os ppt
Os pptOs ppt
Os ppt
 
Upes ppt template
Upes ppt templateUpes ppt template
Upes ppt template
 
Functions
FunctionsFunctions
Functions
 
Structures
StructuresStructures
Structures
 
Functions
FunctionsFunctions
Functions
 
Strings
StringsStrings
Strings
 
Arrays
ArraysArrays
Arrays
 
Unit 2 l1
Unit 2 l1Unit 2 l1
Unit 2 l1
 
Unit1
Unit1Unit1
Unit1
 
How to compile and run a c program on ubuntu linux
How to compile and run a c program on ubuntu linuxHow to compile and run a c program on ubuntu linux
How to compile and run a c program on ubuntu linux
 
Blogs
BlogsBlogs
Blogs
 
Module 4
Module 4Module 4
Module 4
 

Recently uploaded

2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
Aditya Rajan Patra
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 

Recently uploaded (20)

2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 

Control flow stataements

  • 1. © 2015UPESJuly 2015 Department. Of Civil Engineering DECISION CONTROL AND LOOPING STATEMENTS
  • 2. © 2015UPESJuly 2015 Department. Of Civil Engineering DECISION CONTROL STATEMENTS  Decision control statements are used to alter the flow of a sequence of instructions.  These statements help to jump from one part of the program to another depending on whether a particular condition is satisfied or not.  These decision control statements include: If statement If else statement If else if statement Switch statement
  • 3. © 2015UPESJuly 2015 Department. Of Civil Engineering IF STATEMENT  If statement is the simplest form of decision control statements that is frequently used in decision making. The general form of a simple if statement is shown in the figure.  First the test expression is evaluated. If the test expression is true, the statement of if block (statement 1 to n) are executed otherwise these statements will be skipped and the execution will jump to statement x. SYNTAX OF IF STATEMENT if (test expression) { statement 1; .............. statement n; } statement x; Test Expression Statement Block 1 TRUE Statement x FALSE
  • 4. © 2015UPESJuly 2015 Department. Of Civil Engineering IF ELSE STATEMENT  In the if-else construct, first the test expression is evaluated. If the expression is true, statement block 1 is executed and statement block 2 is skipped. Otherwise, if the expression is false, statement block 2 is executed and statement block 1 is ignored. In any case after the statement block 1 or 2 gets executed the control will pass to statement x. Therefore, statement x is executed in every case. SYNTAX OF IF STATEMENT if (test expression) { statement_block 1; } else { statement_block 2; } statement x; Test Expression Statement Block 1 TRUE Statement x FALSE Statement Block 2
  • 5. © 2015UPESJuly 2015 Department. Of Civil Engineering // Program to demonstrate the use of if-statement #include<stdio.h> int main() { int x=10; if ( x>0) x++; printf("n x = %d", x); return 0; } PROGRAMS TO DEMONSTRATE THE USE OF IF STATEMENT
  • 6. © 2015UPESJuly 2015 Department. Of Civil Engineering  // PROGRAM TO FIND WHETHER A NUMBER IS EVEN OR ODD  #include<stdio.h>  main()  {  int a;  printf("n Enter the value of a : ");  scanf("%d", &a);  if(a%2==0)  printf("n %d is even", a);  else  printf("n %d is odd", a);  return 0;  } PROGRAMS TO DEMONSTRATE THE USE OF if elseSTATEMENT
  • 7. © 2015UPESJuly 2015 Department. Of Civil Engineering IF ELSE IF STATEMENT  C language supports if else if statements to test additional conditions apart from the initial test expression. The if-else-if construct works in the same way as a normal if statement. SYNTAX OF IF-ELSE STATEMENT if ( test expression 1) { statement block 1; } else if ( test expression 2) { statement block 2; } ........................... else if (test expression N) { statement block N; } else { Statement Block X; } Statement Y; Test Expression 1 Statement Block 1 TRUE FALSE Test Expression 2 Statement Block 2 TRUE FALSE Statement Block X Statement Y
  • 8. © 2015UPESJuly 2015 Department. Of Civil Engineering // PROGRAM TO CLASSIFY A NUMBER AS POSITIVE, NEGATIVE OR ZERO #include<stdio.h> main() { int num; printf("n Enter any number : "); scanf("%d", &num); if(num==0) printf("n The value is equal to zero"); else if(num>0) printf("n The number is positive"); else printf("n The number is negative"); return 0; }
  • 9. © 2015UPESJuly 2015 Department. Of Civil Engineering SWITCH CASE  A switch case statement is a multi-way decision statement. Switch statements are used: 1. When there is only one variable to evaluate in the expression. 2. When many conditions are being tested for.  Switch case statement advantages include:1. Easy to debug, read, understand and maintain. 2. Execute faster than its equivalent if-else construct . switch(grade) {case 'A': printf("n Excellent"); break; case 'B': printf("n Good"); break; case 'C': printf("n Fair"); break; default: printf("n Invalid Grade"); break; }
  • 10. © 2015UPESJuly 2015 Department. Of Civil Engineering  int number = 10; if(number == 1) { printf("Given number is 1n"); } else if((number == 2) { printf("Given number is 2n"); } else if((number ==3) { printf("Given number is 3n"); } else if((number ==4) { printf("Given number is 4n"); } else if (number ==5) { printf("Given number is 5n"); } else { if(number<0) printf("Given number is negativen"); else printf("Given number is greater than 5n");
  • 11. © 2015UPESJuly 2015 Department. Of Civil Engineering
  • 12. © 2015UPESJuly 2015 Department. Of Civil Engineering  // PROGRAM TO PRINT THE DAY OF THE WEEK  #include<stdio.h>  int main( )  { int day;  printf(“n Enter any number from 1 to 7 : “);  scanf(“%d”,&day);  switch(day)  { case 1: printf(“n SUNDAY”); break;  case 2 : printf(“n MONDAY”); break;  case 3 : printf(“n TUESDAY”); break;  case 4 : printf(“n WEDNESDAY”); break;  case 5 : printf(“n THURSDAY”); break;  case 6 : printf(“n FRIDAY”); break;  case 7 : printf(“n SATURDAY”); break;  default: printf(“n Wrong Number”);  } return 0; }
  • 13. © 2015UPESJuly 2015 Department. Of Civil Engineering The ?: Operator  ?:- is known as conditional operator and is useful for making two way decisions.  It takes three arguments.  Conditional expression? Expression1:exprssion2  The conditional expression is evaluated first. If the result is nonzero, expression1 is evaluated and is returned as the value of conditional expression. Otherwise expression2 is evaluated and its value is returned. If (x<0) flag=0; else flag=1; can be written as flag=(x<0)?: 0:1;
  • 14. © 2015UPESJuly 2015 Department. Of Civil Engineering Programming Exercises  WAP to determine whether a given number is ODD or EVEN and print the message NUMBER IS EVEN or NUMBER IS ODD  Write a program to find the biggest of 3 numbers using conditional operator/ternary operator?  Write a program to find the roots of a quadratic equation?  Program to find the average of students marks, when fail in any subject then result is ‘FAIL’ otherwise print the grade as pass /first class/distinction  Suppose the postal rates for mailing letters are as follows: Rs.0.50 per 10 grams for the first 50 grams. Rs.0.40 per 10 grams for the next 100 grams. Rs.0.25 per 10 grams for the next 250 grams and Rs25 per kilogram for the letters weighing more than 400 grams. Write a program that prompts for the weight of a letter and prints the postage to be paid.
  • 15. © 2015UPESJuly 2015 Department. Of Civil Engineering ITERATIVE STATEMENTS  Iterative statements are used to repeat the execution of a list of statements, depending on the value of an integer expression. In this section, we will discuss all these statements. While loop Do-while loop For loop WHILE LOOP • The while loop is used to repeat one or more statements while a particular condition is true. • In the while loop, the condition is tested before any of the statements in the statement block is executed. • If the condition is true, only then the statements will be executed otherwise the control will jump to the immediate statement outside the while loop block. • We must constantly update the condition of the while loop. while (condition) { statement_block; } statement x; Statement x Condition Statement y FALSE Update the condition expression Statement Block TRUE
  • 16. © 2015UPESJuly 2015 Department. Of Civil Engineering
  • 17. © 2015UPESJuly 2015 Department. Of Civil Engineering DO WHILE LOOP  The do-while loop is similar to the while loop. The only difference is that in a do-while loop, the test condition is tested at the end of the loop.  The body of the loop gets executed at least one time (even if the condition is false).  The do while loop continues to execute whilst a condition is true. There is no choice whether to execute the loop or not. Hence, entry in the loop is automatic there is only a choice to continue it further or not.  The major disadvantage of using a do while loop is that it always executes at least once, so even if the user enters some invalid data, the loop will execute.  Do-while loops are widely used to print a list of options for a menu driven program. Statement x; do { statement_block; } while (condition); statement y; Statement x Statement y Statement Block Update the condition expression Condition FALSE TRUE
  • 18. © 2015UPESJuly 2015 Department. Of Civil Engineering
  • 19. © 2015UPESJuly 2015 Department. Of Civil Engineering FOR LOOP  For loop is used to repeat a task until a particular condition is true.  The syntax of a for loop for (initialization; condition; increment/decrement/update) { statement block; } Statement Y;  When a for loop is used, the loop variable is initialized only once.  With every iteration of the loop, the value of the loop variable is updated and the condition is checked. If the condition is true, the statement block of the loop is executed else, the statements comprising the statement block of the for loop are skipped and the control jumps to the immediate statement following the for loop body.  Updating the loop variable may include incrementing the loop variable, decrementing the loop variable or setting it to some other value like, i +=2, where i is the loop variable.
  • 20. © 2015UPESJuly 2015 Department. Of Civil Engineering PROGRAM FOR FOR-LOOP Look at the code given below which print first n numbers using a for loop. #include<stdio.h> int main() { int i, n; printf(“n Enter the value of n :”); scanf(“%d”, &n); for(i=0; i<= n; i++) printf(“n %d”, i); return 0; }
  • 21. © 2015UPESJuly 2015 Department. Of Civil Engineering
  • 22. © 2015UPESJuly 2015 Department. Of Civil Engineering Nested Loop  In many cases we may use loop statement inside another looping statement. This type of looping is called nested loop. In nested loop the inner loop is executed first and then outer.
  • 23. © 2015UPESJuly 2015 Department. Of Civil Engineering Jumps in Loops  Sometimes, while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop as soon as certain condition becomes true, that is called jumping out of loop. C language allows jumping from one statement to another within a loop as well as jumping out of the loop.
  • 24. © 2015UPESJuly 2015 Department. Of Civil Engineering Break Statement  The break statement is a jump instruction and can be used inside a switch construct, for loop, while loop and do-while loop. The execution of break statement causes immediate exit from the concern construct and the control is transferred to the statement following the loop. In the loop construct the execution of break statement terminates loop and further execution of the program is reserved with the statement following the body of the loop.
  • 25. © 2015UPESJuly 2015 Department. Of Civil Engineering
  • 26. © 2015UPESJuly 2015 Department. Of Civil Engineering CONTINUE STATEMENT • continue statement is a jump statement. The continue statement can be used only inside for loop, while loop and do-while loop. Execution of these statement does not cause an exit from the loop but it suspend the execution of the loop for that iteration and transfer control back to the loop for the next iteration.
  • 27. © 2015UPESJuly 2015 Department. Of Civil Engineering
  • 28. © 2015UPESJuly 2015 Department. Of Civil Engineering GOTO STATEMENT  The goto statement is used to transfer control to a specified label.  Here label is an identifier that specifies the place where the branch is to be made. Label can be any valid variable name that is followed by a colon (:).  Note that label can be placed anywhere in the program either before or after the goto statement. Whenever the goto statement is encountered the control is immediately transferred to the statements following the label.  Goto statement breaks the normal sequential execution of the program.  If the label is placed after the goto statement then it is called a forward jump and in case it is located before the goto statement, it is said to be a backward jump.  int num, sum=0;  read: // label for go to statement  printf("n Enter the number. Enter 999 to end : ");  scanf("%d", &num);  if (num != 999)  {  if(num < 0)  goto read; // jump to label- read  sum += num;  goto read; // jump to label- read  }  printf("n Sum of the numbers entered by the user is = %d", sum);