Implementing Control Logic in C# Svetlin Nakov Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Subroutines in Computer Programming Svetlin Nakov Telerik Corporation
Reading and Writing to the Console Svetlin Nakov Telerik Corporation
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Subroutines in Computer Programming Svetlin Nakov Telerik Corporation
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4 Selection Structures: Making Decisions.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Conditional Statements Implementing Control-Flow Logic in C# SoftUni Team Technical Trainers Software University
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Perform Calculations and Control Flow Telerik Software Academy Learning & Development Team.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
COMP Loop Statements Yi Hong May 21, 2015.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Chapter three Conditional Statements ©
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Statements: Part1  if, if…else, switch 1.
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 3 Control Statements
Selection (also known as Branching) Jumail Bin Taliba by
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Making Decisions.
JavaScript: Control Statements.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 3: Selection Structures: Making Decisions
CSC215 Lecture Control Flow.
Presentation transcript:

Implementing Control Logic in C# Svetlin Nakov Telerik Corporation

1. Comparison and Logical Operators 2. The if Statement 3. The if-else Statement 4. Nested if Statements 5. The switch-case Statement 2

4Operator Notation in C# Equals== Not Equals != Greater Than > Greater Than or Equals >= Less Than < Less Than or Equals <=  Example: bool result = 5 <= 6; Console.WriteLine(result); // True

 De Morgan laws  !!A  A  !(A || B)  !A && !B  !(A && B)  !A || !B Operator Notation in C# Logical NOT ! Logical AND && Logical OR || Logical Exclusive OR (XOR) ^ 5

Implementing Conditional Logic

 The most simple conditional statement  Enables you to test for a condition  Branch to different parts of the code depending on the result  The simplest form of an if statement: if (condition) { statements; statements;} 7

 The condition can be:  Boolean variable  Boolean logical expression  Comparison expression  The condition cannot be integer variable (like in C / C++)  The statement can be:  Single statement ending with a semicolon  Block enclosed in braces 8

 The condition is evaluated  If it is true, the statement is executed  If it is false, the statement is skipped true condition statement false 9

10 static void Main() { Console.WriteLine("Enter two numbers."); Console.WriteLine("Enter two numbers."); int biggerNumber = int.Parse(Console.ReadLine()); int biggerNumber = int.Parse(Console.ReadLine()); int smallerNumber = int.Parse(Console.ReadLine()); int smallerNumber = int.Parse(Console.ReadLine()); if (smallerNumber > biggerNumber) if (smallerNumber > biggerNumber) { biggerNumber = smallerNumber; biggerNumber = smallerNumber; } Console.WriteLine("The greater number is: {0}", Console.WriteLine("The greater number is: {0}", biggerNumber); biggerNumber);}

Live Demo

 More complex and useful conditional statement  Executes one branch if the condition is true, and another if it is false  The simplest form of an if-else statement: if (expression) { statement1; statement1;}else{ statement2; statement2;} 12

 The condition is evaluated  If it is true, the first statement is executed  If it is false, the second statement is executed condition firststatement true secondstatement false 13

 Checking a number if it is odd or even string s = Console.ReadLine(); int number = int.Parse(s); if (number % 2 == 0) { Console.WriteLine("This number is even."); Console.WriteLine("This number is even.");}else{ Console.WriteLine("This number is odd."); Console.WriteLine("This number is odd.");} 14

Live Demo

Creating More Complex Logic

 if and if-else statements can be nested, i.e. used inside another if or else statement  Every else corresponds to its closest preceding if if (expression) { if (expression) if (expression) { statement; statement; } else else { statement; statement; }}else 17

 Always use { … } blocks to avoid ambiguity  Even when a single statement follows  Avoid using more than three levels of nested if statements  Put the case you normally expect to process first, then write the unusual cases  Arrange the code to make it more readable 18

if (first == second) { Console.WriteLine( Console.WriteLine( "These two numbers are equal."); "These two numbers are equal.");}else{ if (first > second) if (first > second) { Console.WriteLine( Console.WriteLine( "The first number is bigger."); "The first number is bigger."); } else else { Console.WriteLine("The second is bigger."); Console.WriteLine("The second is bigger."); }} 19

Live Demo

 Sometimes we need to use another if - construction in the else block  Thus else if can be used: 21 int ch = 'X'; if (ch == 'A' || ch == 'a') { Console.WriteLine("Vowel [ei]"); Console.WriteLine("Vowel [ei]");} else if (ch == 'E' || ch == 'e') { Console.WriteLine("Vowel [i:]"); Console.WriteLine("Vowel [i:]");} else if … else …

Live Demo

Making Several Comparisons at Once

 Selects for execution a statement from a list depending on the value of the switch expression 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; default: Console.WriteLine("Error!"); break; } 24

1. The expression is evaluated 2. When one of the constants specified in a case label is equal to the expression  The statement that corresponds to that case is executed 3. If no case is equal to the expression  If there is default case, it is executed  Otherwise the control is transferred to the end point of the switch statement 25

Live Demo

 Variables types like string, enum and integral types can be used for switch expression  The value null is permitted as a case label constant  The keyword break exits the switch statement  "No fall through" rule – you are obligated to use break after each case  Multiple labels that correspond to the same statement are permitted 27

switch (animal) { case "dog" : case "dog" : Console.WriteLine("MAMMAL"); Console.WriteLine("MAMMAL"); break; break; case "crocodile" : case "crocodile" : case "tortoise" : case "tortoise" : case "snake" : case "snake" : Console.WriteLine("REPTILE"); Console.WriteLine("REPTILE"); break; break; default : default : Console.WriteLine("There is no such animal!"); Console.WriteLine("There is no such animal!"); break; break;}  You can use multiple labels to execute the same statement in more than one case 28

Live Demo

 There must be a separate case for every normal situation  Put the normal case first  Put the most frequently executed cases first and the least frequently executed last  Order cases alphabetically or numerically  In default use case that cannot be reached under normal circumstances 30

 Comparison and logical operators are used to compose logical conditions  The conditional statements if and if-else provide conditional execution of blocks of code  Constantly used in computer programming  Conditional statements can be nested  The switch statement easily and elegantly checks an expression for a sequence of values 31

Questions?

1. Write an if statement that examines two integer variables and exchanges their values if the first one is greater than the second one. 2. Write a program that shows the sign (+ or -) of the product of three real numbers without calculating it. Use a sequence of if statements. 3. Write a program that finds the biggest of three integers using nested if statements. 4. Sort 3 real values in descending order using nested if statements. 33

5. Write program that asks for a digit and depending on the input shows the name of that digit (in English) using a switch statement. 6. Write a program that enters the coefficients a, b and c of a quadratic equation a*x 2 + b*x + c = 0 and calculates and prints its real roots. Note that quadratic equations may have 0, 1 or 2 real roots. 7. Write a program that finds the greatest of given 5 variables. 34

8. Write a program that, depending on the user's choice inputs int, double or string variable. If the variable is integer or double, increases it with 1. If the variable is string, appends " * " at its end. The program must show the value of that variable as a console output. Use switch statement. 9. We are given 5 integer numbers. Write a program that checks if the sum of some subset of them is 0. Example: 3, -2, 1, 1, 8  1+1-2=0. 35

10. Write a program that applies bonus scores to given scores in the range [1..9]. The program reads a digit as an input. If the digit is between 1 and 3, the program multiplies it by 10; if it is between 4 and 6, multiplies it by 100; if it is between 7 and 9, multiplies it by If it is zero or if the value is not a digit, the program must report an error. Use a switch statement and at the end print the calculated new value in the console. 36

11. * Write a program that converts a number in the range [ ] to a text corresponding to its English pronunciation. Examples: 0  "Zero" 273  "Two hundred seventy three" 400  "Four hundred" 501  "Five hundred and one" 711  "Seven hundred and eleven" 37