0% found this document useful (0 votes)
64 views

Lecture 5 - Conditional Statements

This document discusses visual programming and C# programming language constructs such as conditional statements like if, if-else, if-else if, nested if, and switch-case statements. It provides examples of using these constructs to evaluate boolean expressions and control program flow. The document also includes exercises for students to write programs to find the greatest of three numbers and determine a student's grade based on their average score.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Lecture 5 - Conditional Statements

This document discusses visual programming and C# programming language constructs such as conditional statements like if, if-else, if-else if, nested if, and switch-case statements. It provides examples of using these constructs to evaluate boolean expressions and control program flow. The document also includes exercises for students to write programs to find the greatest of three numbers and determine a student's grade based on their average score.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Visual Programming

Department of CSE, QUEST


Dr. Irfana Memon
Dr. Irfana Memon
Department of CSE, QUEST

https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
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- Console.WriteLine("X1 is greater");
expression is false {
} else
{
Console.WriteLine("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: Console.WriteLine("x is Highest");
}

Department of CSE, QUEST


Dr. Irfana Memon
elseif (x > z)
{
Console.WriteLine("x is Highest");
}
elseif (y > z)
{
Console.WriteLine("y is Highest");
}
else
{ 10
Console.WriteLine("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) Console.Write("The greatest value
using System; is:{0}.",y);
namespace Csharp_exercises else Console.Write("The greatest value is:{0}.",z);
{ Console.ReadLine();
class Program }
{ }
static void Main(string[] args)

Department of CSE, QUEST


Dr. Irfana Memon
}
{
int x,y,z;
Console.Write("Enter value 1:");
x = int.Parse(Console.ReadLine());
Console.Write("Enter value 2:");
y = int.Parse(Console.ReadLine());
Console.Write("Enter value 3:");
z = int.Parse(Console.ReadLine());
if (x > y) 13
if (x > z) Console.Write("The greatest value is:{0}.",x);
else Console.Write("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 = float.Parse(Console.ReadLine());
Console.Write("Enter final score:");
using System; final_score = float.Parse(Console.ReadLine());

Department of CSE, QUEST


Dr. Irfana Memon
namespace Csharp_exercises avg = (quiz_score +mid_score+final_score) / 3;
{
class Program if (avg >= 90) Console.WriteLine("Grade A");
{ else if ((avg >= 70) && (avg < 90))
static void Main(string[] args) Console.WriteLine("Grade B");
{ else if ((avg >= 50) && (avg < 70))
float quiz_score; Console.WriteLine("Grade C");
float mid_score; else if (avg < 50) Console.WriteLine("Grade F");
float final_score; else Console.WriteLine("Invalid input");
float avg; Console.ReadLine(); 15
Console.Write("Enter quiz score:"); }
quiz_score=float.Parse(Console.ReadLine()); }
Console.Write("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: Console.WriteLine("Monday");
break;
case 2: Console.WriteLine("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;
Console.WriteLine("Enter two Integers");
x = int.Parse(Console.ReadLine());
y = int.Parse(Console.ReadLine()); Console.WriteLine("Operations\n-------------------------------\n");
Console.WriteLine("1= Addition\n2= Subtraction \n3= Multiplication");
Console.WriteLine("Operations\n-----------------------------------\n");

Department of CSE, QUEST


Dr. Irfana Memon
Console.Write("Enter the operation code::");
int op = int.Parse(Console.ReadLine());
switch (op)
{
case 1: Console.WriteLine("Add=" + (x + y));
break;
case 2: Console.WriteLine("Subs=" + (x - y));
break;
case 3: Console.WriteLine("Multiply=" + (x * y));
break;
default:
Console.WriteLine("wrong choice");
break; 19
}
Console.ReadKey();
}
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

You might also like