1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.

Slides:



Advertisements
Similar presentations
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Advertisements

CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
If Statements & Relational Operators Programming.
Introduction to Computers and Programming Lecture 5 New York University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Outline Relational Operators Boolean Operators Truth Tables Precedence Table Selection and Algorithms The if – else Variations of if The switch.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Flow of Control Part 1: Selection
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Lesson thirteen Conditional Statement "if- else" ©
CPS120: Introduction to Computer Science Decision Making in Programs.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
Random Functions Selection Structure Comparison Operators Logical Operator
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 Type Called bool  Bool has only two possible values: True and False.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
CS0007: Introduction to Computer Programming
Chapter 4: Control Structures I
Basics programming concepts-2
Selection (also known as Branching) Jumail Bin Taliba by
ALGORITHMS CONDITIONAL BRANCH CONTROL STRUCTURE
Introduction to C++ Programming Language
Thanachat Thanomkulabut
Chapter 4 – Control Structures Part 1
Expressions and Control Flow in JavaScript
SELECTION STATEMENTS (1)
OPERATORS (2) CSC 111.
Bools and simple if statements
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Life is Full of Alternatives
Computer Programming Basics
Presentation transcript:

1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand

2 Conditional statement  In the previous lessons, all the statements in the programs are executed sequentially without skipping  Conditional statement Now, some statements may be executed only if certain conditions are satisfied, otherwise, the statements will be skipped number = int.Parse(Console.ReadLine()); if(number>=0) Console.WriteLine("The number is positive"); else Console.WriteLine("The number is negative");

3 Data type: bool  Boolean (bool when declared) is a logical data type. A value of a variable whose data type is boolean can be either true or false. Notes : A variable with type bool can not be read by input statement. However it can be displayed by output statement

4 static void Main(string[] args) { bool found; string fact; int year; Console.Write("Please enter faculty's name : "); fact = Console.ReadLine(); fact = fact.ToLower(); Console.Write("please enter year studying : "); year = int.Parse(Console.ReadLine()); if (((fact == "engineer")&& (year >= 2)) || ((fact == "sciences") && (year >=3))) found = true; else found = false; Console.WriteLine(found); }

5 class ClassBoolean { static void Main(string[] args) { Console.WriteLine(2+3 == 5); Console.WriteLine('A' < 'B'); Console.WriteLine("Engineer" == "engineer"); Console.WriteLine(3*4 == 7*4/2); } Output : true true false

6 Relational Operators  They are used to compare data items of the compatible type Relational Operator in Pascal Meaning ==Equal to >Greater than <Less than >=Greater than or equal to <=Less than or equal to !=Not equal to

7 Boolean Expression - Boolean Constants and Boolean Variable  Boolean constants and Boolean variables are of type bool static void Main(string[] args) { const bool sunRiseFromEast = true; const bool oneGreaterThanTwo = false; bool boy; Console.WriteLine("The sun rises from the east is " + sunRiseFromEast); boy = false; Console.WriteLine("It is " + boy + " that Paul is a boy"); }

8 Boolean Expression - Boolean Expressions with Operands of Arithmetic Expressions Boolean Expression Value 2.5 == 5false 4%10 > 1-3true 5.7 >= -2true 5/2 <= 1.1*2false 999!=999.9true

9 Boolean Expression - Boolean Expressions with Character Operands ASCII – American Standard Code for Information Interchange Contains 128 characters

10 Boolean Expression - Boolean Expressions with Character Operands  Characters are compared using their code values according to certain code system. Boolean Expression ’\’ != ’j’ ’9’ > ’4’ ’A’ >= ’a’ ASCII of First Operand ASCII of Second Operand Boolean Expression true false

11 Compound Boolean Expressions  Simple Boolean Expressions may be joined by logical operators to form compound Boolean expressions  Logical Operators && (and) || (or) ! (not)  Examples (age >= 18) && (age <=20) (grade == 'F') || (grade =='W')

12 Compound Boolean Expressions - Logical Operators  Truth Table for ‘and’ operator  Truth Table for ‘or’ operator Boolean expression PBoolean expression QP && Q TRUE FALSE TRUEFALSE Boolean expression PBoolean expression QP || Q TRUE FALSETRUE FALSETRUETURE FALSE

13 Compound Boolean Expressions - Logical Operators  Truth table for ‘not’ operator Boolean Expression P! Q TRUEFALSE TRUE

14 Precedence  The order of precedence is ‘!’, ‘&&’, ‘||’  The relational operators have lower precedence than logical operators OperatorPrecedence Level ( ) 1 ++, --, !, cast 2 *, /, % 3 +, - 4, >= 5 ==, != 6 && 7 || 8 int x = 3, y = 2, z = 4; Console.WriteLine(++x+3 >= y+6 && z>=4);

15 Boolean Expression Evaluation !('F' != 'M') || (19 > 12) && (19 < 18) !( true ) || (true) && (false) ( false ) || (true) && (false) ( false ) || ( false ) ( false )

16 Flowcharts  Graphical representation Terminator Process Input/output Condition Connector Flow line

17 Conditional Statements  Two kinds of conditional statements in C# are if statements and switch statements If statements If-else statements Nested if statements Switch statements