SlideShare a Scribd company logo
1 of 30
Programming
Languages
Lecture 04: Conditions
1st Semester, 2022 - 2023
Milad Ashqi Abdullah
Quiz
• Write a program that takes three
letters as input and display them in
reverse order.
Quiz
• Write a program to print the output of
multiplication of three numbers which
will be entered by the user.
• Answer:
Input the first number: 2
Input the second number: 3
Input the third number: 6
Expected Output:
2 x 3 x 6 = 36
Test yourself!
• Write a program which will receive
height, width and radios as inputs,
and will find the Blue Area.
• The area of Square:
Area = Height*Width
• The area of Circle:
Area = r2 π
Width
Height
Radios
Conditions
• Can you solve this problem?
If X is greater than Y:
then Z = X/Y
Otherwise Z = 1 ?
It is impossible without using
Conditions.
Conditions
To create a condition in C#:
if ( condition )
statement;
Or
if ( condition )
{
statement;
statement;
statement;
}
Conditions
• For Example
int x = 14;
if (x > 10)
Console.WriteLine(x);
The above line will print 14 because
the condition is true.
Conditions
• For Example
int x = 8;
if (x > 10)
{
Console.WriteLine(x);
Console.WriteLine("Hello");
x = 10;
}
The above statements will not run
Conditions
• IF is followed by ( ) which has a condition inside it.
• The condition value is either true or false.
• So, the condition expression is Boolean (bool).
• It will run the next statement, if condition is true.
• If the condition is false, it will skip the next
statement.
• The if condition affects only one statement after it.
• If there is need of more than one statement, use { }
Group up
Conditions
IF Statement - Example
int x;
x = 14 ;
if (x > 10)
x = 18;
Console.Write(x);
int x;
x = 14 ;
if (x < 10)
x = 18;
Console.Write(x);
true false
14
18
Conditions
IF Statement – Example using { }
int x;
x = 14 ;
if (x > 10)
{
x = 18;
Console.Write(x);
}
int x;
x = 14 ;
if (x < 10)
{
x = 18;
Console.Write(x);
}
true false
18
Conditions
IF Statement – find p and d
int x = -3;
char p = ‘F’, d;
if ( x > -5)
d = p;
x = 18;
if (x < 18)
d = ‘h’;
if (x-10 > -5)
p = ‘k’;
Condition Expressions
• In C# we have these comparing expressions:
• X > Y : if X is greater than Y
• X < Y : if X is smaller than Y
• X == Y : if x is equal to Y
• X <= Y : if x is less than and equal to Y
• X >= Y : if x is greater than and equal to y
if (1 == 9) Console.WriteLine(“Soran”);
if (18 > 4) Console.WriteLine(“Dohuk”);
if (9 <= -10) Console.WriteLine(“Zakho”);
if (1+6 > 4+2) Console.WriteLine(“Hawler”);
if (1*5 == 10-5) Console.WriteLine(“Ammedi”);
if (3 > 4/2 ) Console.WriteLine(“Akre”);
if (17%2 == 1) Console.WriteLine(“Halabja”);
if (-1 < -3) Console.WriteLine(“Suleimani”);
if (3*6+1 > 1+9*2) Console.WriteLine(“Karkuk”);
if (true) Console.WriteLine(“Rania”);
Condition Expressions
• Simple Quick Question
Write a program which:
if X is greater than 10 print X
If not then print “ERROR”
There are two ways to solve this
Conditions
Conditions
IF Statement – Way 1
int x;
x = 18 ;
if (x > 10)
Console.Write(x);
If (x <= 10)
Console.Write(“Error”);
Conditions
IF Statement – Way 2
int x;
x = 18 ;
if (x > 10)
Console.Write(x);
else
Console.Write(“Error”);
• Else grammar
if (conditional expression) // run if true
statement;
else // run if false
statement;
For example
if ( x > 10 )
Console.Write ( “X is greater than 10”);
else
Console.Write ( “X is not greater than 10”);
Conditions
Conditions
Else Statement
• else Statement does not need Condition.
• Else will run the next statement if condition is false.
• The else statement does not have ; at the end of it.
• Each if condition can have only one else statement
• The else statement affects only one statement after it.
• If there is need of more than one statement, use { }
Conditions
Example – find the output
double g = 14;
double x = 18;
g = g + x;
if (g < x)
Console.Write(“ g < x“);
else
Console.Write(“ g > = x“);
If
(g<x)
If Code
Else
Code
true
false
continue
Conditions
Example – find the output
int g= 8;
int x= -3;
g = g + x;
if (g > x)
Console.Write(“ g > x“);
else
Console.Write(“ g <= x“);
If
(g>x)
If Code
Else
Code
true
false
continue
Conditions
Statement
static void main()
{
int z;
float b = 28;
double c = b * b;
if (b>c)
z=1;
else
z=2;
}
How many Statements will run from above code?
Statement 1
Statement 2
Statement 3
Statement 4
Conditions
Statement – Which one is Correct
static void main()
{
int z,x;
float b = 28;
double c = b * b;
if (b>c)
{
z=1;
}
else
z=2;
}
static void main()
{
int z,x;
float b = 28;
double c = b * b;
if (b>c)
z=1;
else
z=2;
b=0;
}
Conditions
Example – find variable values
Double x = 1;
Double y = 4;
if (x * y == y * x)
if ( x> y)
x = 0;
else
y = 0;
else
x = y;
If
(xy ==
yx)
X = y
continue
If X>y
X = 0 y = 0
false
true
true
false
Conditions
Boolean Algebra
• Boolean values are either true or false
• They construct base of computing 1 and 0
• Conditions uses Boolean values to run
• For example:
bool a = true;
if (a)
Console.WriteLine(“ This is True”);
else
Console.WriteLine(“This is false”);
Conditions
Example 1
Write a program which will show if two
strings are equal
string a = Console.ReadLine();
string b = Console.ReadLine();
if ( a==b)
Console.WriteLine(“strings are equal”);
else
Console.WriteLine(“strings are not equal”);
Conditions
Example 2
Write a program which will indicate if
a number is negative or positive.
int number = -5;
if ( number < 0)
Console.WriteLine(number +” is negative”);
else
Console.WriteLine(number +” is positive”);
Conditions
Example 3
Write a program which will indicate if
a number is odd or even.
int number = 18;
if ( number %2 == 1)
Console.WriteLine(number +” is odd”);
else
Console.WriteLine(number +” is even”);
Lab Plan
• Let’s create a calculator
• Let’s check the IF
• Enjoy

More Related Content

Similar to 04 Conditions.pptx

Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 

Similar to 04 Conditions.pptx (20)

Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
3 flow
3 flow3 flow
3 flow
 
3 flow
3 flow3 flow
3 flow
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selection
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.ppt
 
Software engineering
Software engineeringSoftware engineering
Software engineering
 
Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
 
Cin and cout
Cin and coutCin and cout
Cin and cout
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Fekra c++ Course #2
Fekra c++ Course #2Fekra c++ Course #2
Fekra c++ Course #2
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPE
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
Branching statements
Branching statementsBranching statements
Branching statements
 
conditional.ppt
conditional.pptconditional.ppt
conditional.ppt
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
901131 examples
901131 examples901131 examples
901131 examples
 

Recently uploaded

如何办理(CSU毕业证书)圣马科斯分校毕业证成绩单原件一模一样
如何办理(CSU毕业证书)圣马科斯分校毕业证成绩单原件一模一样如何办理(CSU毕业证书)圣马科斯分校毕业证成绩单原件一模一样
如何办理(CSU毕业证书)圣马科斯分校毕业证成绩单原件一模一样
qyguxu
 
Abortion Clinic in Hazyview +27791653574 Hazyview WhatsApp Abortion Clinic Se...
Abortion Clinic in Hazyview +27791653574 Hazyview WhatsApp Abortion Clinic Se...Abortion Clinic in Hazyview +27791653574 Hazyview WhatsApp Abortion Clinic Se...
Abortion Clinic in Hazyview +27791653574 Hazyview WhatsApp Abortion Clinic Se...
mikehavy0
 
Abortion pills in Jeddah Saudi Arabia (+966572737505) buy cytotec
Abortion pills in Jeddah Saudi Arabia (+966572737505) buy cytotecAbortion pills in Jeddah Saudi Arabia (+966572737505) buy cytotec
Abortion pills in Jeddah Saudi Arabia (+966572737505) buy cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
如何办理(Indiana State毕业证书)印第安纳州立大学毕业证成绩单原件一模一样
如何办理(Indiana State毕业证书)印第安纳州立大学毕业证成绩单原件一模一样如何办理(Indiana State毕业证书)印第安纳州立大学毕业证成绩单原件一模一样
如何办理(Indiana State毕业证书)印第安纳州立大学毕业证成绩单原件一模一样
qyguxu
 
一比一原版(UCI毕业证)加州大学欧文分校毕业证成绩单学位证留信学历认证
一比一原版(UCI毕业证)加州大学欧文分校毕业证成绩单学位证留信学历认证一比一原版(UCI毕业证)加州大学欧文分校毕业证成绩单学位证留信学历认证
一比一原版(UCI毕业证)加州大学欧文分校毕业证成绩单学位证留信学历认证
vflw6bsde
 
如何办理(UNTEC毕业证书)新西兰联合理工学院毕业证成绩单原件一模一样
如何办理(UNTEC毕业证书)新西兰联合理工学院毕业证成绩单原件一模一样如何办理(UNTEC毕业证书)新西兰联合理工学院毕业证成绩单原件一模一样
如何办理(UNTEC毕业证书)新西兰联合理工学院毕业证成绩单原件一模一样
qyguxu
 
如何办理(UoA毕业证书)奥克兰大学毕业证成绩单原件一模一样
如何办理(UoA毕业证书)奥克兰大学毕业证成绩单原件一模一样如何办理(UoA毕业证书)奥克兰大学毕业证成绩单原件一模一样
如何办理(UoA毕业证书)奥克兰大学毕业证成绩单原件一模一样
qyguxu
 
如何办理(UST毕业证书)圣托马斯大学毕业证成绩单原件一模一样
如何办理(UST毕业证书)圣托马斯大学毕业证成绩单原件一模一样如何办理(UST毕业证书)圣托马斯大学毕业证成绩单原件一模一样
如何办理(UST毕业证书)圣托马斯大学毕业证成绩单原件一模一样
muwyto
 
如何办理(NEU毕业证书)东北大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(NEU毕业证书)东北大学毕业证成绩单本科硕士学位证留信学历认证如何办理(NEU毕业证书)东北大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(NEU毕业证书)东北大学毕业证成绩单本科硕士学位证留信学历认证
gakamzu
 
如何办理(UIUC毕业证书)UIUC毕业证香槟分校毕业证成绩单本科硕士学位证留信学历认证
如何办理(UIUC毕业证书)UIUC毕业证香槟分校毕业证成绩单本科硕士学位证留信学历认证如何办理(UIUC毕业证书)UIUC毕业证香槟分校毕业证成绩单本科硕士学位证留信学历认证
如何办理(UIUC毕业证书)UIUC毕业证香槟分校毕业证成绩单本科硕士学位证留信学历认证
gakamzu
 
B. A. (Prog.) Political Science 6th Semester 2019.pdf
B. A. (Prog.) Political Science 6th Semester 2019.pdfB. A. (Prog.) Political Science 6th Semester 2019.pdf
B. A. (Prog.) Political Science 6th Semester 2019.pdf
paraspiyush3
 
如何办理(CBU毕业证书)浸会大学毕业证成绩单原件一模一样
如何办理(CBU毕业证书)浸会大学毕业证成绩单原件一模一样如何办理(CBU毕业证书)浸会大学毕业证成绩单原件一模一样
如何办理(CBU毕业证书)浸会大学毕业证成绩单原件一模一样
qyguxu
 
如何办理(laurentian毕业证书)劳伦森大学毕业证成绩单原件一模一样
如何办理(laurentian毕业证书)劳伦森大学毕业证成绩单原件一模一样如何办理(laurentian毕业证书)劳伦森大学毕业证成绩单原件一模一样
如何办理(laurentian毕业证书)劳伦森大学毕业证成绩单原件一模一样
muwyto
 
如何办理(Wintec毕业证书)怀卡托理工学院毕业证成绩单原件一模一样
如何办理(Wintec毕业证书)怀卡托理工学院毕业证成绩单原件一模一样如何办理(Wintec毕业证书)怀卡托理工学院毕业证成绩单原件一模一样
如何办理(Wintec毕业证书)怀卡托理工学院毕业证成绩单原件一模一样
qyguxu
 
如何办理(USC毕业证书)南加利福尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(USC毕业证书)南加利福尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(USC毕业证书)南加利福尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(USC毕业证书)南加利福尼亚大学毕业证成绩单本科硕士学位证留信学历认证
gakamzu
 

Recently uploaded (20)

The Best VFX Course with Job Placement near Dunlop
The Best VFX Course with Job Placement near DunlopThe Best VFX Course with Job Placement near Dunlop
The Best VFX Course with Job Placement near Dunlop
 
CV OF Dr. David Burkett | Cardiologist and Electrophysiologist .
CV OF Dr. David Burkett | Cardiologist and Electrophysiologist .CV OF Dr. David Burkett | Cardiologist and Electrophysiologist .
CV OF Dr. David Burkett | Cardiologist and Electrophysiologist .
 
如何办理(CSU毕业证书)圣马科斯分校毕业证成绩单原件一模一样
如何办理(CSU毕业证书)圣马科斯分校毕业证成绩单原件一模一样如何办理(CSU毕业证书)圣马科斯分校毕业证成绩单原件一模一样
如何办理(CSU毕业证书)圣马科斯分校毕业证成绩单原件一模一样
 
Abortion Clinic in Hazyview +27791653574 Hazyview WhatsApp Abortion Clinic Se...
Abortion Clinic in Hazyview +27791653574 Hazyview WhatsApp Abortion Clinic Se...Abortion Clinic in Hazyview +27791653574 Hazyview WhatsApp Abortion Clinic Se...
Abortion Clinic in Hazyview +27791653574 Hazyview WhatsApp Abortion Clinic Se...
 
Abortion pills in Jeddah Saudi Arabia (+966572737505) buy cytotec
Abortion pills in Jeddah Saudi Arabia (+966572737505) buy cytotecAbortion pills in Jeddah Saudi Arabia (+966572737505) buy cytotec
Abortion pills in Jeddah Saudi Arabia (+966572737505) buy cytotec
 
如何办理(Indiana State毕业证书)印第安纳州立大学毕业证成绩单原件一模一样
如何办理(Indiana State毕业证书)印第安纳州立大学毕业证成绩单原件一模一样如何办理(Indiana State毕业证书)印第安纳州立大学毕业证成绩单原件一模一样
如何办理(Indiana State毕业证书)印第安纳州立大学毕业证成绩单原件一模一样
 
一比一原版(UCI毕业证)加州大学欧文分校毕业证成绩单学位证留信学历认证
一比一原版(UCI毕业证)加州大学欧文分校毕业证成绩单学位证留信学历认证一比一原版(UCI毕业证)加州大学欧文分校毕业证成绩单学位证留信学历认证
一比一原版(UCI毕业证)加州大学欧文分校毕业证成绩单学位证留信学历认证
 
如何办理(UNTEC毕业证书)新西兰联合理工学院毕业证成绩单原件一模一样
如何办理(UNTEC毕业证书)新西兰联合理工学院毕业证成绩单原件一模一样如何办理(UNTEC毕业证书)新西兰联合理工学院毕业证成绩单原件一模一样
如何办理(UNTEC毕业证书)新西兰联合理工学院毕业证成绩单原件一模一样
 
We’re looking for a junior patent engineer to join our Team!
We’re looking for a junior patent engineer to join our Team!We’re looking for a junior patent engineer to join our Team!
We’re looking for a junior patent engineer to join our Team!
 
We’re looking for a Technology consultant to join our Team!
We’re looking for a Technology consultant to join our Team!We’re looking for a Technology consultant to join our Team!
We’re looking for a Technology consultant to join our Team!
 
如何办理(UoA毕业证书)奥克兰大学毕业证成绩单原件一模一样
如何办理(UoA毕业证书)奥克兰大学毕业证成绩单原件一模一样如何办理(UoA毕业证书)奥克兰大学毕业证成绩单原件一模一样
如何办理(UoA毕业证书)奥克兰大学毕业证成绩单原件一模一样
 
Master SEO in 2024 - The Complete Beginner's Guide
Master SEO in 2024 - The Complete Beginner's GuideMaster SEO in 2024 - The Complete Beginner's Guide
Master SEO in 2024 - The Complete Beginner's Guide
 
如何办理(UST毕业证书)圣托马斯大学毕业证成绩单原件一模一样
如何办理(UST毕业证书)圣托马斯大学毕业证成绩单原件一模一样如何办理(UST毕业证书)圣托马斯大学毕业证成绩单原件一模一样
如何办理(UST毕业证书)圣托马斯大学毕业证成绩单原件一模一样
 
如何办理(NEU毕业证书)东北大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(NEU毕业证书)东北大学毕业证成绩单本科硕士学位证留信学历认证如何办理(NEU毕业证书)东北大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(NEU毕业证书)东北大学毕业证成绩单本科硕士学位证留信学历认证
 
如何办理(UIUC毕业证书)UIUC毕业证香槟分校毕业证成绩单本科硕士学位证留信学历认证
如何办理(UIUC毕业证书)UIUC毕业证香槟分校毕业证成绩单本科硕士学位证留信学历认证如何办理(UIUC毕业证书)UIUC毕业证香槟分校毕业证成绩单本科硕士学位证留信学历认证
如何办理(UIUC毕业证书)UIUC毕业证香槟分校毕业证成绩单本科硕士学位证留信学历认证
 
B. A. (Prog.) Political Science 6th Semester 2019.pdf
B. A. (Prog.) Political Science 6th Semester 2019.pdfB. A. (Prog.) Political Science 6th Semester 2019.pdf
B. A. (Prog.) Political Science 6th Semester 2019.pdf
 
如何办理(CBU毕业证书)浸会大学毕业证成绩单原件一模一样
如何办理(CBU毕业证书)浸会大学毕业证成绩单原件一模一样如何办理(CBU毕业证书)浸会大学毕业证成绩单原件一模一样
如何办理(CBU毕业证书)浸会大学毕业证成绩单原件一模一样
 
如何办理(laurentian毕业证书)劳伦森大学毕业证成绩单原件一模一样
如何办理(laurentian毕业证书)劳伦森大学毕业证成绩单原件一模一样如何办理(laurentian毕业证书)劳伦森大学毕业证成绩单原件一模一样
如何办理(laurentian毕业证书)劳伦森大学毕业证成绩单原件一模一样
 
如何办理(Wintec毕业证书)怀卡托理工学院毕业证成绩单原件一模一样
如何办理(Wintec毕业证书)怀卡托理工学院毕业证成绩单原件一模一样如何办理(Wintec毕业证书)怀卡托理工学院毕业证成绩单原件一模一样
如何办理(Wintec毕业证书)怀卡托理工学院毕业证成绩单原件一模一样
 
如何办理(USC毕业证书)南加利福尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(USC毕业证书)南加利福尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(USC毕业证书)南加利福尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(USC毕业证书)南加利福尼亚大学毕业证成绩单本科硕士学位证留信学历认证
 

04 Conditions.pptx

  • 1. Programming Languages Lecture 04: Conditions 1st Semester, 2022 - 2023 Milad Ashqi Abdullah
  • 2. Quiz • Write a program that takes three letters as input and display them in reverse order.
  • 3. Quiz • Write a program to print the output of multiplication of three numbers which will be entered by the user. • Answer: Input the first number: 2 Input the second number: 3 Input the third number: 6 Expected Output: 2 x 3 x 6 = 36
  • 4. Test yourself! • Write a program which will receive height, width and radios as inputs, and will find the Blue Area. • The area of Square: Area = Height*Width • The area of Circle: Area = r2 π Width Height Radios
  • 5. Conditions • Can you solve this problem? If X is greater than Y: then Z = X/Y Otherwise Z = 1 ? It is impossible without using Conditions.
  • 6. Conditions To create a condition in C#: if ( condition ) statement; Or if ( condition ) { statement; statement; statement; }
  • 7. Conditions • For Example int x = 14; if (x > 10) Console.WriteLine(x); The above line will print 14 because the condition is true.
  • 8. Conditions • For Example int x = 8; if (x > 10) { Console.WriteLine(x); Console.WriteLine("Hello"); x = 10; } The above statements will not run
  • 9. Conditions • IF is followed by ( ) which has a condition inside it. • The condition value is either true or false. • So, the condition expression is Boolean (bool). • It will run the next statement, if condition is true. • If the condition is false, it will skip the next statement. • The if condition affects only one statement after it. • If there is need of more than one statement, use { }
  • 11. Conditions IF Statement - Example int x; x = 14 ; if (x > 10) x = 18; Console.Write(x); int x; x = 14 ; if (x < 10) x = 18; Console.Write(x); true false 14 18
  • 12. Conditions IF Statement – Example using { } int x; x = 14 ; if (x > 10) { x = 18; Console.Write(x); } int x; x = 14 ; if (x < 10) { x = 18; Console.Write(x); } true false 18
  • 13. Conditions IF Statement – find p and d int x = -3; char p = ‘F’, d; if ( x > -5) d = p; x = 18; if (x < 18) d = ‘h’; if (x-10 > -5) p = ‘k’;
  • 14. Condition Expressions • In C# we have these comparing expressions: • X > Y : if X is greater than Y • X < Y : if X is smaller than Y • X == Y : if x is equal to Y • X <= Y : if x is less than and equal to Y • X >= Y : if x is greater than and equal to y
  • 15. if (1 == 9) Console.WriteLine(“Soran”); if (18 > 4) Console.WriteLine(“Dohuk”); if (9 <= -10) Console.WriteLine(“Zakho”); if (1+6 > 4+2) Console.WriteLine(“Hawler”); if (1*5 == 10-5) Console.WriteLine(“Ammedi”); if (3 > 4/2 ) Console.WriteLine(“Akre”); if (17%2 == 1) Console.WriteLine(“Halabja”); if (-1 < -3) Console.WriteLine(“Suleimani”); if (3*6+1 > 1+9*2) Console.WriteLine(“Karkuk”); if (true) Console.WriteLine(“Rania”); Condition Expressions
  • 16. • Simple Quick Question Write a program which: if X is greater than 10 print X If not then print “ERROR” There are two ways to solve this Conditions
  • 17. Conditions IF Statement – Way 1 int x; x = 18 ; if (x > 10) Console.Write(x); If (x <= 10) Console.Write(“Error”);
  • 18. Conditions IF Statement – Way 2 int x; x = 18 ; if (x > 10) Console.Write(x); else Console.Write(“Error”);
  • 19. • Else grammar if (conditional expression) // run if true statement; else // run if false statement; For example if ( x > 10 ) Console.Write ( “X is greater than 10”); else Console.Write ( “X is not greater than 10”); Conditions
  • 20. Conditions Else Statement • else Statement does not need Condition. • Else will run the next statement if condition is false. • The else statement does not have ; at the end of it. • Each if condition can have only one else statement • The else statement affects only one statement after it. • If there is need of more than one statement, use { }
  • 21. Conditions Example – find the output double g = 14; double x = 18; g = g + x; if (g < x) Console.Write(“ g < x“); else Console.Write(“ g > = x“); If (g<x) If Code Else Code true false continue
  • 22. Conditions Example – find the output int g= 8; int x= -3; g = g + x; if (g > x) Console.Write(“ g > x“); else Console.Write(“ g <= x“); If (g>x) If Code Else Code true false continue
  • 23. Conditions Statement static void main() { int z; float b = 28; double c = b * b; if (b>c) z=1; else z=2; } How many Statements will run from above code? Statement 1 Statement 2 Statement 3 Statement 4
  • 24. Conditions Statement – Which one is Correct static void main() { int z,x; float b = 28; double c = b * b; if (b>c) { z=1; } else z=2; } static void main() { int z,x; float b = 28; double c = b * b; if (b>c) z=1; else z=2; b=0; }
  • 25. Conditions Example – find variable values Double x = 1; Double y = 4; if (x * y == y * x) if ( x> y) x = 0; else y = 0; else x = y; If (xy == yx) X = y continue If X>y X = 0 y = 0 false true true false
  • 26. Conditions Boolean Algebra • Boolean values are either true or false • They construct base of computing 1 and 0 • Conditions uses Boolean values to run • For example: bool a = true; if (a) Console.WriteLine(“ This is True”); else Console.WriteLine(“This is false”);
  • 27. Conditions Example 1 Write a program which will show if two strings are equal string a = Console.ReadLine(); string b = Console.ReadLine(); if ( a==b) Console.WriteLine(“strings are equal”); else Console.WriteLine(“strings are not equal”);
  • 28. Conditions Example 2 Write a program which will indicate if a number is negative or positive. int number = -5; if ( number < 0) Console.WriteLine(number +” is negative”); else Console.WriteLine(number +” is positive”);
  • 29. Conditions Example 3 Write a program which will indicate if a number is odd or even. int number = 18; if ( number %2 == 1) Console.WriteLine(number +” is odd”); else Console.WriteLine(number +” is even”);
  • 30. Lab Plan • Let’s create a calculator • Let’s check the IF • Enjoy