SlideShare a Scribd company logo
Statements
&
Exceptions
Overview
 Introduction to Statements
 Using Selection Statements
 Using Iteration Statements
 Using Jump Statements
 Handling Basic Exceptions
 Raising Exceptions
Introduction to Statements
 Statement Blocks
 Types of Statements
Statement Blocks
 Use braces
As block
delimiters
{
// code
}
{
int i;
...
{
int i;
...
}
}
{
int i;
...
}
...
{
int i;
...
}
 A block and its
parent block
cannot have a
variable with
the same name
 Sibling blocks can
have variables
with
the same name
Types of Statements
Selection Statements
The if and switch statements
Iteration Statements
The while, do, for, and foreach statements
Jump Statements
The goto, break, and continue statements
Using Selection Statements
 The if Statement
 Cascading if Statements
 The switch Statement
 Quiz: Spot the Bugs
The if Statement
 Syntax:
 No implicit conversion from int to bool
int x;
...
if (x) ... // Must be if (x != 0) in C#
if (x = 0) ... // Must be if (x == 0) in C#
if ( Boolean-expression )
first-embedded-statement
else
second-embedded-statement
Cascading if Statements
enum Suit { Clubs, Hearts, Diamonds, Spades }
Suit trumps = Suit.Hearts;
if (trumps == Suit.Clubs)
color = "Black";
else if (trumps == Suit.Hearts)
color = "Red";
else if (trumps == Suit.Diamonds)
color = "Red";
else
color = "Black";
The switch Statement
 Use switch statements for multiple case blocks
 Use break statements to ensure that no fall
through occurs
switch (trumps) {
case Suit.Clubs :
case Suit.Spades :
color = "Black"; break;
case Suit.Hearts :
case Suit.Diamonds :
color = "Red"; break;
default:
color = "ERROR"; break;
}
Quiz: Spot the Bugs
if number % 2 == 0 ...
if (percent < 0) || (percent > 100) ...
if (minute == 60);
minute = 0;
switch (trumps) {
case Suit.Clubs, Suit.Spades :
color = "Black";
case Suit.Hearts, Suit.Diamonds :
color = "Red";
defualt :
...
}
2
3
4
1
Using Iteration Statements
 The while Statement
 The do Statement
 The for Statement
 The foreach Statement
 Quiz: Spot the Bugs
The while Statement
 Execute embedded statements based on Boolean value
 Evaluate Boolean expression at beginning of loop
 Execute embedded statements while Boolean value
Is True
int i = 0;
while (i < 10) {
Console.WriteLine(i);
i++;
}
0 1 2 3 4 5 6 7 8 9
The do Statement
 Execute embedded statements based on Boolean value
 Evaluate Boolean expression at end of loop
 Execute embedded statements while Boolean value
Is True
int i = 0;
do {
Console.WriteLine(i);
i++;
} while (i < 10);
0 1 2 3 4 5 6 7 8 9
The for Statement
 Place update information at the start of the
loop
 Variables in a for block are scoped only within
the block
 A for loop can iterate over several values
for (int i = 0; i < 10; i++) {
Console.WriteLine(i);
}
0 1 2 3 4 5 6 7 8 9
for (int i = 0; i < 10; i++)
Console.WriteLine(i);
Console.WriteLine(i); // Error: i is no longer in scope
for (int i = 0, j = 0; ... ; i++, j++)
The foreach Statement
 Choose the type and name of the iteration variable
 Execute embedded statements for each element of the
collection class
ArrayList numbers = new ArrayList( );
for (int i = 0; i < 10; i++ ) {
numbers.Add(i);
}
foreach (int number in numbers) {
Console.WriteLine(number);
}
0 1 2 3 4 5 6 7 8 9
Quiz: Spot the Bugs
for (int i = 0, i < 10, i++)
Console.WriteLine(i);
int i = 0;
while (i < 10)
Console.WriteLine(i);
for (int i = 0; i >= 10; i++)
Console.WriteLine(i);
do
...
string line = Console.ReadLine( );
guess = int.Parse(line);
while (guess != answer);
2
3
4
1
Using Jump Statements
 The goto Statement
 The break and continue Statements
The goto Statement
 Flow of control transferred to a labeled statement
 Can easily result in obscure “spaghetti” code
if (number % 2 == 0) goto Even;
Console.WriteLine("odd");
goto End;
Even:
Console.WriteLine("even");
End:;
The break and continue Statements
 The break statement jumps out of an iteration
 The continue statement jumps to the next iteration
int i = 0;
while (true) {
Console.WriteLine(i);
i++;
if (i < 10)
continue;
else
break;
}
Handling Basic Exceptions
 Why Use Exceptions?
 Exception Objects
 Using try and catch Blocks
 Multiple catch Blocks
Why Use Exceptions?
 Traditional procedural error handling is cumbersome
int errorCode = 0;
FileInfo source = new FileInfo("code.cs");
if (errorCode == -1) goto Failed;
int length = (int)source.Length;
if (errorCode == -2) goto Failed;
char[] contents = new char[length];
if (errorCode == -3) goto Failed;
// Succeeded ...
Failed: ... Error handling
Core program logic
Exception Objects
Exception
SystemException
OutOfMemoryException
IOException
NullReferenceException
ApplicationException
Using try and catch Blocks
 Object-oriented solution to error handling
 Put the normal code in a try block
 Handle the exceptions in a separate catch block
try {
Console.WriteLine("Enter a number");
int i = int.Parse(Console.ReadLine());
}
catch (OverflowException caught)
{
Console.WriteLine(caught);
}
Error handling
Program logic
Multiple catch Blocks
 Each catch block catches one class of exception
 A try block can have one general catch block
 A try block is not allowed to catch a class that is derived
from a class caught in an earlier catch block
try
{
Console.WriteLine("Enter first number");
int i = int.Parse(Console.ReadLine());
Console.WriteLine("Enter second number");
int j = int.Parse(Console.ReadLine());
int k = i / j;
}
catch (OverflowException caught) {…}
catch (DivideByZeroException caught) {…}
Raising Exceptions
 The throw Statement
 The finally Clause
 Checking for Arithmetic Overflow
 Guidelines for Handling Exceptions
The throw Statement
 Throw an appropriate exception
 Give the exception a meaningful message
throw expression ;
if (minute < 1 || minute >= 60) {
throw new InvalidTimeException(minute +
" is not a valid minute");
// !! Not reached !!
}
The finally Clause
 All of the statements in a finally block are always
executed
Monitor.Enter(x);
try {
...
}
finally {
Monitor.Exit(x);
}
Any catch blocks are optional
Checking for Arithmetic Overflow
 By default, arithmetic overflow is not checked
 A checked statement turns overflow checking on
checked {
int number = int.MaxValue;
Console.WriteLine(++number);
}
unchecked {
int number = int.MaxValue;
Console.WriteLine(++number);
} -2147483648
OverflowException
Exception object is thrown.
WriteLine is not executed.
MaxValue + 1 is negative?
Guidelines for Handling Exceptions
 Throwing
 Avoid exceptions for normal or expected cases
 Never create and throw objects of class Exception
 Include a description string in an Exception object
 Throw objects of the most specific class possible
 Catching
 Arrange catch blocks from specific to general
 Do not let exceptions drop off Main
Custom Exception
 Custom Exception class is derived from System.Exception
or one of the other common base exception classes.
 Provides a type safe mechanism for a developer to detect
an error condition.
 Add situation specific data to an exception.
 No existing exception adequately described my problem.
Review
 Introduction to Statements
 Using Selection Statements
 Using Iteration Statements
 Using Jump Statements
 Handling Basic Exceptions
 Raising Exceptions

More Related Content

What's hot (20)

Loop control statements
Loop control statementsLoop control statements
Loop control statements
Jaya Kumari
 
Decision statements
Decision statementsDecision statements
Decision statements
Jaya Kumari
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
ppd1961
 
Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
Kumar
 
Scjp questions
Scjp questionsScjp questions
Scjp questions
roudhran
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Operators
OperatorsOperators
Operators
Allah Ditta
 
04a intro while
04a intro while04a intro while
04a intro while
hasfaa1017
 
Exception Handling
Exception HandlingException Handling
Exception Handling
backdoor
 
Program control statements in c#
Program control statements in c#Program control statements in c#
Program control statements in c#
Dr.Neeraj Kumar Pandey
 
04 control structures 1
04 control structures 104 control structures 1
04 control structures 1
Jomel Penalba
 
Exception handling
Exception handlingException handling
Exception handling
Iblesoft
 
Exception handling
Exception handlingException handling
Exception handling
Prafull Johri
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Exceptions in python
Exceptions in pythonExceptions in python
Exceptions in python
baabtra.com - No. 1 supplier of quality freshers
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
Mohammed Sikander
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
rajshreemuthiah
 
Loop control statements
Loop control statementsLoop control statements
Loop control statements
Jaya Kumari
 
Decision statements
Decision statementsDecision statements
Decision statements
Jaya Kumari
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
ppd1961
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
Kumar
 
Scjp questions
Scjp questionsScjp questions
Scjp questions
roudhran
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
04a intro while
04a intro while04a intro while
04a intro while
hasfaa1017
 
Exception Handling
Exception HandlingException Handling
Exception Handling
backdoor
 
04 control structures 1
04 control structures 104 control structures 1
04 control structures 1
Jomel Penalba
 
Exception handling
Exception handlingException handling
Exception handling
Iblesoft
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
rajshreemuthiah
 

Similar to Module 5 : Statements & Exceptions (20)

C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
Loops
LoopsLoops
Loops
Kamran
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
06 Loops
06 Loops06 Loops
06 Loops
maznabili
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
06.Loops
06.Loops06.Loops
06.Loops
Intro C# Book
 
Flow Control and Exception Handling.pptx
Flow Control and Exception Handling.pptxFlow Control and Exception Handling.pptx
Flow Control and Exception Handling.pptx
Sheetal343198
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
PRN USM
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
guest5c8bd1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
Vijay A Raj
 
Java tut1
Java tut1Java tut1
Java tut1
Ajmal Khan
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
Abdul Aziz
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
msharshitha03s
 
C# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.pptC# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.ppt
Riannel Tecson
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
satvirsandhu9
 
exceptioninpython.pptx
exceptioninpython.pptxexceptioninpython.pptx
exceptioninpython.pptx
SulekhJangra
 
Introduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptxIntroduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Introduction to C++ programming language
Introduction to C++ programming languageIntroduction to C++ programming language
Introduction to C++ programming language
divyadhanwani67
 
Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage"
Rapita Systems Ltd
 
C Programming Lesson 3.pdf
C Programming Lesson 3.pdfC Programming Lesson 3.pdf
C Programming Lesson 3.pdf
Rajeev Mishra
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
Flow Control and Exception Handling.pptx
Flow Control and Exception Handling.pptxFlow Control and Exception Handling.pptx
Flow Control and Exception Handling.pptx
Sheetal343198
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
PRN USM
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
msharshitha03s
 
C# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.pptC# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.ppt
Riannel Tecson
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
satvirsandhu9
 
exceptioninpython.pptx
exceptioninpython.pptxexceptioninpython.pptx
exceptioninpython.pptx
SulekhJangra
 
Introduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptxIntroduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Introduction to C++ programming language
Introduction to C++ programming languageIntroduction to C++ programming language
Introduction to C++ programming language
divyadhanwani67
 
Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage"
Rapita Systems Ltd
 
C Programming Lesson 3.pdf
C Programming Lesson 3.pdfC Programming Lesson 3.pdf
C Programming Lesson 3.pdf
Rajeev Mishra
 

More from Prem Kumar Badri (20)

Module 15 attributes
Module 15 attributesModule 15 attributes
Module 15 attributes
Prem Kumar Badri
 
Module 14 properties and indexers
Module 14 properties and indexersModule 14 properties and indexers
Module 14 properties and indexers
Prem Kumar Badri
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scope
Prem Kumar Badri
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
Prem Kumar Badri
 
Module 11 : Inheritance
Module 11 : InheritanceModule 11 : Inheritance
Module 11 : Inheritance
Prem Kumar Badri
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
Prem Kumar Badri
 
Module 9 : using reference type variables
Module 9 : using reference type variablesModule 9 : using reference type variables
Module 9 : using reference type variables
Prem Kumar Badri
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and generics
Prem Kumar Badri
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
Prem Kumar Badri
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parameters
Prem Kumar Badri
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variables
Prem Kumar Badri
 
Module 2: Overview of c#
Module 2:  Overview of c#Module 2:  Overview of c#
Module 2: Overview of c#
Prem Kumar Badri
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET Platform
Prem Kumar Badri
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collection
Prem Kumar Badri
 
C# Multi threading
C# Multi threadingC# Multi threading
C# Multi threading
Prem Kumar Badri
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
Prem Kumar Badri
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
C# Generic collections
C# Generic collectionsC# Generic collections
C# Generic collections
Prem Kumar Badri
 
C# Global Assembly Cache
C# Global Assembly CacheC# Global Assembly Cache
C# Global Assembly Cache
Prem Kumar Badri
 
Module 14 properties and indexers
Module 14 properties and indexersModule 14 properties and indexers
Module 14 properties and indexers
Prem Kumar Badri
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scope
Prem Kumar Badri
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
Prem Kumar Badri
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
Prem Kumar Badri
 
Module 9 : using reference type variables
Module 9 : using reference type variablesModule 9 : using reference type variables
Module 9 : using reference type variables
Prem Kumar Badri
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and generics
Prem Kumar Badri
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parameters
Prem Kumar Badri
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variables
Prem Kumar Badri
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET Platform
Prem Kumar Badri
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collection
Prem Kumar Badri
 

Recently uploaded (20)

Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 

Module 5 : Statements & Exceptions

  • 2. Overview  Introduction to Statements  Using Selection Statements  Using Iteration Statements  Using Jump Statements  Handling Basic Exceptions  Raising Exceptions
  • 3. Introduction to Statements  Statement Blocks  Types of Statements
  • 4. Statement Blocks  Use braces As block delimiters { // code } { int i; ... { int i; ... } } { int i; ... } ... { int i; ... }  A block and its parent block cannot have a variable with the same name  Sibling blocks can have variables with the same name
  • 5. Types of Statements Selection Statements The if and switch statements Iteration Statements The while, do, for, and foreach statements Jump Statements The goto, break, and continue statements
  • 6. Using Selection Statements  The if Statement  Cascading if Statements  The switch Statement  Quiz: Spot the Bugs
  • 7. The if Statement  Syntax:  No implicit conversion from int to bool int x; ... if (x) ... // Must be if (x != 0) in C# if (x = 0) ... // Must be if (x == 0) in C# if ( Boolean-expression ) first-embedded-statement else second-embedded-statement
  • 8. Cascading if Statements enum Suit { Clubs, Hearts, Diamonds, Spades } Suit trumps = Suit.Hearts; if (trumps == Suit.Clubs) color = "Black"; else if (trumps == Suit.Hearts) color = "Red"; else if (trumps == Suit.Diamonds) color = "Red"; else color = "Black";
  • 9. The switch Statement  Use switch statements for multiple case blocks  Use break statements to ensure that no fall through occurs switch (trumps) { case Suit.Clubs : case Suit.Spades : color = "Black"; break; case Suit.Hearts : case Suit.Diamonds : color = "Red"; break; default: color = "ERROR"; break; }
  • 10. Quiz: Spot the Bugs if number % 2 == 0 ... if (percent < 0) || (percent > 100) ... if (minute == 60); minute = 0; switch (trumps) { case Suit.Clubs, Suit.Spades : color = "Black"; case Suit.Hearts, Suit.Diamonds : color = "Red"; defualt : ... } 2 3 4 1
  • 11. Using Iteration Statements  The while Statement  The do Statement  The for Statement  The foreach Statement  Quiz: Spot the Bugs
  • 12. The while Statement  Execute embedded statements based on Boolean value  Evaluate Boolean expression at beginning of loop  Execute embedded statements while Boolean value Is True int i = 0; while (i < 10) { Console.WriteLine(i); i++; } 0 1 2 3 4 5 6 7 8 9
  • 13. The do Statement  Execute embedded statements based on Boolean value  Evaluate Boolean expression at end of loop  Execute embedded statements while Boolean value Is True int i = 0; do { Console.WriteLine(i); i++; } while (i < 10); 0 1 2 3 4 5 6 7 8 9
  • 14. The for Statement  Place update information at the start of the loop  Variables in a for block are scoped only within the block  A for loop can iterate over several values for (int i = 0; i < 10; i++) { Console.WriteLine(i); } 0 1 2 3 4 5 6 7 8 9 for (int i = 0; i < 10; i++) Console.WriteLine(i); Console.WriteLine(i); // Error: i is no longer in scope for (int i = 0, j = 0; ... ; i++, j++)
  • 15. The foreach Statement  Choose the type and name of the iteration variable  Execute embedded statements for each element of the collection class ArrayList numbers = new ArrayList( ); for (int i = 0; i < 10; i++ ) { numbers.Add(i); } foreach (int number in numbers) { Console.WriteLine(number); } 0 1 2 3 4 5 6 7 8 9
  • 16. Quiz: Spot the Bugs for (int i = 0, i < 10, i++) Console.WriteLine(i); int i = 0; while (i < 10) Console.WriteLine(i); for (int i = 0; i >= 10; i++) Console.WriteLine(i); do ... string line = Console.ReadLine( ); guess = int.Parse(line); while (guess != answer); 2 3 4 1
  • 17. Using Jump Statements  The goto Statement  The break and continue Statements
  • 18. The goto Statement  Flow of control transferred to a labeled statement  Can easily result in obscure “spaghetti” code if (number % 2 == 0) goto Even; Console.WriteLine("odd"); goto End; Even: Console.WriteLine("even"); End:;
  • 19. The break and continue Statements  The break statement jumps out of an iteration  The continue statement jumps to the next iteration int i = 0; while (true) { Console.WriteLine(i); i++; if (i < 10) continue; else break; }
  • 20. Handling Basic Exceptions  Why Use Exceptions?  Exception Objects  Using try and catch Blocks  Multiple catch Blocks
  • 21. Why Use Exceptions?  Traditional procedural error handling is cumbersome int errorCode = 0; FileInfo source = new FileInfo("code.cs"); if (errorCode == -1) goto Failed; int length = (int)source.Length; if (errorCode == -2) goto Failed; char[] contents = new char[length]; if (errorCode == -3) goto Failed; // Succeeded ... Failed: ... Error handling Core program logic
  • 23. Using try and catch Blocks  Object-oriented solution to error handling  Put the normal code in a try block  Handle the exceptions in a separate catch block try { Console.WriteLine("Enter a number"); int i = int.Parse(Console.ReadLine()); } catch (OverflowException caught) { Console.WriteLine(caught); } Error handling Program logic
  • 24. Multiple catch Blocks  Each catch block catches one class of exception  A try block can have one general catch block  A try block is not allowed to catch a class that is derived from a class caught in an earlier catch block try { Console.WriteLine("Enter first number"); int i = int.Parse(Console.ReadLine()); Console.WriteLine("Enter second number"); int j = int.Parse(Console.ReadLine()); int k = i / j; } catch (OverflowException caught) {…} catch (DivideByZeroException caught) {…}
  • 25. Raising Exceptions  The throw Statement  The finally Clause  Checking for Arithmetic Overflow  Guidelines for Handling Exceptions
  • 26. The throw Statement  Throw an appropriate exception  Give the exception a meaningful message throw expression ; if (minute < 1 || minute >= 60) { throw new InvalidTimeException(minute + " is not a valid minute"); // !! Not reached !! }
  • 27. The finally Clause  All of the statements in a finally block are always executed Monitor.Enter(x); try { ... } finally { Monitor.Exit(x); } Any catch blocks are optional
  • 28. Checking for Arithmetic Overflow  By default, arithmetic overflow is not checked  A checked statement turns overflow checking on checked { int number = int.MaxValue; Console.WriteLine(++number); } unchecked { int number = int.MaxValue; Console.WriteLine(++number); } -2147483648 OverflowException Exception object is thrown. WriteLine is not executed. MaxValue + 1 is negative?
  • 29. Guidelines for Handling Exceptions  Throwing  Avoid exceptions for normal or expected cases  Never create and throw objects of class Exception  Include a description string in an Exception object  Throw objects of the most specific class possible  Catching  Arrange catch blocks from specific to general  Do not let exceptions drop off Main
  • 30. Custom Exception  Custom Exception class is derived from System.Exception or one of the other common base exception classes.  Provides a type safe mechanism for a developer to detect an error condition.  Add situation specific data to an exception.  No existing exception adequately described my problem.
  • 31. Review  Introduction to Statements  Using Selection Statements  Using Iteration Statements  Using Jump Statements  Handling Basic Exceptions  Raising Exceptions
  翻译: