Visual Programming
Department of CSE, QUEST
Dr. Irfana Memon
Dr. Irfana Memon
Department of CSE, QUEST
[Link]
C# Programming
language constructs,
Creating Methods,
Department of CSE, QUEST
Dr. Irfana Memon
Invoking Methods,
Overloaded Methods,
and Handling
Exceptions 2
C# Programming Language
Constructs
We will discuss the commonly used programming constructs
Department of CSE, QUEST
Dr. Irfana Memon
like if, if-then, if-else, nested if, Switch-case, for, while, and do-
while loops. Let's analyze each of them in detail.
3
C# Conditional statements
• if construct
• if...else construct
• if...else if construct
• Nested if construct
Department of CSE, QUEST
Dr. Irfana Memon
4
C# Conditional statements
• if statement construct:
The if statement allows you to test whether a specific
condition is met or not. The syntax for declaring if statement
as follows:
Department of CSE, QUEST
Dr. Irfana Memon
if (boolean-expression)
{
// statements executed if boolean-expression is true
}
5
C# Conditional statements
• if statement construct:
The if statement allows you to
test whether a specific condition
is met or not. The syntax for
declaring if statement as follows:
Department of CSE, QUEST
Dr. Irfana Memon
if (boolean-expression)
{
// statements executed if
boolean-expression is true
}
6
C# Conditional statements
• if...else construct
if (boolean-expression)
{
// statements executed if boolean- expression is true
}
Department of CSE, QUEST
Dr. Irfana Memon
else
{
// statements executed if boolean- expression is false
}
7
C# Conditional statements
• if...else construct using System;
class Fund
if (boolean-expression) {
{ public static void Main()
// statements executed if boolean- {
expression is true int x1 = 50;
} int x2 = 100;
else if(x1>x2)
Department of CSE, QUEST
Dr. Irfana Memon
{ {
// statements executed if boolean- [Link]("X1 is greater");
expression is false {
} else
{
[Link]("X2 is greater")
}
}
}
8
C# Conditional statements
• if...else if Construct
You can also combine the elseif statement to test multiple
conditions.
if (boolean-expression)
{
Department of CSE, QUEST
Dr. Irfana Memon
// statements executed if boolean- expression is true
}
else if (boolean-expression)
{
// statements executed if boolean- expression is true
}
else
{
// statements executed if boolean- expression is false
} 9
C# Conditional statements
• if...else if Construct
For example, we are checking int x=10, y=20,z=22;
if (x >y)
the highest number of three {
as follows: [Link]("x is Highest");
}
Department of CSE, QUEST
Dr. Irfana Memon
elseif (x > z)
{
[Link]("x is Highest");
}
elseif (y > z)
{
[Link]("y is Highest");
}
else
{ 10
[Link]("z is Highest");
}
C# Conditional statements
• Nested if Construct
In c#, Nested if-else statements or conditions are useful to include
one if…else statementwithin another if…else statement to test one
condition followed by another condition.
if (condition)
{
if (nested_condition_1)
{
Department of CSE, QUEST
Dr. Irfana Memon
// Statements to Execute
}
else
{
// Statements to Execute
}
}
else
{
if (nested_condition_2)
{
// Statements to Execute
} 11
else
{
// Statements to Execute
}}
Exercise
1. Write a C# program that prompts the user to input three integer values and
find the greatest value of the three values.
Department of CSE, QUEST
Dr. Irfana Memon
12
Exercise
1. Write a C# program that prompts the user to input three integer values and
find the greatest value of the three values.
else if (y > z) [Link]("The greatest value
using System; is:{0}.",y);
namespace Csharp_exercises else [Link]("The greatest value is:{0}.",z);
{ [Link]();
class Program }
{ }
static void Main(string[] args)
Department of CSE, QUEST
Dr. Irfana Memon
}
{
int x,y,z;
[Link]("Enter value 1:");
x = [Link]([Link]());
[Link]("Enter value 2:");
y = [Link]([Link]());
[Link]("Enter value 3:");
z = [Link]([Link]());
if (x > y) 13
if (x > z) [Link]("The greatest value is:{0}.",x);
else [Link]("The greatest value is:{0}.", z);
Exercise
2. Write a C# program that determines a student’s grade.
The program will read three types of scores(quiz score, mid-term score, and final
score) and determine the grade based on the following rules:-
-if the average score =90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F
Department of CSE, QUEST
Dr. Irfana Memon
14
Exercise
2. Write a C# program that determines a student’s grade.
The program will read three types of scores(quiz score, mid-term score, and final
score) and determine the grade based on the following rules:-
-if the average score =90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F mid_score = [Link]([Link]());
[Link]("Enter final score:");
using System; final_score = [Link]([Link]());
Department of CSE, QUEST
Dr. Irfana Memon
namespace Csharp_exercises avg = (quiz_score +mid_score+final_score) / 3;
{
class Program if (avg >= 90) [Link]("Grade A");
{ else if ((avg >= 70) && (avg < 90))
static void Main(string[] args) [Link]("Grade B");
{ else if ((avg >= 50) && (avg < 70))
float quiz_score; [Link]("Grade C");
float mid_score; else if (avg < 50) [Link]("Grade F");
float final_score; else [Link]("Invalid input");
float avg; [Link](); 15
[Link]("Enter quiz score:"); }
quiz_score=[Link]([Link]()); }
[Link]("Enter mid-term score:"); }
C# switch construct
switch (variable/expression)
{
case value1:
// Statements executed if expression(or variable) = value1
break;
case value2:
Department of CSE, QUEST
Dr. Irfana Memon
// Statements executed if expression(or variable) = value1
break;
default:
// Statements executed if no case matches
}
16
C# switch construct
using System;
class Switchexample
{
public static void Main()
{
int wday = 2;
switch(wday)
Department of CSE, QUEST
Dr. Irfana Memon
{
case 1: [Link]("Monday");
break;
case 2: [Link]("Tuesday");
break;
}
} 17
}
C# switch construct
Write C# program for three basic arithmetic operation(Addition, Subtraction,
Multiplication) of two numbers.
Department of CSE, QUEST
Dr. Irfana Memon
18
C# switch construct
Write C# program for three basic arithmetic operation(Addition, Subtraction,
Multiplication) of two numbers.
Staticvoid Main(string[] args)
{
int x,y;
[Link]("Enter two Integers");
x = [Link]([Link]());
y = [Link]([Link]()); [Link]("Operations\n-------------------------------\n");
[Link]("1= Addition\n2= Subtraction \n3= Multiplication");
[Link]("Operations\n-----------------------------------\n");
Department of CSE, QUEST
Dr. Irfana Memon
[Link]("Enter the operation code::");
int op = [Link]([Link]());
switch (op)
{
case 1: [Link]("Add=" + (x + y));
break;
case 2: [Link]("Subs=" + (x - y));
break;
case 3: [Link]("Multiply=" + (x * y));
break;
default:
[Link]("wrong choice");
break; 19
}
[Link]();
}
Exercise
1. C# Program to check whether an integer entered by the user is
odd or even.
Department of CSE, QUEST
Dr. Irfana Memon
20
Exercise
1. C# Program to check whether an integer entered by the user is
odd or even.
Department of CSE, QUEST
Dr. Irfana Memon
21
Exercise
2. C# program to check leap year using conditional Operator.
Department of CSE, QUEST
Dr. Irfana Memon
22
Exercise
2. C# program to check leap year using conditional Operator.
Department of CSE, QUEST
Dr. Irfana Memon
23
Exercise
3. C# Program to Check whether an alphabet is a vowel or not.
Department of CSE, QUEST
Dr. Irfana Memon
24
Exercise
3. C# Program to Check whether an alphabet is a vowel or not.
Department of CSE, QUEST
Dr. Irfana Memon
25
Exercise
4. C# program to check number is positive, negative or zero.
Dr. Irfana Memon
Department of CSE, QUEST
26
Exercise
5. C# program to determine a candidate's age is eligible for casting
the vote or not.
Department of CSE, QUEST
Dr. Irfana Memon
27
Exercise
5. C# program to determine a candidate's age is eligible for casting
the vote or not.
Department of CSE, QUEST
Dr. Irfana Memon
28
Exercise
6. C# program to check whether a triangle can be formed by the
given value for the angles.
Department of CSE, QUEST
Dr. Irfana Memon
29
Exercise
6. C# program to check whether a triangle can be formed by the
given value for the angles.
Department of CSE, QUEST
Dr. Irfana Memon
30
Wish You Good Luck
Dr. Irfana Memon
31
Department of CSE, QUEST