SlideShare a Scribd company logo
Control statements
Topics
 if statement
 switch statement
 goto statement
If statement
 The if statement is a powerful decision
making statement and is used to control the
flow of execution of statements.
 The if statement can be implemented in
different forms:
1. Simple if statement
2. if….else statement
3. Nested if….else statement
4. else if ladder
Simple If statement
 The general form of a simple if statement is:
if(condition)
{
statement-block;
}
satement-x;
 If the condition is true then statement-block
is executed; otherwise the statement-block
will be skipped and the execution will jump
to the statement-x;
If…else statement
 The if…else statement is an extension of the
simple if statement. The general form is
if(condition)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x;
Continue..
 If the condition is true then the true-block
statement(s) will execute; otherwise the
false-block statement(s) will execute.
 Then it will go to statement-x
 Ex: if(x>y)
z=x-y;
else
z=y-x;
printf(“%d”,z);
Nested If…else statement
 When a series of decisions are involved, we may have to use more than one
if…else statement in nested form as follows:
if(condition-1)
{
if(condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
statement-x;
Continue…
 If condition-1 is false then statement-3 will
be executed; otherwise if continues to
perform the second test.
 If the condition-2 is true, then statement-1
will be executed; otherwise statement-2 will
be executed.
 Then the control will be transferred to
statement-x.
Else If ladder
 There is another way of putting ifs together when
multipath decisions are involved. The general
form:
if(condition-1)
statement-1;
else if(condition-2)
statement-2;
-------------
else if(condition-n)
statement-n;
else
default-statement;
statement-x;
Continue..
 The conditions are evaluated from the top.
As soon as a true condition is found, the
statement associated with it is executed and
the control is transferred to the statement-x.
 When all the conditions are false then the
final else containing the default-statement
will be executed.
switch statement
 Switch is a multiway decision statement. The
general form:
switch(expression)
{
case value-1: block-1;
break;
case value-2: block-2;
break;
-------------
default: default-block;
}
statement-x;
Continue…
 The expression is an integer expression or
character. Value-1, value-2,… are constants or
constant expression and are known as case
labels. Each of these values should be unique.
 When the switch is executed, the value of the
expression is successively compared against the
values value-1, value-2,….If a case is found whose
value matches with the value of the expression,
the block of statements are executed.
 The break at the end of each block signals the end
of particular case and causes exit from switch and
transfer control to statement-x.
 The default is optional and if no matches occur
then this default-block is executed.

More Related Content

What's hot

structure and union
structure and unionstructure and union
structure and union
student
 

What's hot (20)

Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Control structure
Control structureControl structure
Control structure
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
 
Static variables
Static variablesStatic variables
Static variables
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
structure and union
structure and unionstructure and union
structure and union
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Structure and union
Structure and unionStructure and union
Structure and union
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Type conversion
Type conversionType conversion
Type conversion
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 

Similar to Chap 5(decision making-branching)

Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 

Similar to Chap 5(decision making-branching) (20)

If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
C statements
C statementsC statements
C statements
 
Selection statements
Selection statementsSelection statements
Selection statements
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
Decision control structures
Decision control structuresDecision control structures
Decision control structures
 
Control structure of c language
Control structure of c languageControl structure of c language
Control structure of c language
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
Decision makingandbranching in c
Decision makingandbranching in cDecision makingandbranching in c
Decision makingandbranching in c
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
 
Decision Making.pptx
Decision Making.pptxDecision Making.pptx
Decision Making.pptx
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
 
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
 
1. Control Structure in C.pdf
1. Control Structure in C.pdf1. Control Structure in C.pdf
1. Control Structure in C.pdf
 

More from Bangabandhu Sheikh Mujibur Rahman Science and Technology University

More from Bangabandhu Sheikh Mujibur Rahman Science and Technology University (20)

Antenna (2)
Antenna (2)Antenna (2)
Antenna (2)
 
Voltage suppler..
Voltage suppler..Voltage suppler..
Voltage suppler..
 
Number system
Number systemNumber system
Number system
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
 
Chap 11(pointers)
Chap 11(pointers)Chap 11(pointers)
Chap 11(pointers)
 
Chap 10(structure and unions)
Chap 10(structure and unions)Chap 10(structure and unions)
Chap 10(structure and unions)
 
Chap 9(functions)
Chap 9(functions)Chap 9(functions)
Chap 9(functions)
 
Chap 8(strings)
Chap 8(strings)Chap 8(strings)
Chap 8(strings)
 
Chap 7(array)
Chap 7(array)Chap 7(array)
Chap 7(array)
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)
Chap 6(decision making-looping)
 
Chap 3(operator expression)
Chap 3(operator expression)Chap 3(operator expression)
Chap 3(operator expression)
 
Chap 2(const var-datatype)
Chap 2(const var-datatype)Chap 2(const var-datatype)
Chap 2(const var-datatype)
 
Computer hardware ppt1
Computer hardware ppt1Computer hardware ppt1
Computer hardware ppt1
 
# Operating system
# Operating system# Operating system
# Operating system
 
Magnetism 3
Magnetism 3Magnetism 3
Magnetism 3
 
Magnetism 2
Magnetism 2Magnetism 2
Magnetism 2
 
2. sinusoidal waves
2. sinusoidal waves2. sinusoidal waves
2. sinusoidal waves
 
Magnetism 1
Magnetism 1Magnetism 1
Magnetism 1
 

Recently uploaded

Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
YibeltalNibretu
 

Recently uploaded (20)

How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 

Chap 5(decision making-branching)

  • 1. Control statements Topics  if statement  switch statement  goto statement
  • 2. If statement  The if statement is a powerful decision making statement and is used to control the flow of execution of statements.  The if statement can be implemented in different forms: 1. Simple if statement 2. if….else statement 3. Nested if….else statement 4. else if ladder
  • 3. Simple If statement  The general form of a simple if statement is: if(condition) { statement-block; } satement-x;  If the condition is true then statement-block is executed; otherwise the statement-block will be skipped and the execution will jump to the statement-x;
  • 4. If…else statement  The if…else statement is an extension of the simple if statement. The general form is if(condition) { True-block statement(s) } else { False-block statement(s) } statement-x;
  • 5. Continue..  If the condition is true then the true-block statement(s) will execute; otherwise the false-block statement(s) will execute.  Then it will go to statement-x  Ex: if(x>y) z=x-y; else z=y-x; printf(“%d”,z);
  • 6. Nested If…else statement  When a series of decisions are involved, we may have to use more than one if…else statement in nested form as follows: if(condition-1) { if(condition-2) { statement-1; } else { statement-2; } } else { statement-3; } statement-x;
  • 7. Continue…  If condition-1 is false then statement-3 will be executed; otherwise if continues to perform the second test.  If the condition-2 is true, then statement-1 will be executed; otherwise statement-2 will be executed.  Then the control will be transferred to statement-x.
  • 8. Else If ladder  There is another way of putting ifs together when multipath decisions are involved. The general form: if(condition-1) statement-1; else if(condition-2) statement-2; ------------- else if(condition-n) statement-n; else default-statement; statement-x;
  • 9. Continue..  The conditions are evaluated from the top. As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the statement-x.  When all the conditions are false then the final else containing the default-statement will be executed.
  • 10. switch statement  Switch is a multiway decision statement. The general form: switch(expression) { case value-1: block-1; break; case value-2: block-2; break; ------------- default: default-block; } statement-x;
  • 11. Continue…  The expression is an integer expression or character. Value-1, value-2,… are constants or constant expression and are known as case labels. Each of these values should be unique.  When the switch is executed, the value of the expression is successively compared against the values value-1, value-2,….If a case is found whose value matches with the value of the expression, the block of statements are executed.  The break at the end of each block signals the end of particular case and causes exit from switch and transfer control to statement-x.  The default is optional and if no matches occur then this default-block is executed.