0% found this document useful (0 votes)
13 views17 pages

CIT304 Chapter 6 Y24

The document discusses conditional statements in C# including comparison operators, if and if-else statements, logical operators, switch-case statements, and examples of using them. It also provides practice questions for readers.

Uploaded by

Bolaji Ogunlana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

CIT304 Chapter 6 Y24

The document discusses conditional statements in C# including comparison operators, if and if-else statements, logical operators, switch-case statements, and examples of using them. It also provides practice questions for readers.

Uploaded by

Bolaji Ogunlana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming with C Sharp (CIT 304)

Conditional Statement in C#

Adedoyin I. OYEBADE
[email protected]

Bowen University,
Iwo, Nigeria

May 15, 2023

OYEBADE A. (BU) CIT 304 May 15, 2023 1 / 17


Presentation Overview

1 Conditional Statement in C#
Comparison Operators
Conditional Statements ”if” and ”if-else”

OYEBADE A. (BU) CIT 304 May 15, 2023 2 / 17


Introduction C# Operators

1 Comparism Operators
• Comparism operators are used to pair integers, floating-point
numbers, characters, strings and other types
• Comparison operators can be used to compare expressions
such as two numbers, two numerical expressions, or a number
and a variable
• The result of the comparison is a Boolean value (true or false).

OYEBADE A. (BU) CIT 304 May 15, 2023 3 / 17


C# Operators and there symbols

Figure: C# Comparism Operators

OYEBADE A. (BU) CIT 304 May 15, 2023 4 / 17


C#: Classwork

1 Practice these questions


• int weight = 700;
Console.WriteLine(weight >= 500);
• char gender = ’m’;
Console.WriteLine(gender <= ’f’);
• double colorWaveLength = 1.630;
Console.WriteLine(colorWaveLength > 1.621);
• int a = 5;
int b = 7;
bool condition = (b > a) && (+b < a ∗ b);
Console.WriteLine(condition);
• Console.WriteLine(′ B ′ ==′ A′ + 1);

OYEBADE A. (BU) CIT 304 May 15, 2023 5 / 17


Comparison of Integers and Characters

1 This is done by comparing their binary representation in


memory
• Console.WriteLine(”char ′ a′ ==′ a′ ? ” + (′ a′ ==′ a′ ));

Console.WriteLine(”char ′ a′ ==′ b′ ? ” + (′ a′ ==′ b′ ));

Console.WriteLine(”5! = 6? ” + (5! = 6));

Console.WriteLine(”5.0 == 5L? ” + (5.0 == 5L));

Console.WriteLine(”true == false? ” + (true == false));

OYEBADE A. (BU) CIT 304 May 15, 2023 6 / 17


Conditional Logical Operators

1 Logical Operators
• Logical (Boolean) operators take Boolean values and return a
Boolean result (true or false).
• The basic Boolean operators are ”AND” (&&), ”OR” (||), ”exclusive
OR” ( ˆ) and logical negation (!).

Figure: C# Logical Operators Operation

OYEBADE A. (BU) CIT 304 May 15, 2023 7 / 17


Conditional Statements ”if” and ”if-else”

1 Conditional Statement ”if and ”if-else”


• This allow the implementation of programming logic
• They are refer to as the Control Statement
2 Conditional Statement ”if” format

if (Boolean expression)
{
Body of the conditional statement;
}

OYEBADE A. (BU) CIT 304 May 15, 2023 8 / 17


Conditional Statements ”if” and ”if-else”
1 Conditional Statement ”if Example
using System;
class Program {
static void Main(string[] args)
{
Console.WriteLine(”Enter two numbers.”);
Console.Write(”Enter first number: ”);
int firstNumber = int.Parse(Console.ReadLine());
Console.Write(”Enter second number: ”);
int secondNumber = int.Parse(Console.ReadLine());
int biggerNumber = firstNumber;
if (secondNumber > firstNumber)
{
biggerNumber = secondNumber;
}
Console.WriteLine(”The bigger number is: {0}”,
biggerNumber);
OYEBADE A. (BU) CIT 304 May 15, 2023 9 / 17
Conditional Statements ”if-else”

1 Conditional Statement ”if-else”


• If the ”if” conditional statement is ”True” the expression inside
the curly bracket is executed and the if conditional statement is
”False”, the expression inside the curly of else is executed

2 Conditional Statement ”if-else” format

if (Boolean expression)
{
Body of the conditional statement;
} else
{
Body of the else statement;
}

OYEBADE A. (BU) CIT 304 May 15, 2023 10 / 17


Conditional Statements ”if” and ”if-else”

1 Conditional Statement ”if-else Example


using System;
class Program {
static void Main()
{
int x = 2;
if (x > 3)
{
Console.WriteLine(”x is greater than 3”);
}
else
{
Console.WriteLine(”x is not greater than 3”);
}
}

OYEBADE A. (BU) CIT 304 May 15, 2023 11 / 17


Conditional Nested ”if” Statements
1 Nested ”if” Statements
• This is the testing of programming logic inside a programming
login
• It tested for a condition after the the first condition is TRUE
2 Conditional Statement Nested ”if” format
if (Boolean expression)
{
if (Boolean expression){
Body of the nested statement;
}
} else
{
if (Boolean expression){
Body of the nested statement;
}
}
OYEBADE A. (BU) CIT 304 May 15, 2023 12 / 17
Conditional Statements Nested ”if”

1 Conditional Statement ”Nested if-else Example


using System;
class Program {
static void Main()
{
int first = 5;
int second = 3;
if (first == second)
{
Console.WriteLine(”These two numbers are equal.”);
}
else
{
if (first > second)
{
Console.WriteLine(”The first number is greater.”);
}
else
{
Console.WriteLine(”The second number is greater.”);
}
}
}

OYEBADE A. (BU) CIT 304 May 15, 2023 13 / 17


Conditional Statement ”switch-case”
1 ”Switch-case” Statements
• The structure switch-case chooses which part of the
programming code to execute based on the calculated value of
a certain expression
2 The format of the structure for choosing an option is as follows
switch (integer selector)
{
case integer value 1:
statements;
break;
case integer value 2:
statements;
break;
// . . .
default:
statements;
break;
}OYEBADE A. (BU) CIT 304 May 15, 2023 14 / 17
Conditional Statement ”switch-case”

1 ”Switch-case” Statements Explain


• selector: This is an expression returning a resulting value that
can be compared, like a number or string
• switch: This is an operator that compares the result of the
selector to every value listed in the case labels in the body of the
switch structure
• default: If no match is found in the case label, the default is
executed if such exist

OYEBADE A. (BU) CIT 304 May 15, 2023 15 / 17


Conditional Statements ”switch-case” Example

1 Conditional Statements ”switch-case” Example


using System;
class Program {
static void Main()
{
int number = 6;
switch (number)
{
case 1:
case 4:
case 6:
case 8:
case 10:
Console.WriteLine(”The number is not prime!”);
break;
case 2:
case 3:
case 5:
case 7:
Console.WriteLine(”The number is prime!”);
break;
default:
Console.WriteLine(”Unknown number!”);
break;
}
}
}

OYEBADE A. (BU) CIT 304 May 15, 2023 16 / 17


Practice Questions
1 Thread and Task Management.Write a program that shows the
sign (+ or -) of the product of three real numbers, without
calculating it. Use a sequence of if operators
2 Write a program that finds the biggest of three integers, using
nested if statements.
3 Sort 3 real numbers in descending order. Use nested if
statements
4 Write a program that gets the coefficients a, b and c of a
quadratic equation: ax 2 + bx + c, calculates and prints its real
roots (if they exist). Quadratic equations may have 0, 1 or 2
real roots
5 Write a program that, depending on the user’s choice, inputs
int, double or string variable. If the variable is int or double, the
program increases it by 1. If the variable is a string, the
program appends ”*” at the end. Print the result at the
console. Use switch statement.
OYEBADE A. (BU) CIT 304 May 15, 2023 17 / 17

You might also like