0% found this document useful (0 votes)
19 views

Computer Applications in Industrial Engg.-I: Lecture #03

This document contains a lecture summary on computer applications in industrial engineering by Dr. Atif Shahzad. It discusses various C# programming concepts like comparison and logical operators, the if, if-else and switch-case statements, data types, variables, input/output functions, arithmetic operators and precedence. It provides examples of writing programs to calculate roots of quadratic equations and comparing numbers using conditional statements.

Uploaded by

atifshahzad
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Computer Applications in Industrial Engg.-I: Lecture #03

This document contains a lecture summary on computer applications in industrial engineering by Dr. Atif Shahzad. It discusses various C# programming concepts like comparison and logical operators, the if, if-else and switch-case statements, data types, variables, input/output functions, arithmetic operators and precedence. It provides examples of writing programs to calculate roots of quadratic equations and comparing numbers using conditional statements.

Uploaded by

atifshahzad
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 28

DR ATIF SHAHZAD

IE-322

Computer Applications
LECTURE #03 in Industrial Engg.-I
Computer Applications in
Industrial Engg.-I

IE322
Dr. Atif Shahzad

8/12/2018
…Recap
What will se see…
Dr. Atif Shahzad

8/12/2018
What we will see…
Comparison and Logical Operators

The if Statement

The if-else Statement

Nested if Statements

The switch-case Statement


Dr. Atif Shahzad

8/12/2018
Console.Read()
Console.ReadLine()
Console.Write()
Console.WriteLine()
Dr. Atif Shahzad

8/12/2018 6
Formatting Text with WriteLine
Console.WriteLine(“{0}\n{1}”, “This is
my another Program”, “in C#”)
Dr. Atif Shahzad

8/12/2018 7
Escape Sequence
\ is an escape character
\n
\t
\r
\\
\”
Dr. Atif Shahzad

8/12/2018 8
Reading some numeric data
Console.Write(“Enter first number”);
Num1=Convert.ToInt32(Console.ReadLine())
Dr. Atif Shahzad

8/12/2018 9
Data Conversion
Convert.ToInt32()
Convert.ToDouble()
Dr. Atif Shahzad

8/12/2018 10
Arithmetic Operators

* /

% +

-
Dr. Atif Shahzad

8/12/2018 11
Operators Precedence Example

Write the expression in c#

z=pr%q + w/x-y
Dr. Atif Shahzad

8/12/2018 12
Increment (Decrement) Operator
x=6;
x = x + 1;
x = x++;
x += 1; //Compound Assignment

x /= 4; // equivalent to x=x/4;
x *= 8; // equivalent to x=x*8;
x %= 2; // equivalent to x=x%2;
Dr. Atif Shahzad

8/12/2018 13
Problem
Write a program that enters the
coefficients a, b and c of a quadratic
equation
2
𝑎𝑥 + 𝑏𝑥 + 𝑐 = 0
and calculates and prints its real roots.
Note that quadratic equations may have 0, 1 or
2 real roots.
Dr. Atif Shahzad

8/12/2018 14
Relational Operators
Operator Notation in C#
Equals ==
Not Equals !=
Greater Than >
Greater Than or Equals >=
Less Than <
Less Than or Equals <=
Dr. Atif Shahzad

8/12/2018 16
Increment (Decrement) Operator
x=6;
x = x + 1;
x = x++;
x += 1; //Compound Assignment

x /= 4; // equivalent to x=x/4;
x *= 8; // equivalent to x=x*8;
x %= 2; // equivalent to x=x%2;
Dr. Atif Shahzad

8/12/2018 17
Prefix & Postfix form
++x; //prefix
x++; //postfix
int x = 3;
int y = ++x;
// x is 4 and y is 4

int x = 3;
int y = x++;
// x is 4 and y is 3
Dr. Atif Shahzad

8/12/2018 18
Prefix & Postfix form
++x; //prefix
x++; //postfix
int x = 3;
int y = ++x;
// x is 4 and y is 4

int x = 3;
int y = x++;
// x is 4 and y is 3
Dr. Atif Shahzad

8/12/2018 19
Comparison Operators
Operator Notation in C#
Equals ==
Not Equals !=
Greater Than >
Greater Than or Equals >=
Less Than <
Less Than or Equals <=

bool result = 5 <= 6;


Dr. Atif Shahzad

Console.WriteLine(result); // True
8/12/2018 20
Logical Operators
Operator Notation in C#
Logical NOT !
Logical AND &&
Logical OR ||
Logical Exclusive OR (XOR) ^
Dr. Atif Shahzad

8/12/2018 21
if statement
if (condition)
{
statements;
}

static void Main()


{
Console.WriteLine("Enter two numbers.");

int biggerNumber = int.Parse(Console.ReadLine());


int smallerNumber = int.Parse(Console.ReadLine());

if (smallerNumber > biggerNumber)


{
biggerNumber = smallerNumber;
}
Dr. Atif Shahzad

Console.WriteLine("The greater number is: {0}",


biggerNumber);
}
8/12/2018 22
if else statement
if (expression)
{
statement1;
}
else
{
statement2;
} string s = Console.ReadLine();
int number = int.Parse(s);

if (number % 2 == 0)
{
Console.WriteLine("This number is even.");
}
else
{
Dr. Atif Shahzad

Console.WriteLine("This number is odd.");


}
8/12/2018 23
Nested if else statements
if (expression)
{
if (expression)
{
statement; if (first == second)
} {
else Console.WriteLine(
"These two numbers are equal.");
{ }
statement; else
} {
} if (first > second)
{
else Console.WriteLine(
statement; "The first number is bigger.");
}
else
{
Dr. Atif Shahzad

Console.WriteLine("The second is bigger.");


}
}

8/12/2018 24
if else if statements

int ch = 'X';
if (ch == 'A' || ch == 'a')
{
Console.WriteLine("Vowel [ei]");
}
else if (ch == 'E' || ch == 'e')
{
Console.WriteLine("Vowel [i:]");
}
Dr. Atif Shahzad

else if …
else …

8/12/2018 25
Switch case statement

switch (day)
{
case 1: Console.WriteLine("Monday"); break;
case 2: Console.WriteLine("Tuesday"); break;
case 3: Console.WriteLine("Wednesday"); break;
case 4: Console.WriteLine("Thursday"); break;
case 5: Console.WriteLine("Friday"); break;
case 6: Console.WriteLine("Saturday"); break;
case 7: Console.WriteLine("Sunday"); break;
Dr. Atif Shahzad

default: Console.WriteLine("Error!"); break;


}

8/12/2018 26
Switch case statement

switch (animal)
{
case "dog" :
Console.WriteLine("MAMMAL");
break;
case "crocodile" :
case "tortoise" :
case "snake" :
Console.WriteLine("REPTILE");
break;
default :
Console.WriteLine("There is no such animal!");
Dr. Atif Shahzad

break;
}
8/12/2018 27
NEVER hesitate to
contact should you
have any question
Dr. Atif Shahzad
Dr. Atif Shahzad

8/12/2018 29

You might also like