1 CSE1301 Computer Programming Lecture 8 Selection.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 CSE1301 Computer Programming Lecture 5 C Primitives 2.
CSE1301 Computer Programming Lecture 4: C Primitives I.
1 CSE1301 Computer Programming Lecture 9 Selection.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
FIT Objectives By the end of this lecture, students should: understand selection statements understand nested selection understand tradeoff.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Conditional Statement
J-1 University of Washington Computer Programming I Switch Statement © 2000 UW CSE.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
WEEK 4 Class Activities Lecturer’s slides.
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 1 If statement (preview version) Syntax form: if ( condition) Statements else Statements.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
CSE 220 – C Programming Loops.
Bill Tucker Austin Community College COSC 1315
Decisions Chapter 4.
Lecture 7: Repeating a Known Number of Times
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
CS1010 Programming Methodology
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Introduction to C Programming
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Chapter 13 Control Structures
INC 161 , CPE 100 Computer Programming
Chapter 13 Control Structures
Writing a program with conditionals
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
CSCE 206 Lab Structured Programming in C
Chapter 4 - Program Control
CSE 1020:Control Structure
CSC215 Lecture Flow Control.
Conditions and Boolean Expressions
CSC215 Lecture Control Flow.
CSI 121 Structure Programming Language Lecture 9 Selection
CSC215 Homework Homework 03 Due date: Oct 07, 2016.
Relational, Logical, and Equality Operators
Computer Programming Techniques Semester 1, 1998
Introduction to Computing Lecture 04: Booleans & Selection
Lecture 4: Selection Control Structure
CSC215 Lecture Control Flow.
CSCE 206 Lab Structured Programming in C
Chapter 13 Control Structures
Presentation transcript:

1 CSE1301 Computer Programming Lecture 8 Selection

2 Topics The if statement The else statement Cascaded if Nested if Readings: D&D (2/e or 3/e): Sections 3.4 to 3.6 and 4.10 to 4.11

3 The if statement Determines whether a block is executed. Implements the selection instructions within an algorithm. Decides what to do by evaluating a Boolean expression. If the expression is true (non-zero), the block is executed.

4 Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number } Example: oddnum.c

5 #include /* Read in a number, and echo it if it is odd. */ int main() { return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

6 #include /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

7 #include /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

8 #include /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number } Do not put “then” here!

9 #include /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number } Do not put semicolon here!

10 #include /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Example: oddnum.c Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

11 Notes on if Which of the following code fragments are equivalent? if (number % 2 != 0) { printf("%d", number); } printf(” is odd\n"); if (number % 2 != 0) printf("%d", number); printf(” is odd\n"); if (number % 2 != 0) { printf("%d", number); printf(” is odd\n"); } A B C

12 Notes on if Which of the following code fragments are equivalent? if (number % 2 != 0) { printf("%d", number); } printf(” is odd\n"); if (number % 2 != 0) printf("%d", number); printf(” is odd\n"); if (number % 2 != 0) { printf("%d", number); printf(” is odd\n"); } A B C

13 Notes on if Common mistake if (number % 2 != 0); { printf("%d is an odd ", number); } printf("number\n");

14 Notes on if Common mistake if (number % 2 != 0); { printf("%d is an odd ", number); } printf("number\n"); No semi- colon here!

15 Notes on if Common mistake if (number = 0) { printf("%d\n", number); } printf("%d\n", number);

16 Notes on if Common mistake if (number = 0) { printf("%d\n", number); } printf("%d\n", number); Should be ==

17 The else statement Can only occur after an if statement Is only executed when the if ’s block does not execute

18 #include /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } Example: oddeven.c Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” }

19 #include /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); } Example: oddeven.c Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” }

20 #include /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); } Example: oddeven.c Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” } No semicolons here!

21 #include /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); } Example: oddeven.c Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” }

22 Cascaded if statement Multiple alternative blocks each with a Boolean expression. First expression which evaluates to true causes execution of the associated block. Only at most one block will be executed.

23 Example: months.c Determine the number of days in a given month: 30 days hath September, April, June and November. All the rest hath 31, Excepting February alone, Which hath 28 days clear, And 29 in each leap year. output “Enter an integer” input month if (month is September, or April, or June, or November) then { output “30 days” } else if (month is February) { output “28 or 29 days” } else { output “31 days” }

24 int main() { return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

25 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

26 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

27 #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2; int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } return 0; } Example: months.c Common mistake: if (month==September || April || June || November )

28 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

29 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } else { printf("31 days\n"); } return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

30 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } else { printf("31 days\n"); } return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2; “Default” block.

31 int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } else { printf("31 days\n"); } return 0; } Example: months.c #include /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

32 Notes on Cascaded if if (letter >= ’a’) { printf(“S1\n”); } else if (letter <= ’z’) { printf(“S2\n”); } else if (letter >= ’A’) { printf(“S3\n”); } else if (letter <= ’Z’) { printf(“S4\n”); } What is the output if: letter is equal to ‘b’ letter is equal to ‘z’ letter is equal to ‘A’ letter is equal to ‘X’ Q:

33 More Examples if (ch >= ’a’ && ch <= ’z’) { printf(“%c is in lower case.\n”, ch); } else if (ch >= ’A’ && ch <= ’Z’) { printf(“%c is in upper case.\n”. ch); } else if (ch >= ’0’ && ch <= ’9’) { printf(“%c is a digit with value %d.\n”, ch, ch - ’0’); }

34 if (ch >= ’a’ && ch <= ’z’) { printf(“%c is in lower case.\n”, ch); } else if (ch >= ’A’ && ch <= ’Z’) { printf(“%c is in upper case.\n”. ch); } else if (ch >= ’0’ && ch <= ’9’) { printf(“%c is a digit with value %d.\n”, ch, ch - ’0’); } More Examples

35 if ( coldWeather ) { wearJumper = 1; wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) { wearThermal = 1; } Example: Nested if

36 if ( coldWeather ) { wearJumper = 1; wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) { wearThermal = 1; } Example: Nested if

37 if ( coldWeather ) { wearJumper = 1; wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) { wearThermal = 1; } Example: Nested if

38 if ( coldWeather ) { wearJumper = 1; wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) { wearThermal = 1; } Example: Nested if

39 Next Lecture Reading: D&D (2/e or 3/e) Chapter 3, Section 3.7 to 3.8 Chapter 4, Sections 4.1 to 4.6 Sections 4.8 to 4.11