RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION 350142 - Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.

Slides:



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

Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.
Logical Operators and Conditional statements
Chapter 4 Making Decisions
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
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
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CPS120: Introduction to Computer Science Decision Making in Programs.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Flow of Control Part 1: Selection
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Control statements Mostafa Abdallah
Review : C Programming Language
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Lecture 3 Selection Statements
The Ohio State University
Chapter 3 Selection Statements
Intro to C Tutorial 4: Arithmetic and Logical expressions
Topics The if Statement The if-else Statement Comparing Strings
Iteration statement while do-while
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Topics The if Statement The if-else Statement Comparing Strings
And now for something completely different . . .
C Programming Variables.
Relational Operators Operator Meaning < Less than > Greater than
Structured Program
Conditions and Boolean Expressions
Selection Statements.
Chapter 4: Control Structures I (Selection)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Life is Full of Alternatives
Introduction to Computing Lecture 04: Booleans & Selection
Computer Programming Basics
CSCE 206 Lab Structured Programming in C
Iteration Statement for
Presentation transcript:

RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat

Relational Operators  Relational operators are used to test the relation between two values. All C relational operators are binary operators and hence requires two operands.  The output of this expression is either TRUE or FALSE SymbolDefinition ==Equal !=Not equal >Greater than >=Greater than or equal to <Less than <=Less than or equal to Warning: A = 5; and A == 5; are not the same A = 5 means assigning the value 5 to A A == 5 means comparing the value of A and 5.

QUIZ 1  What is the truth value of these expressions?  10 > 8  -9 < -3  13 != 13  2 < -5  == 16 / 2

Character Comparison  We can also test the value of 2 characters by using its ASCII value  ‘C’ < ‘c’ ASCII code of C is 67 ASCII code of c is 99  ‘P’ > ‘M’ ASCII code of P is 80 ASCII code of M is 77

True and False representation in C  In C, actually there are no TRUE and FALSE value  C language uses integer number to represent the truth value  0 for FALSE  1 for TRUE  Thus,  5 > 2  True  C will return 1  -5 < -10  False  C will return 0

Logical Operators  You can join 2 or more comparison expressions by using logical operators  In C, there are 3 logical operators SymbolMeaningType !NOTUnary Operator &&ANDBinary Operator ||ORBinary Operator In C language, any numeric values are considered as TRUE except 0 (zero) as false

Boolean Table for NOT (!)  ! (logical NOT) returns 1 (true) when its operand result is 0 (false) and false otherwise. Ex.int a = 10; !a; /* the result is 0 (false) */ !(a < 5);/* the result is 1 (true) */ Logical OperationResult !false1 (true) !true0 (false)

Boolean Table for AND (&&)  && (logical AND) returns 1 (true) when both of its operands are true and returns 0 (false) otherwise. Ex.int a = 5, b = 10; (a >= 5) && (b > 20); /* the result is 0 (false) */ (a != 15) && b /* the result is 1 (true) */

Boolean Table for OR (||)  || (logical OR) returns 1 (true) when either of its operands are true and returns 0 (false) if both the operands are false. Ex.int a = 5, b = 10; (a != 5) || (b <= 5); /* the result is 0 (false) */ (b > 20) || (a == 5); /* the result is 1 (true) */

Summary: Boolean Table

Operator Ordering  What are the output of the following expressions?  !(2 < 3) && (5 == 5)  !(2*5 >= 3) || (5 != (15/3))  0 || (2 – 2) && 5 OrderOperator 1Arithmetic Operators 2! 3>, >=, <, <= 4==, != 5&& 6||

Conditional Control Statement  Usually the C language statements are executed sequentially. But in some environments, this sequential flow of execution might have to be changed and the control should be transferred to another part of the program.  C provides 3 kinds of conditional control statement that help you make change to the flow of your program  if statement  if - else statement  if – else if – else statement  next week

The if statement if (condition) statement;  condition: the expression that programmer defined to consider either true or false  statement: this statement will be executed if the condition is true, otherwise just skip this statement if (condition) { statement-1; statement-2; statement-3; … statement-n; }  condition: the expression that programmer defined to consider either true or false  statements: these statements will be executed if the condition is true, otherwise just skip all of these statements. 1 2

Example: IF statement A == 5 ? A = A + 5 true false int main(void) { int A = 5; if ( A == 5 ) A = A + 5; } A == 5 ? A = A + 5 truefalse A = A * 10 int main(void) { int A = 5; if ( A == 5 ) { A = A + 5; A = A * 10; }

Example #include int main(int argc, char **argv) { int point; printf(“Enter your examination point : ”); scanf(“%d”, &point); if (point >= 50) printf(“You passed, congratulation\n”); } #include int main(int argc, char **argv) { int point; printf(“Enter your examination point : ”); scanf(“%d”, &point); if (point >= 50) printf(“You passed, congratulation\n”); }

QUIZ 2 int main(int argc, char **argv) { int X; printf(“Enter a number: ”); scanf(“%d”, &X); if (X <= 5) X += 10; if (X >= 13) { X -= 5; X++; } printf(“X = %d\n”, X); } int main(int argc, char **argv) { int X; printf(“Enter a number: ”); scanf(“%d”, &X); if (X <= 5) X += 10; if (X >= 13) { X -= 5; X++; } printf(“X = %d\n”, X); } What is the output of this program, if user enter the following numbers as an input to X variable :

The if-else Statement if (condition) statement; else statement; if (condition) { statement 1; statement 2; statement 3; ….. } else { statement 1; statement 2; statement 3; ….. } 1 2

Condition: IF-ELSE statement int main(void) { int A = 5; if ( A == 5 ) A = A + 5; else A = A – 10; } A == 5 ? A = A + 10 true false A = A * 10 int main(void) { int A = 5; if ( A == 5 ) { A = A + 10; A = A * 10; } else { A = A – 10; A = A / 10; } A == 5 ? A = A + 10 A = A - 10 true false A = A - 10 A = A / 10

Example #include int main(int argc, char **argv) { int point; printf(“Enter your examination point : ”); scanf(“%d”, &point); if (point >= 50) printf(“You passed, congratulation\n”); else printf(“Sorry, you didn’t pass\n”); } #include int main(int argc, char **argv) { int point; printf(“Enter your examination point : ”); scanf(“%d”, &point); if (point >= 50) printf(“You passed, congratulation\n”); else printf(“Sorry, you didn’t pass\n”); }

QUIZ 3 #include int main(int argc, char **argv) { int num; printf(“Enter number : ”); scanf(“%d”, &num); if (num % 2 == 0) printf(“Even number\n”); else printf(“Odd number\n”); } #include int main(int argc, char **argv) { int num; printf(“Enter number : ”); scanf(“%d”, &num); if (num % 2 == 0) printf(“Even number\n”); else printf(“Odd number\n”); } What is the output of this program, if user input the following numbers:

Nested If if (condition-1) { if (condition-2) { … if (condition-n) statement; } #include int main(int argc, char **argv) { int num; printf(“Enter your number : ”); scanf(“%d”, &num); if (num >= 30) { if (num <= 40) { if (num % 2 == 1) printf(“Right number\n”); else printf(“Wrong number\n”); } else printf(“Large number\n”); } else printf(“Small number\n”); }  Find the output of the program when user input the following numbers:  20  50  30  35