Visual C# Visual C#: Programming Language 1 Using

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Programming Language 1

using
Visual C# Visual C#
Lesson 07
Making Decisions
Prepared by: Mr. Dan Chua for SISC
3 Categories of Programming Constructs
1. Simple Sequence
- Executes program one line at a time. From the top until it reaches the last
statement of the Main() method.
2. Selection Statement (Branching)
is used for decision making and allows your program statements to deviate
from the sequential path and perform different statements based on the value
of the expression.
if...else
3. Iterations (Looping)
- allows you to write instructions that can be repeated
if...else
switch...case
ternary conditional operator
while / dowhile
for
foreach (for Arrays)
The If...Else Selection Statement
Boolean Result The test condition always result to True or False only.
ex. If (prelim grade is greater than 74) // true or false?
then (keep it up!) // If true
Else
(better study hard!) // If false
3 Types of Test Condition
1. Equality Operators
2. Relational Operators
3. Logical Operators
The If...Else Selection Statement
Equality Operators To test for equality using an assignment operator.
Symbol Meaning Example Result
== Equal (num== 25) True
!= Not Equal (num!= 25) False
Relational Operators To compare values of variables whether one variable is greater
than or less than the other
Symbol Meaning Example Result
> Greater Than (num> 25) True > Greater Than (num> 25) True
< Less Than (num< 25) False
>= Greater Than or Equal (num>= 25) True
<= Less Than or Equal (num <= 25) False
Logical Operators To test and evaluate the result of compound conditions
Symbol Meaning Example Result
&& AND (num1 >= 25 && num2 <= 50) True
|| OR (num1 >= 25 || num2 <= 50) False
The If...Else Selection Statement
One-Way If Statement
Syntax:
if (expression)
statement;
Ex.
if (num1 == 25)
Console.WriteLine("The Number is Twenty Five");
int score1 = 87;
int score2 = 75;
int score3 = 90;
float average;
average = (score1 + score2 + score3) / 3;
if (average > 75)
Console.WriteLine("Congratulations! You Passed...");
Console.WriteLine("End of Program...");
Console.ReadKey();
The If...Else Selection Statement
Two-Way If...Else Statement
Syntax:
if (expression)
statement1;
else
statement2;
Ex.
if (num1 >= 25)
Console.WriteLine(The number is higher or equal to 25...");
else
Console.WriteLine(The number is lower than 25"); Console.WriteLine(The number is lower than 25");
int score1 = 87;
int score2 = 75;
int score3 = 90;
float average;
average = (score1 + score2 + score3) / 3;
if (average > 75)
Console.WriteLine("Congratulations! You Passed...");
else
Console.WriteLine(Sorry, You have Failed...");
Console.WriteLine("End of Program...");
The If...Else Selection Statement
Nested If...Else Statement
Syntax:
if (expression1)
if (expression2)
statement1;
else
statement2;
else if (expression2)
statement2;
else
statement3;
Ex int num1 = 34;
int num2 = 56;
int num3 = 9;
if (num1 > num2)
if (num1 > num3)
Console.WriteLine("{0} is the highest", num1);
else
Console.WriteLine("{0} is the highest", num3);
else if (num2 > num3)
Console.WriteLine("{0} is the highest", num2);
else if (num3 > num1)
Console.WriteLine("{0} is the highest", num3);
Ternary Conditional Operator
Symbols to be used: ? and :
Example:
If ( a > b)
c = 0;
else
Converting to Ternary Operator:
c = (a > b) ? 0 : 1;
else
c = 1;
c = (a > b) ? 0 : 1;
If (x == y)
value = true;
else
value = false;
value = (x == y) ? true : false;
SwitchSelection Statement
switch statement is considered a multiple selection structure. It is also know as
case statement. The switch or case statement allows you to perform a large
number of alternatives based on the value of a single variable.
It is appropriate to variable with data type: int, char, or string
But cannot be used in: float, decimal or double
Syntax:
switch (expression)
{{
case value1: statement1;
break;
case value2: statement2;
break;
case value3: statement3;
break;
default: statement4;
break;
}
char choice; char cont = 'Y';
while (cont == 'Y')
{
try
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Option <R> - Red, "<B> - Blue, <Y> - Yellow," +
"<G> - Green, <M> - Magenta: ");
choice = char.Parse(Console.ReadLine().ToUpper());
switch (choice)
{
case 'R': Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\n\n\t\tRED");
break;
case 'B': Console.ForegroundColor = ConsoleColor.Blue;
catch (Exception e)
{
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Black;
case 'B': Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("\n\n\t\tBLUE");
break;
case 'Y': Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\n\n\t\tYELLOW");
break;
case 'G': Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\n\n\t\tGREEN");
break;
case 'M': Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\n\n\t\tMAGENTA");
break;
default: Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("\n\n\t\tWRONG Code!!!!"); break;
}
}
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("\n\n\t{0}",e.Message);
}
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\n\n\t\tContinue? [Y/N] : ");
cont = char.Parse(Console.ReadLine().ToUpper());
}
Machine Problem
(MP#03)
The use of ifelse statements Part 1 The use of ifelse statements Part 1
Note: Complete details will be posted in the Moodle
Machine Problem
(MP#04)
The use of ifelse statements Part 2 The use of ifelse statements Part 2
Note: Complete details will be posted in the Moodle
Machine Problem
(MP#05)
The use of nested ifelse statements
Part 3 Part 3
Note: Complete details will be posted in the Moodle
Machine Problem
(MP#06)
The use of
ifelse or switch selection statement Part 1 ifelse or switch selection statement Part 1
Note: Complete details will be posted in the Moodle
Machine Problem
(MP#07)
The use of
switch selection statement Part 2 switch selection statement Part 2
Note: Complete details will be posted in the Moodle
Machine Problem
(MP#08)
The use of
switch selection statement Part 3 switch selection statement Part 3
Note: Complete details will be posted in the Moodle
End of Lesson 07
Making Decisions
Prepared by: Mr. Dan Chua for SISC

You might also like