Chapter 2. Section 2.3 if and if-else statements. CS 50 Hathairat Rattanasook

Similar documents
Chapter 6. Section 6.4 Altering the Flow of Control. CS 50 Hathairat Rattanasook

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

CS 139 Practice Midterm Questions #2

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

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

Introduction. C provides two styles of flow control:

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Crude Video Game Simulator Algorithm

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Flow Control. CSC215 Lecture

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

LECTURE 5 Control Structures Part 2

Introduction to C Programming

Chapter 13 Control Structures

CS16 Exam #1 7/17/ Minutes 100 Points total

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Conditional Statement

Conditional Expressions

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Decision Making -Branching. Class Incharge: S. Sasirekha

CS201 Some Important Definitions

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Lecture 6. Statements

Chapter 4. Section 4.4 Passing Parameters to Functions. CS 50 Hathairat Rattanasook

Chapter 13 Control Structures

OER on Loops in C Programming

DECISION MAKING STATEMENTS

Decision Making. if Statement. Decision Making

Prerequisites: Read all chapters through Chapter 4 in the textbook before attempting this lab. Read through this entire assignment before you begin.

Chapter 1. Section 1.4 Subprograms or functions. CS 50 - Hathairat Rattanasook

Tail recursion. Decision. Assignment. Iteration

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

Understanding main() function Input/Output Streams

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Review for Test 1 (Chapter 1-5)

Computational Expression

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

Chapter 2 Control in Programming. lecture 2 1

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Chapter 13. Control Structures

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

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

CS4215 Programming Language Implementation

Chapter 4. Flow of Control

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

CS 101 Spring 2007 Midterm 2 Name: ID:

Lecture 19 CSE August You taught me Language, and my profit on t is I know how to curse. William Shakspere, The Tempest, I, ii.

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

CS302: Self Check Quiz 2

Unit 3 Decision making, Looping and Arrays

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

An algorithm may be expressed in a number of ways:

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Chapter 7: Preprocessing Directives

The Design of C: A Rational Reconstruction (cont.)

& Technology. Expression? Statement-x. void main() int no; scanf("%d", &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements.

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

8. Control statements

Lecture 5: C programming

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 3 Structured Program Development

Chapter 4 C Program Control

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

Information Science 1

2.2 - Making Decisions

Programming for Engineers Iteration

CS536 Spring 2011 FINAL ID: Page 2 of 11

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Module 4: Decision-making and forming loops

Couple Notes about Boolean Expressions in C

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Chapter 14 Testing Tactics

Lecture 5 Tao Wang 1

Digital Forensics Lecture 4 - Reverse Engineering

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

CT 229 Java Syntax Continued

TheCrazyProgrammer.com Page 1

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

Comments. Comments: /* This is a comment */

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

This exam is worth 30 points, or 18.75% of your total course grade. The exam contains

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Transcription:

Chapter 2 Section 2.3 if and if-else statements CS 50 Hathairat Rattanasook

if Statement An if-statement is a statement with a condition that can be evaluated to true or false. If it evaluates to true, the following statement or compound statements are executed If it evaluates to false, the following statement or compound statements are skipped if ( expression ) // condition statement with control expression statement CS 50 - Hathairat Rattanasook 1

if Statement if ( expression ) // condition statement with control expression statement A condition statement: allows a program to test a condition and then choose which code to execute next. A control expression: Its value will be interpreted as either true (nonzero) or false (zero). Statement: A statement can be a block, an executable statement, or another if statement. CS 50 - Hathairat Rattanasook 2

if Statement Example An if-statement with a statement: If number is smaller than 10, the printf(..) method call will be executed. if (number < 10) printf("number is less than 10"); An if-statement with a block: If number is smaller than 10, the printf(..) method call will be executed. if (number < 10) { printf("number is less than 10"); CS 50 - Hathairat Rattanasook 3

if-else Statement The code will execute the statement1 when the expression is true (nonzero). The code will execute the statement2 when the expression is false (zero). if ( expression ) statement 1 else statement 2 CS 50 - Hathairat Rattanasook 4

if-else Statement Example If age is more than 15, the code will print "you can drive". Otherwise, it will print "you can NOT drive". if (age > 15) printf("you can drive"); else printf("you can NOT drive); If age is more than 15, the block with "you can drive" is executed. Otherwise, the block after else is executed. if (age > 15) { printf("you can drive"); else { printf("you can NOT drive); CS 50 - Hathairat Rattanasook 5

if-else Statement Example The following code will produce a compile time error. Remember: if you don't declare a block, you can only have a single statement in the if or the else part. If you want to have multiple statements, you must declare a block. if (age > 15) printf("you can drive"); printf("drive safely"); else printf("you can NOT drive); CS 50 - Hathairat Rattanasook 6

if-else Statement Example The following code will NOT produce a compile time error. However: it is likely a logical error! The printf("you can NOT drive") of the else-part will only be executed if age > 15 evaluates to false. The printf("see you later") is ALWAYS executed because it is a regular statement that just follows the if-else statement. if (age > 15) { printf("you can drive"); printf("drive safely"); else printf("you can NOT drive); printf("see you later"); CS 50 - Hathairat Rattanasook 7

Hint A good programmer always uses { and to define a block in if and in if-else statements. A block improves code clarity and reduces the likelihood of bugs CS 50 - Hathairat Rattanasook 8

Compound if-else Statement Many times you may want to test several conditions and execute different statements depending on the result of each condition. That's what compound if-else statements are used for. if (expression 1) statement 1; else if (expression 2) statement 2; else if (expression 3) statement 3; else statement n; CS 50 - Hathairat Rattanasook 9

Nested if-else Statement It is also possible to nest if or if-else statements if (expression 1) { if (expression 2) { statement 2; else if (expression 3) { if (expression 4) { statement 3; else { statement n; CS 50 - Hathairat Rattanasook 10

Question 1. Which statement gets executed in the below code if a = 3 and b = 9? 2. Which if-statement does the else in line 4 belong to? Line 1 or line 2? 1 if (a > 5) 2 if (b < 10) 3 statement1; 4 else if (b > 10) 6 statement2; 7 else 8 statement3; CS 50 - Hathairat Rattanasook 11

Answer 1. If a = 3 and b = 9, no statement is executed (see answer 2) 2. The else in line 4 belongs to the if condition in line 2 and even else in line 8 belongs to the compound if-else in line 2. Therefore, if a is 5 or smaller, the ifcondition evaluates to false and lines 2-8 are skipped. 1 if (a > 5) 2 if (b < 10) 3 statement1; 4 else if (b > 10) 6 statement2; 7 else 8 statement3; This is a good example why declaring blocks are important! CS 50 - Hathairat Rattanasook 12

Compound if-else Example The code segment below will cause a compile time error. In case, you want a compiler to execute multiple statements, those statements should be inside a block. This code will produce a compile error for statement 1! if (expression 1) if (expression 2) statement 2; statement 1 else if (expression 3) statement 3; else statement n; if (expression 1) { if (expression 2) { statement 2; statement 1 else if (expression 3) { statement 3; else { statement n; CS 50 - Hathairat Rattanasook 13

Code Clarity Many of the last examples show that it is very important to define blocks! Defining blocks: makes the code more clear Helps others to better understand what you are trying to do Reduces compile errors and logical bugs! Helps to maintain code (eg. when you come back in a year and need to update your code) CS 50 - Hathairat Rattanasook 14