Programming1 Lecture Presentations
Programming1 Lecture Presentations
• Generic
• General
• does not necessarily always mean "program" or "application" (e.g.
a software "library" or "framework" is not a "program" or
"application", but are used to facilitate the functional
requirements of "programs" or "applications").
Why do programmers make computer
programs?
• To simplify task
• Provide accurate result
• Aides in facilitating day to day activities
LLL vs HLL
• Web languages
• Software languages
• The different generations of languages
• Procedure oriented programming
• Object oriented programming
Web Languages
• HTML
• XML
• JAVASCRIPT
• VBSCRIPT
• PHP
• ASP
• JAVA
Software Languages
• C
• C++
• Visual Basic
• JAVA
The different generations of languages
• Cookbook
• Recipe Booklet
• Choreography
Notation Meaning
: comparison
& logical and
Y yes
N no
EOF End of File
Relational Test Operators
Notation Meaning
> is greater than
< is less than
<= or =< is less than or equal to
>= or => is greater than or equal to
<> or >< is not equal to
= is equal to
Arithmetic Operators
Notations Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
() Grouping
** or ^ Exponentiation
Precedence of the Operators
1. Grouping ()
2. Exponentiation
3. Multiplication or division
4. Addition or subtraction
When operators are all of equal priority, the computer
evaluate the expression from left to right.
When operators are of different priorities, the computer
performs those operations with higher priorities first.
Operations enclosed in parentheses will take place before other
operations. Note (if there are multiple parentheses, the innermost
parenthesis will be evaluated first).
Valid or Invalid?
Example #2.1
1+-2
Example #2.2
3/-2
Example #2.3
2L / 3G
Answer?
( 2 * L ) / (3 * G )
Sample Problem #4
2. Test for Limit Conditions – before logic flow gets out of the loop, a
loop – terminating condition must first be satisfied. The process of testing
is usually found either at the beginning or at the end of a loop.
Draw a flowchart which will read and print the names and individual
scores of 40 students for a particular examination. Determine their
average and print it out.
The algorithm for this particular requirement
could be listed as follows:
Find the amount to charge people of varying ages for a food ticket.
When the person is under 16, the charge is Php 7.00, when the
person is 65 or over, the charge is Php 5.00, all others are charged
Php 10.00. The conditions are the following:
Age Charge
<16 7
>=16 and <65 10
>=65 5
Sample Problem #12
Draw a flowchart that would count and display all the even and odd
numbers from 1 to 10.
“God has perfect timing: never early, never late,
it takes a little patience and a whole lot of
faith, but it’s worth the wait”
CHAPTER 3
• Namespace declaration
• A class
• Class methods
• Class attributes
• A Main method
• Statements & Expressions
• Comments
The C# Code
using System;
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
Line #1: using System;
Line #2: class HelloWorld
Line #3: {
Line #4: static void Main(string[] args)
Line #5: {
Line #6: /* my first program in C# */
Line #7: Console.WriteLine("Hello World");
Line #8: Console.ReadKey();
Line #9: }
Line #10: }
Line #1: using System;
/*
This program demonstrates
The basic syntax of C# programming
Language
*/
WriteLine is a method
of the Console class
defined in the System
namespace. This
statement causes the
message "Hello, World!"
to be displayed on the
screen.
Line #8: Console.ReadKey();
• C# is case sensitive.
• All statements and expression must end with a semicolon (;).
• The program execution starts at the Main method.
Identifiers
An identifier is a name used to identify a
class, variable, function, or any other user-
RULE #1: defined item.
NEVER EVER
USE THEM AS IDENTIFIERS!
C# Keywords
abstract As Base bool break byte case
catch Char checked class const continue decimal
default delegate do double else enum event
explicit extern false finally fixed float for
foreach Goto if implicit in in (generic int
modifier)
interface internal is lock long namespace new
null object operator out out (generic override params
modifier)
Computer’s Memory
sAge
sname
num1
deptName
mTime
Note: Every location has a
name and characteristic, in
short, a variable has a name
and data type
Rules in Naming Variables
• Abc
• abc123
• bahaykubokahitmunti
• walang_forever
• sum
• average
Valid or Invalid?
Number
String
Signed Unsigned
Empty
Not empty
(-) (+) Or
Zero-length
“ISU”
“Joe”
“”
1 -60 “I love CCSICT”
Both 2 -98.2
5.0 -8
Available Datatypes in C#
Type Represents Range Default Value
== Checks if the values of two operands are equal or not, if (A == B) is not true.
yes then condition becomes true.
!= Checks if the values of two operands are equal or not, if (A != B) is true. Let
values are not equal then condition becomes true.
> Checks if the value of left operand is greater than the (A > B) is not true. A = 10
value of right operand, if yes then condition becomes
true. B = 20
< Checks if the value of left operand is less than the value (A < B) is true.
of right operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or (A >= B) is not true.
equal to the value of right operand, if yes then
condition becomes true.
<= Checks if the value of left operand is less than or equal (A <= B) is true.
to the value of right operand, if yes then condition
becomes true.
Logical Operators
= Simple assignment operator, Assigns values from right side operands to C = A + B will assign value of A + B into
left side operand C
+= Add AND assignment operator, It adds right operand to the left C += A is equivalent to C = C + A
operand and assign the result to left operand
-= Subtract AND assignment operator, It subtracts right operand from the C -= A is equivalent to C = C - A
left operand and assign the result to left operand
*= Multiply AND assignment operator, It multiplies right operand with the C *= A is equivalent to C = C * A
left operand and assign the result to left operand
/= Divide AND assignment operator, It divides left operand with the right C /= A is equivalent to C = C / A
operand and assign the result to left operand
%= Modulus AND assignment operator, It takes modulus using two C %= A is equivalent to C = C % A
operands and assign the result to left operand
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
Sample Problem #16
Create a C# program that would get and display the sum of two
integer numbers given by the user.
Solution to Problem #16
using System;
class my_app
{
static void Main(string[] args)
{
string Num1 = "";
string Num2 = "";
int Sum = 0;
Console.WriteLine("The sum of " + Num1 + " and " + Num2 + " is: " + Sum);
Console.ReadKey();
}
}
Sample Problem #17
Create a C# program that would get and display the quotient of two
numbers given by the user.
Solution to Problem #17
using System;
class my_app
{
static void Main(string[] args)
{
string Num1 = "";
string Num2 = "";
double Quotient = 0;
Console.WriteLine("The quotient of " + Num1 + " and " + Num2 + " is: " + Quotient.ToString ("N2"));
Console.ReadKey();
}
}
“Have I not commanded you?be strong and
courageous. Do not be discouraged, for the Lord
your God will be with you wherever you go.”
Joshua 1:9
CHAPTER 3
Conditional Statements
When do we use conditional statements
• Decisions
• Evaluate a value
C# Conditional Statements
• if statement
• if…else statement
• else if statement
• switch statement
if statement
Syntax:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
Sample Problem #18
Console.ReadKey();
}
}
if…else statement
Syntax:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}
Sample Problem #19
Console.ReadKey();
}
}
Nested if……
Console.ReadKey();
}
}
C# Loops
• There may be a situation, when you need to execute a block of
code several number of times. In general, the statements are
executed sequentially: The first statement in a function is
executed first, followed by the second, and so on.