Module: Statements and Exceptions
Contents
Introduction to Statements
Using Selection Statements
Using Iteration Statements
Using Jump Statements
Lab 4.1: Using Statements
Handling Basic Exceptions
Raising Exceptions
Lab 4.2: Using Exceptions
Introduction to
Statements
Statements are grouped in the C# language
A program consists of a sequence of statements. At run
time, these statements are executed one after the other,
as they appear in the program, from left to right and from
.top to bottom
Grouping Statements into Blocks
A group of statements enclosed between braces
is referred to as a block. A block can contain a
single statement or another block that is
.nested within it . Each block defines a scope
A variable that is declared in a block is called a
. local variable
The scope of a local variable extends from its
declaration to the
.right brace that ends its enclosing block
Algorithms
Any computing problem can be solved by
executing a series of actions in a specific
.order
A procedure for solving a problem in terms of
the actions to be executed. 1
the order in which these actions are to be. 2
executed
is called an algorithm
Types of Statements
Selection Statements
The if and switch statements
Iteration Statements looping statements
The while, do, for, and for each statements
Jump Statements
The go to, break, and continue statements
Selection Statements
The if and switch statements are known as
. selection statements
They make choices based on the value of
expressions and selectively execute
statements
based on those choices
Single IF
If students grade is
greater
than or equal to 60
Print Passed
if ( studentGrade >= 60 )
;[Link]( "Passed" )
If/Else
If students grade is
greater than or equal to
60 Print Passed
Else Print Failed
if ( studentGrade >= 60 )
;[Link]( "Passed" )
else
;[Link]( "Failed" )
Single IF
If/Else
Nesting if
Statements
If students grade is
greater than or equal
Print A
Else
If students grade is
greater than or equal
Print B
Else
If students grade is
greater than or equal
Print C
Else
If students grade is
greater than or equal
Print D
Else
Print F
to 90
to 80
to 70
to 60
if ( studentGrade >= 90 )
;[Link]( "A" )
else
if ( studentGrade >= 80 )
;[Link]( "B" )
else
if ( studentGrade >= 70 )
;[Link]( "C" )
else
if ( studentGrade >= 60 )
;[Link]( "D" )
else
;[Link]( "F" )
switch Multiple-Selection Structure
}switch (var)
:case a
;_______________
;break
:case b
;_______________
;break
:case c
;_______________
;break
:default
;`_______________
;break
}
Using goto in switch Statements goto
;case c
Iteration Statements
The while, do, for, and foreach statements
.are known as iteration statements
You use them to perform operations while
.a specific condition is true
The while Statement
Execute embedded statements while
Boolean value Is True
;int i = 0
while (i < 10)
}
;[Link](i)
++;i
{
9876543210
do/while Repetition
Structure
;int counter = 1
do
}
[Link]( co
;unter )
++;counter
}
while ( counter <=
;5 )
do
}
statement
;while ( condition )}
For Repetition Structure
Foreach Statement
Jump Statements
go to, break, and continue statements
For ( int x=0 ; x<10 ; x++ )
If ( x==5 )}
}
;____#___
;[Link](x)
}
}
4321
Break
Continue
98764321
END
THANK YOU