C programming. conditional statements

Similar documents
Conditional Statement

Operators in C. Staff Incharge: S.Sasirekha

COMP 208 Computers in Engineering

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Flow Control. So Far: Writing simple statements that get executed one after another.

Chapter 6. Section 6.3 The switch Statement. CS 50 Hathairat Rattanasook

CT 229 Java Syntax Continued

Flow Control. CSC215 Lecture

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Operators & Expressions

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Introduction to C Programming

Data Types and Variables in C language

משתנים שעור מס. 2 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

Chapter 2 Control in Programming. lecture 2 1

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Unit-2 (Operators) ANAND KR.SRIVASTAVA

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

Tail recursion. Decision. Assignment. Iteration

Operators Questions

Test Bank for Problem Solving with C++: The Object of Programming, 8/e Chapter 2 C++ Basics

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Lecture 5 Tao Wang 1

Introduction. C provides two styles of flow control:

Expressions and Precedence. Last updated 12/10/18

Chapter 4 The If Then Statement

Simple if Simple if-else Nested if - else Notes for if-else statement Logical operators

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

Control structures in C. Going beyond sequential

EECE.2160: ECE Application Programming Spring 2018

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

CS Introduction to Data Structures How to Parse Arithmetic Expressions

Working with Lists 4

CS 117 Fall Compound boolean expressions. Control Statements, Part 2. Using boolean operators. Boolean operators

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

LECTURE 02 INTRODUCTION TO C++

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

LECTURE 5 Control Structures Part 2

Fundamentals of Programming CS-110. Lecture 3

Operators And Expressions

The Arithmetic Operators

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Variables and literals

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Operators in java Operator operands.

Unit 3. Operators. School of Science and Technology INTRODUCTION

Review of the C Programming Language for Principles of Operating Systems

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Operators. Java operators are classified into three categories:

(Refer Slide Time: 00:26)

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

DECISION MAKING STATEMENTS

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Part I Part 1 Expressions

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Booleans, Logical Expressions, and Predicates

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

QUIZ: What value is stored in a after this

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Object Oriented Software Design

Expression and Operator

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

CSE 115. Introduction to Computer Science I

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

INF2220: algorithms and data structures Series 1

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Conditional Statement

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Department of Computer Science

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Type Conversion. and. Statements

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

SML A F unctional Functional Language Language Lecture 19

Transcription:

C programming Lecture 4: Logical expressions, conditional statements Based on slides designed by Shay Artsi,Aythan Avior,Gitit Rockstein and Saher Ismir for the department of Computer Science, Technion, Israel Institute of Technology Translated and updated by Anne Weill-Zrahia נכתב על-ידי טל כהן, נערך ע"י איתן אביאור. כל הזכויות שמורות לטכניון מכון טכנולוגי לישראל

Statements Expression statement terminated by ; usually performs some action Empty statement terminated by ; does nothing Compound statement (or block) a series of statements between {} Contents of a block may be blocks A block can be empty (same signification as empty statement). 2

Logical expressions Calculations are only a small part of computer programming Decision statements are needed - they will specify the order in which statements are executed A logical expression can have two values TRUE FALSE or in C language for FALSE int a = 3, a b = -3, c = ; ערך אמת ביטוי a + b a += b a = 2*c true false false true : or any number for TRUE 3

Relational and Equality operators int a =, b = 5; Expression a == b b == a b <= 8 a!= b Value (int) Value (logical) F F T T Relational < <= >= > Equality ==!= Values: TRUE FALSE < -b < 8 a = b b = a a = a 5 T T F F 4

Logical operations Are used to build logical expressions. Example: ( < b) && (b < 8) האופרטור &&! משמעות לוגית and or not exp exp 2 exp && exp 2 exp exp 2! exp 5

Operators table Group Operators Associativity Unary! + - (type) sizeof() ++ -- Right to left Multiplication * / % Left to right Addition + - Left to right Relational < <= >= > Left to right Equality ==!= Left to right Logical AND && Left to right Logical OR Left to right Assignment = += -= *= /= %= Right to left 6

Short Circuit Computation of logical expressions stops the moment the value is known: Example : (a <= 3) && (i++ < ) If a>3 obviously the whole expression is F The second parenthesis will not be computed The value of i will not be changed. Example 2: (a <= 3) (i++ < ) If a<=3 obviously the whole expression is T The second parenthesis will not be computed The value of i will not be changed 7.

If statement if ( expression ) statement If the logical value of expression is TRUE, statement will be executed If the logical value of expression is FALSE, statement will not be executed, and command will go to next statement A statement can be complex : if ( b * b < 4 * a * c ) { printf("no real solution to the equation.\n"); יכול להיות משפט מורכב, ; = לדוגמה: solutions } statement : 8

if- statement if ( exp ) stat stat 2 If the value of exp is TRUE: stat will be executed If the value of exp is FALSE, stat2 will be executed. d = b * b - 4 * a * c; if ( d < ) { printf("no real solution to the equation.\n"); solutions = ; } { printf("exists a real solution to the equation.\n"); solutions = ; } 9

Nested statements if- d = b * b - 4 * a * c; if ( d < ) if ( b == ) printf("two imaginary solutions.\n"); printf("two complex solutions.\n"); if ( d == ) printf("one real solution.\n"); printf("two real solutions.\n");

Oscillating if ( d >= ) /* Need only real solutions */ if ( d > ) printf("solutions: %g and %g:\n", (-b + sqrt(d)) / ( 2*a), (-b - sqrt(d)) / ( 2*a)); printf("solution: %g\n", -b / (2*a)); if ( d >= ) if ( d > ) /* d == */ if ( d >= ) if ( d > ) /* d < */

:. Oscillating how to prevent errors In C, an oscillating if statement refers to the closest In this case the execution was correct (by sheer luck) This is not recommended since: We may not remember it is so We may write in other programming languages where it is not so Another person reading the code may not understand it correctly if ( d >= ) if ( d > ) if ( d >= ) if ( d > ) ; /* or {} */ if ( d >= ) { if ( d > ) } 2

What if we need the opposite if ( d!= ) /* Need only real solutions */ if ( d > ) printf("solutions: %g and %g:\n", (-b + sqrt(d)) / ( 2*a), (-b - sqrt(d)) / ( 2*a)); printf("solution: %g\n", -b / (2*a)); if ( d!= ) if ( d > ) /* d < */ if ( d!= ) if ( d > ) /* d == */ הפעם המזל לרעתנו... 3

Oscillating opposite case As said before luck is not on our side How to fix that : The print will occur only if d ==. if ( d!= ) if ( d > ) /* d == is false */ if ( d!= ) if ( d > ) ; /* or {} */ if ( d!= ) { if ( d > ) } 4

Series of if--if if ( d == ) printf("one real solution.\n"); if ( d > ) printf("two real solutions.\n"); if ( b == ) printf("two imaginary solutions.\n"); printf("two complex solutions.\n"); if ( d == ) printf("one real solution.\n"); if ( d > ) printf("two real solutions.\n"); if ( b == ) printf("two imaginary solutions.\n"); printf("two complex solutions.\n"); This one is better! 5

Conditional statement if ( m > n ) max = m; max = n; if ( m > n ) printf("max is %d\n", m); printf("max is %d\n", n); max = m > n? m : n; Ternary operation This is an expression, not a statement. Priority of? Is very low, just before assignment Associativity is right to left. printf("max is %d\n", m > n? m : n); 6