BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

Similar documents
BBM#101# #Introduc/on#to# Programming#I# Fall$2014,$Lecture$4$

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

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

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal

Decision Making -Branching. Class Incharge: S. Sasirekha

Flow Control. CSC215 Lecture

5. Selection: If and Switch Controls

Chapter 4. Flow of Control

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Conditional Statement

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

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

Chapter 4 - Notes Control Structures I (Selection)

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

Introduction to C Programming

BRANCHING if-else statements

Conditional Statement

Lecture 8. Daily Puzzle

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

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

Introduction to Object-Oriented Programming

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

COMP 208 Computers in Engineering

C: How to Program. Week /Mar/05

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

UEE1302(1102) F10: Introduction to Computers and Programming

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

Control Structure: Selection

Logical Operators and switch

Topics. Chapter 5. Equality Operators

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

Introduction. C provides two styles of flow control:

LECTURE 04 MAKING DECISIONS

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 6-7

If Control Construct

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

Chapter 3 Structured Program Development

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

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Chapter 4: Making Decisions

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.

Computer Science & Engineering 150A Problem Solving Using Computers

Chapter 8. More Control Statements

Chapter 4: Making Decisions

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

DECISION CONTROL AND LOOPING STATEMENTS

MODULE 2: Branching and Looping

DECISION MAKING STATEMENTS

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Decision making with if Statement : - Control Statements. Introduction: -

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

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

Introduction to Computing Lecture 05: Selection (continued)

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

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Formatting & Style Examples

Chapter 2 - Introduction to C Programming

CSE 142 Programming I

Lecture 6. Statements

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

C++ Programming: From Problem Analysis to Program Design, Third Edition

Control Structures in Java if-else and switch

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

Structured Program Development

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

EE 121. Office suite. Lecture 3: Introduction to programming and C programming language

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Full file at C How to Program, 6/e Multiple Choice Test Bank

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

The following expression causes a divide by zero error:

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

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

D0010E Object- oriented programming and design. Today. Today An introduc<on to the basic syntax and seman<cs of Java

Fundamentals of Programming Session 9

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Control Structures in Java if-else and switch

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

Operators and Expressions:

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

n Group of statements that are executed repeatedly while some condition remains true

Fundamental of Programming (C)

CS 106 Introduction to Computer Science I

Following is the general form of a typical decision making structure found in most of the programming languages:

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Decision Making and Loops

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

UIC. C Programming Primer. Bharathidasan University

Lecture 3. More About C

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Lecture 5 Tao Wang 1

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Conditionals. CSE / ENGR 142 Programming I. Chapter 4. Conditional Execution. Conditional ("if ") Statement. Conditional Expressions

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Transcription:

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4 Instructors: Aykut Erdem, Erkut Erdem, Fuat Akal TAs: Ahmet Selman Bozkir, Gultekin Isik, Yasin Sahin 1

Today Condi/onal Branching Logical Expressions if and If- else statements switch statement goto statement Itera/on Control Loop Statements for, while, do- while structures break and con/nue 2

Rela/onal Operators Take 2 expressions as operands e.g., a < 3 reads as a is less than 3 Yield either the int value 0 (false) or the int value 1 (true). Operator Meaning <= Less than or equal to < Less than > Greater than >= Greater than or equal to == Equal!= Not equal 3

Rela/onal Operators (Examples) Valid Examples (assume a = 1 and b = 2) a < b à 1 (true) a!= b 1 à 0 (false) a + 1 <= b à 1 (true) Invalid Examples a =< b a => b The Most Confused Case ( = vs. == ) a = b is an assignment expression a == b is a test for equality One of the most common problems the C programmer faces is mixing them up 4

Logical Operators The precedence of && is higher than Both operators are of lower precedence than all unary, arithmexc and relaxonal operators. i.e.,! > && >> Expressions connected by && or are evaluated from ley to right. Operator Meaning && Logical AND Logical OR! Logical Nega/on 5

Truth Table a b a && b a b!a zero zero 0 0 1 zero nonzero 0 1 1 nonzero zero 0 1 0 nonzero nonzero 1 1 0 We had covered this before while studying Boolean Algebra in Lecture 2. 6

Logical Operators (Examples) NegaXon Examples!5 à 0!!5 à 1!(6 < 7) à 0!6 < 7 à 1!(3-4) à 0 7

Logical Operators (Examples) Given declaraxons int a = 3, b = 3, c = 3; double x = 0.0, y = 2.5; char ch = g Expressions!(a < b) && c à 1 ch >= a && ch <= z à 1 x a && b 3 à 0 a < b && x < y à 0 a < b x < y à 1 8

Short Circuit Evalua/on For the expressions that contain the && or operands, the expression process stops as soon as the outcome is already known. Suppose expr1 is zero. expr1 && expr2 = 0 (expr2 will not be evaluated.) Suppose expr1 is nonzero. expr1 expr2 = 1 (expr2 will not be evaluated.) 9

The if Statement Syntax if (condition) statement; If the condixon is true (nonzero) the statement will be executed. If the condixon is false (0) the statement will not be executed. 10

The if Statement (Example) Suppose a program which writes a message if the student passes the class if (grade >= 60) printf( Passed! ); MulXple statements may be grouped by puang them inside curly braces {}. if (grade >= 60) { printf( Passed! ); totalpassed++; } 11

Example: Write a program that prints the maximum of two numbers entered by the user #include <stdio.h> int main ( ) { int value1, value2, max = 0; printf( Enter two values:\n ); scanf( %d %d, &value1, &value2); if (value1 > value2) max = value1; if (value1 <= value2) max = value2; } printf( %d\n, max); return 0; 12

The if- else Statement Syntax if (condition) statement 1; else statement 2; If the condixon is true (nonzero) statement 1 will be executed. If the condixon is false (zero) statement 2 will be executed. 13

The if- else Statement (Example) Suppose a program which writes a different message if the student passes or fails the class if (grade >= 60) printf( Passed! ); else printf( Failed! ); MulXple statements may be grouped by puang them inside curly braces {}. if (grade >= 60) { printf( Passed! ); totalpassed++; } else { printf( Failed! ); totalfailed++; } 14

Example: (Re)Write a program that prints the maximum of two numbers entered by the user #include <stdio.h> int main ( ) { int value1, value2, max = 0; printf( Enter two values:\n ); scanf( %d %d, &value1, &value2); if (value1 > value2) max = value1; else max = value2; } printf( %d\n, max); return 0; 15

The Dangling else Problem Consider the code below. Which if does the else belong to? if (grade < 60) if (attendance == 100) printf( Passed! ); else printf( Failed! ); Dangling else agaches to the nearest if. Always use curly braces to avoid ambiguous situaxons. if (grade < 60) { if (attendance == 100) printf( Passed! ); else printf( Failed! ); } ß Do NOT forget to get the message here, too J 16

The?: Construct Syntax (expression)? value1 : value2 The statement returns value1 if the expression evaluates to true. Returns value2 otherwise. RevisiXng if- else statement example: printf( "%s\n", grade >= 60? "Passed!" : "Failed!"); Or, it could be wrigen as: grade >= 60? printf("passed! ) : printf("failed!"); 17

Nested if- else Statements Tests for mulxple cases by placing if else selecxon statements inside if else selecxon statement Syntax if (condition 1) statement 1; else if (condition 2) statement 2;... else if (condition n) statement n; else default statement; Once condi<on i is met, rest of statements skipped If no condixon is met, default statement is executed 18

Nested if- else Statements (Example) Code segment for a simple calculator if (operator == + ) result += value; else if (operator == - ) result -= value; else if (operator == * ) result *= value; else if (operator == / ) result /= value; else printf( Unknown operator! ); 19

The switch Statement The switch statement evaluates the value of an expression and branches to one of the case labels. Syntax switch ( expression ) { case constant 1 : statement; break ; case constant n : statement; break; default: statement; break ; } Duplicate labels are not allowed. The expression must evaluate an integer, character, or enumeraxon. 20

The switch Statement (Example) RevisiXng the code segment for a simple calculator switch (operator) { case + : result += value; break; case '-': result -= value; break; case '*': result *= value; break; case '/': result /= value; break; default: printf("unknown operator! ); break; } 21

The switch Statement if (month==1) { printf( Jan. ); } else if (month==2) { printf( Feb. ); } else if (month==3) { printf( Mar. ); } else if (month==4) { printf( Apr. ); } else if (month==5) { printf( May ); } else { printf( Summer ); } Slide credit: B. Huang switch(month) { case 1: printf( Jan. ); break; case 2: printf( Feb. ); break; case 5: printf( May ); break; default: printf( Summer ); break; } 22

Dus/er Corner of the switch Statement break statement exits the switch structure. If a break statement is not there, execuxon will conxnue with the next statement. switch (control) { case 0: printf("reset\n"); case 1: printf("initializing\n"); break; case 2: printf("working\n"); } Program prints: Reset Initializing Because, it is not possible to determine if the program is supposed to fall through from case 0 to case 1, or if the programmer forgot to put in a break statement. case 2 does not need a break as it is the last statement. But, put a break there anyways. 23

The goto Statement Syntax goto label; label: statement Program flow jumps to the statement right ayer the label The goto statement is covered here only for the sake of completeness. Do NOT use it! It makes the logic of the program complex. In modern programming, goto statement is considered a harmful construct and a bad programming pracxce. any program can be perfectly wrigen without the use of goto statement. 24

Next Week Condi/onal Branching Logical Expressions if and If- else statements switch statement goto statement Itera/on Control Loop Statements for, while, do- while structures break and con/nue 25