Professional Documents
Culture Documents
boolean v;
2
Comparison Operators
Java Mathematics Name Example Result
Operator Symbol (radius is 5)
< < less than < 0
radius false
<= ≤ less than or equal to <= 0
radius false
> > greater than radius > 0 true
>= ≥ greater than or equal to radius >= 0 true
== = equal to radius == 0 false
!= ≠ not equal to radius != 0 true
3
One-way if
Statements if (radius >= 0)
{
area = radius * radius * PI;
if (boolean-expression) Console.WriteLine("The
statement(s); area"
{}
+ " for the circle of radius "
} + radius + " is " + area);
false false
Boolean (radius >= 0)
Expression
true true
(A) (B)
4
Note
if (i > 0) {
if i > 0
{
Console.WriteLine("i is positive"); Console.WriteLine("i is positive");
} }
(b) Correct
(a) Wrong
if (i > 0) { if (i > 0)
Console.WriteLine("i is positive"); Equivalent Console.WriteLine("i is positive");
}
(a) (b)
5
Examples
6
Common
Errors
Adding a semicolon at the end of an if clause is a common
mistake.
if (radius >= 0); Wrong
{
area = radius*radius*PI;
Console.WriteLine(
"The area for the circle of radius "
+ radius + " is " + area);
}
7
Note
if (even == true) Equivalent if (even)
Console.WriteLine( Console.WriteLine(
"It is even."); "It is even.");
(a) (b)
8
The Two-way if Statement
if (boolean-expression)
{
statement(s)-for-the-true-case;
}
else
{
statement(s)-for-the-false-case;
}
true false
Boolean
Expression
Statement(s) for the true case Statement(s) for the false case
9
if-else Example
if (radius >= 0)
{ area = radius * * 3.14159;
radius
Console.WriteLine("The area for the “
+ “circle of + radius +
radius " " is " +
} area);
else
{ Console.WriteLine("Negative input");
}
10
Exercise
• Draw a flowchart
s and write a program
to read an integer and decides weather
it is even or odd.
12
Multi-Way if Statements
false
score >= 90
false
true score >=
80 fals
grade = 'A' true score >= e
70 fals
grade = rue score >= e
'B' 60
grade = true
'C'
grade = 'D'
grade =
'F'
Multi-Way if-else Statements
16
Example
17
Example
Console.WriteLine("Is " + number + " divisible by 2 and 3? "
+
((number % 2 == 0) && (number % 3 == 0)));
19
switch Statement
switch (switch-expression)
{
case value1: statement(s)1;
break;
case value2: statement(s)2;
break;
…
case valueN: statement(s)N;
break;
default: statement(s)-for-
default;
}
20
switch Statement
sta tu s is 0
C o m p ut e ta x fo r sin gle file rs b rea k
sta tu s is 1
C o m p ut e ta x fo r m a r ried join t ly o r q u a lifyi n g w i d o w (e r) b rea k
sta tu s is 2
C o m p ut e ta x fo r m a r ried fili n g sep a ra te ly b re a k
sta tu s is 3
C o m p ut e ta x fo r h e a d o f h o u se h old b rea k
d e fa u lt
D efa u lt a c tion s
switch
Statement
switch (status)
{
case 0: compute taxes for single
filers; break;
case 1: compute taxes for married file
jointly; break;
case 2: compute taxes for married file
separately; break;
case 3: compute taxes for head of
household; break;
default: Console.WriteLine("Errors: invalid
status");
System.exit(1); 22
switch statement -
Examples
1) Draw a flowchart and write a C# program to read the
name and score of a student and prints the name and
the grade. Use switch.
23
Conditional Operator
if (x > 0)
y = 1;
else
y = -1;
is
equivale
nt to
y = (x >
0) ? 1 :
-1;
24
(boolea
Conditional Operator
if (num % 2 == 0)
Console.WriteLine(num + “is even”);
else
Console.WriteLine(num + “is odd”);
Console.WriteLine( (n
um % 2 == 0)? num + “is even” :
num + “is odd”);
25
Problem: A Simple Math Learning
Tool
26
References
• C# Programming for Absolute Beginners;
Radek Vystavel.
• https://www.geeksforgeeks.org/csharp-progr
amming-language/
• https://docs.microsoft.com/en-us/dotnet/csh
arp/