Emerging Technologies (IT-3302)
Week 03
Usman Akbar
[Link]@[Link]
FB Group - SuperiorUniversity2016
Lecture Content
Last lecture review
C# - if Statement & if- else if
C# - Nested if Statements
C# - Switch Statement
Tasks & Questions
References
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 2
Last Lecture Review
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. C# has rich set of built-in
operators and provides the following type of operators
Arithmetic Operators
+ - * / % ++ --
Relational Operators
== != > < >= <=
Logical Operators
&& || !
Assignment Operators
= += -= *= /=
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 3
C# - if Statement
An if statement consists of a boolean expression followed by one or
more statements.
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is
true */
}
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 4
Example
using System;
namespace DecisionMaking {
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement
*/
if (a < 20) {
/* if condition is true then print the following */
[Link]("a is less than 20");
}
[Link]("value of a is : {0}", a);
[Link](); } }
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 5
C# if...else if (if-then-else if) Statement
if (boolean-expression-1)
{
// statements executed if boolean-expression-1 is
true
}
else if (boolean-expression-2)
{
// statements executed if boolean-expression-2 is
true
}
else if (boolean-expression-3)
{
// statements executed if boolean-expression-3 is
true
}
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 6
C# - Nested if Statements
if (boolean-expression-1)
{
// statements executed if boolean-expression-1 is true
??
if (boolean-expression-2)
{
// statements executed if boolean-expression-2 is
true
}
}
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 7
Class Task
Write Console C# Program which can
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 8
C# - Switch Statement
Syntax A switch statement allows a variable to be
switch(expression) { tested for equality against a list of values.
case expression : Each value is called a case, and the
statement(s); variable being switched on is checked for
break; /* optional */ each switch case.
case expression :
statement(s);
break; /* optional */
/* you can have any number of case
statements */
default : /* Optional */
statement(s);
}
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 9
Grades : Example
char grade = 'B';
switch (grade) {
case 'A':
[Link]("Excellent!");
break;
case 'B':
case 'C':
[Link]("Well done");
break;
case 'D':
[Link]("You passed");
break;
case 'F':
[Link]("Better try again");
break;
default:
[Link]("Invalid grade");
break;
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 10
There are following assignment operators supported by C#
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 11
Question
Questions….?
References
C# 6 for Programmers ( Chapter 03 )
[Link]
[Link]
Social Community Help ….!
[Link]
[Link]
[Link]
EMERGING TECHNOLOGIES ( USMAN AKBAR ) 13