0% found this document useful (0 votes)
2 views20 pages

04 - Conditional.statements - Windows_Programming_with_C#

The document provides an overview of conditional statements in C# programming, including the use of comparison operators, Boolean expressions, and the syntax for 'if', 'if-else', and 'switch-case' statements. It explains how these constructs allow programs to execute different code paths based on evaluated conditions. Additionally, it includes good practices for using 'switch-case' and an assignment related to quadratic equations.

Uploaded by

roberatech
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)
2 views20 pages

04 - Conditional.statements - Windows_Programming_with_C#

The document provides an overview of conditional statements in C# programming, including the use of comparison operators, Boolean expressions, and the syntax for 'if', 'if-else', and 'switch-case' statements. It explains how these constructs allow programs to execute different code paths based on evaluated conditions. Additionally, it includes good practices for using 'switch-case' and an assignment related to quadratic equations.

Uploaded by

roberatech
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/ 20

Windows Programming with C#

Using Visual Studio IDE, Visual Basic .NET, Visual Studio Code + Mono
Windows Console Application

Instructor : Eyasu G.
Telegram : @JoshKiyakoo
Email : sendtokiya@gmail.com
Conditional Statements

<date/time> <footer> 2
Overview of Operators and Expressions
In the following section we will recall the basic comparison operators and Boolean
expressions in the C# language. They are important, because we use them to describe
conditions in our conditional statements.
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 ).
The logical operators && (logical AND) and || (logical OR) are only used on Boolean
expressions (values of type bool ).
In order for the result – of comparing two expressions with the operator && – to be true (true),
both operands must have the value true .
When two operands are compared with || and the first one is "true", we still evaluate the
second operand and the final result is nevertheless "true".
The ^ operator, also known as exclusive OR (XOR). The result of applying the operator is true if
exactly one of the operands is true, but not both simultaneously. Otherwise the result is false .

<date/time> <footer> 3
Conditional Statements "if" and "if-else" or “switch-case”

Conditional statements if and if-else or switch-case are conditional


control statements.
Because of them ( Conditional Statements - if and if-else or switch-
case ) the program can behave differently based on a defined condition
checked during the execution of the statement.

<date/time> <footer> 4
Conditional Statements "if"
After reviewing how to compare expressions, we will continue with
conditional statements, which will allow us to implement programming
logic.
The main format of the conditional statements if is as follows:

Syntax:
if ( expression ) {
// Body of the conditional statement;
}

<date/time> <footer> 5
Conditional Statements "if"
The conditional statement starts with the keyword if clause.
The expression can be a Boolean variable or Boolean logical expression.
Boolean expressions cannot be integer.
The body of the statement is the part locked between the curly brackets:
{...} . It may consist of one or more operations (statements). When there are
several operations, we have a complex block operator, i.e. series of
commands that follow one after the other, enclosed in curly brackets.
The expression in the brackets which follows the keyword if must return
the Boolean value true or false . If the expression is calculated to the value
true , then the body of a conditional statement is executed. If the result is
false , then the operators in the body will be skipped.

<date/time> <footer> 6
Conditional Statements "if"
static void Main () {
Console.WriteLine("Please enter the 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);
}

<date/time> <footer> 7
Conditional Statements "if-else"
In C#, as in most of the programming languages there is a conditional
statement with else clause: the if - else statement. Its format is the
following:

Syntax:
if ( expression ) {
// Body of the conditional statement;
}
else {
// Body of the else statement;
}

<date/time> <footer> 8
Conditional Statements "if-else"
The expression in the brackets (a Boolean expression) is
calculated. The calculation result must be Boolean – true or
false . Depending on the result there are two possible
outcomes.
If the Boolean expression is calculated to true , the body of
the conditional statement is executed and the else -
statement is omitted and its operators do not execute.
Otherwise, if the Boolean expression is calculated to false ,
the else -body is executed, the main body of the conditional
statement is omitted and the operators in it are not executed.
<date/time> <footer> 9
Conditional Statements "if-else"
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");
}
}
<date/time> <footer> 10
Nested "if" or “if-else” Statements
Sometimes the programming logic in a program or an
application needs to be represented by multiple if -
structures contained in each other. We call them nested if
or nested if-else structures.
We call nesting the placement of an if or if - else structure
in the body of another if or else-if or else structure. In such
situations every else clause corresponds to the closest
previous if clause.

<date/time> <footer> 11
Nested "if" or “if-else” Statements
Syntax:
if ( expression ) {
// Body of the outer-conditional statement;
}
else {
// Body of the outer-else statement;
if ( expression ) {
// Body of the inner-conditional statement;
}
else {
// Body of the inner-else statement;
}
}

<date/time> <footer> 12
Nested "if" or “if-else” Statements
Here is an example of using nested if structures:
int firstNum = 10; int secondNum = 5;
if (firstNum == secondNum) {
Console.WriteLine("These two numbers are equal.");
}
else {
if (firstNum > secondNum) {
Console.WriteLine("The first number is greater.");
}
else {
Console.WriteLine("The second number is greater.");
}
}
}

<date/time> <footer> 13
Conditional Statement "switch-case"

The structure switch-case chooses which part of the programming


code to execute based on the calculated value of a certain expression
(most often of integer type or some constant values).

<date/time> <footer> 14
Conditional Statement "switch-case"
Syntax
switch ( selector ) {
case const_value_1:
// statements;
break;
case const_value_2:
// statements;
break;
default:
// statements;
break;
}

<date/time> <footer> 15
Conditional Statement "switch-case"
The selector is an expression returning a resulting value that can be compared, like a
number or string . The switch operator compares the result of the selector to every
value listed in the case labels in the body of the switch structure.
If a match is found in a case label, the corresponding structure is executed (simple or
complex). If no match is found, the default statement is executed (when such exists).
The value of the selector must be calculated before comparing it to the values inside the
switch structure. The labels should not have repeating values, they must be unique.
As it can be seen from the definition above, every case ends with the operator break ,
which ends the body of the switch structure. The C# compiler requires the word break at
the end of each case-section containing code. If no code is found after a case-statement,
the break can be omitted and the execution passes to the next case-statement and
continues until it finds a break operator. After the default structure break is
obligatory.

<date/time> <footer> 16
Good Practices When Using "switch-case"
- A good practice when using the switch statement is to put the default statement
at the end, in order to have easier to read code.
- It’s good to place first the cases , which handle the most common situations or
decisions. Case statements, which handle situations occurring rarely, can be placed
at the end of the structure.
- If the values in the case labels are integer or real, it’s recommended that they be
arranged in ascending order.
- If the values in the case labels are of character type, it’s recommended that the
case labels are sorted alphabetically.
- It’s advisable to always use a default block to handle situations that cannot be
processed in the normal operation of the program. If in the normal operation of the
program the default block should not be reachable, you could put in it a code
reporting an error.
<date/time> <footer> 17
Assignment 4
Write a program that gets the coefficients a , b and c of a quadratic equation: ax2 + bx + c,
calculates and prints its real roots (if they exist). Quadratic equations may have 0, 1 or 2 real
roots.
**CLUE**: From math it is known, that a quadratic equation may have one or two real roots or no
real roots at all. In order to calculate the real roots of a given quadratic equation, we first calculate
the discriminant (D) by the formula: D = b2 - 4 ac . If the discriminant is zero, then the quadratic
equation has one double real root and it is calculated by the formula:

If the value of the discriminant is positive, then the equation has


two distinct real roots, which are calculated by the formula:

If the discriminant is negative, the quadratic equation has no real roots.

<date/time> <footer> 18
Iterations | Loops

<date/time> <footer> 19
Thank You !!!

<date/time> <footer> 20

You might also like