Sie sind auf Seite 1von 33

OOP

C#
Programming
LESSON 3

ROMMEL L. DORIN
Contents

3 Ways of Converting String

Decision Structure Intro

Operators

Types of Decision Structure


3 ways to convert String

• Using the Parse Method


• Using the TryParse Method
• Using the Convert Method
from (System.Convert class)
3 ways to convert String

• Convert Method
Convert.ToInt32() method is only
that Convert.ToInt32() method
always accept the null value return
it. Hence the output is Zero.
3 ways to convert String

• Parse Method
Int32.Parse() method to convert the
length from string to int (32-bit
signed integer equivalent).
3 ways to convert String

• Parse Method
When we have a value greater
than Integer: If you assign the
value big value to string then it will
through an OverflowException as int
data type can’t handle the large
values (It very much depends upon
the range of data types).
3 ways to convert String

• Parse Method
When we have Empty String: If
you keep string blank then it will
through an exception System
Format Exception as the input is
blank.
3 ways to convert String
• TryParse Method
The difference in Parse() and TryParse()
method is only that TryParse() method
always return the value it will never
throw an exception.
3 ways to convert String

• TryParse Method
First of all it validate your string. If your
string is integer it returns True else False.

int.TryParse contain two arguments first


is string and another is int(out type). If
the input string is integer it returns 2nd
arguments(out type int). Else it returns
first argument(string).
3 ways to convert String

• TryParse Method
Decision Structure

Programs that need


the computer to
make a decision
require the use of the
selection structure
(also called the
decision structure)
Decision Structure
 The Decision structure indicates that a decision (based on
some condition) needs to be made, followed by an
appropriate action derived from that decision.
Conditional Statement

 Selection structure uses conditional statement because it


executes only if the value of the relational expression is
true. If the value of the relational expression is false,
then the conditional statement is not executed.
Decision structures
Relational Operators
A relational operator determines whether a specific
relationship exists between two values.
Logical Operators
Logical operators connect two or more
relational expressions into one or reverse
the logic of an expression.
Logical Operators
Logical AND operator
It takes two Boolean expressions as operands
and creates a Boolean expression that is true
only when both subexpressions are true.
Logical Operators
Logical OR operator
It takes two Boolean expressions as operands
and creates a Boolean expression that is true
when either of the subexpressions is true.
Logical Operators
Logical NOT operator
It is a unary operator that takes a Boolean
expression as its operand and reverses its logical
value.
Logical Operators
Precedence of Logical Operators

! Not
&& AND
|| OR
Precedence of Operators

Precedence of Logical Operators


Review
Review

 If a = 2, b = 4, and c = 6, indicate whether each of the


following conditions is true or false:
Review

If a = 2, b = 4, j=true, y=false and c = 6,


indicate whether each of the following
conditions is true or false:

A) a <= ++b && y


B) y || b+a!= 6 && !j
C) c-b >= a && !y
D) !(a>b) || (!j && (c+4 >=10))
Decision structures

A decision structure can be based


on:

(A)Single Alternative (one code


path)

(A)Dual Alternative (two code path)


Decision structures
(A)Single Alternative (one code path)
Decision structures
(B) Dual Alternative (two code path)
Decision structures
(B) Dual Alternative (two code path)

N Y

Print B Print A
Practice

Problem #1: A bookstore owner


wants you to create a program
(BOGOHO) that will accept 2 price of
item and compare them, the one with
lowest price will be 50% off. Display
the total price to be paid by the
customer.
IPO
Input Process Output
Item Price 1 1. Ask the user to enter price of the first Total Price
Item Price 2 item
2. Read the user input (item price 1)
3. Ask the user to enter price of the
second item
4. Read the user input (item price 2)
5. Compute total price by comparing
item price 1 and item price 2. If item
price 1 is less than item price 2 get
the 50% off from item price 1 and add
it to item price 2 else get the 50% off
from item price 2 and add it to the
item price 1.
6. Display total price.
Variable Table

Data Type Variable Name Description


Float itemPrice1 Price of the 1st item

Float itemPrice2 Price of the 2nd item

Float totalPrice Total amount to be paid


Flowchart A
START
itemPrice1
itemPrice1, <
itemPrice2, itemPrice2
totalPrice

Display “Enter totalPrice=itemPrice totalPrice=itemPrice


price of 1st 1 + (itemPrice2 * .5) 2 + (itemPrice1 * .5)
item”

Read
itemPrice1

Display “Enter
price of 2nd Display
item” totalPrice

Read
itemPrice2
STOP

A
Take Home Activity

Create the Csharp program using


the IPO and Flowchart.
Upload your file/program in our
Google Classroom.
File name: LastName_Bogoho.cs

Das könnte Ihnen auch gefallen