CSE1301 Sem 2-2003 Selection Lecture 25 Lecture 9: Selection.

Slides:



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

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 CSE1301 Computer Programming Lecture 8 Selection.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
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 9 Selection.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
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
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
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 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
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.
Control Statements: Part1  if, if…else, switch 1.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 5: Structured Programming
CSE 220 – C Programming Loops.
Chapter 3 Control Statements
Administrative things
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 2 - Introduction to C Programming
Selections Java.
Decisions Chapter 4.
Lecture 7: Repeating a Known Number of Times
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Week 3 C Program Structures (Selection Structures)
Chapter 4 (Conditional Statements)
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Week 3 – Selection Structures
CS1010 Programming Methodology
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 2 - Introduction to C Programming
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Introduction to C Programming
Introduction to Programming
CS1100 Computational Engineering
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
Chapter 5 Decision Making and Branching
Structured Program
Chapter 4 - Program Control
Introduction to C Programming
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Conditions and Boolean Expressions
CSC215 Lecture Control Flow.
Chapter 7 Conditional Statements
CSI 121 Structure Programming Language Lecture 9 Selection
Relational, Logical, and Equality Operators
Week 3 – Program Control Structure
Control Structures Lecture 6.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Introduction to Computing Lecture 04: Booleans & Selection
Lecture 4: Selection Control Structure
CSC215 Lecture Control Flow.
CSCE 206 Lab Structured Programming in C
Switch Case Structures
Chapter 13 Control Structures
Presentation transcript:

CSE1301 Sem 2-2003 Selection Lecture 25 Lecture 9: Selection

Summary of previous lecture In the previous lecture we have been covered, Algorithms and Programs A C programming language History of C C, A High level language How to get started with C. Basic Structure of a C program Data Storage and Data Types Variables, Keywords, identifiers, Assignment

Summary of previous lecture constant variable printf() and scanf() functions and usage Precedence int and float Unary operations Increment and decrement operations Comments Error and its Types

Today’s Topics The if statement The else statement Cascaded if Nested if Switch statement

Conditions We come across conditions in daily life, For example If you will get less than 50 marks then you will fail. If team scored less than 250 runs, it will be out of the tournament. If the temperature is greater than 50 degree Celsius then wear light dress.

Conditions in C Programming Same if structure is used in c programming to test the condition. It has two parts Conditional or antecedent or LHS part and Conclusion or action or consequent or RHS part.

The if statement if ( expression ) statement Determines whether a statement or 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 statement or block is executed. if ( expression ) statement

What is a statement? An empty statement is a single semicolon. Statements are lines of instructions in our programs ending with a semicolon (;). A compound statement or block is a series of statements surrounded by braces. E.g. { number = number + 1; printf("%d\n", number); } An empty statement is a single semicolon.

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 }

Example: oddnum.c #include <stdio.h> /* Read in a number, and echo it if it is odd. */ int main() { return 0; } 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 #include <stdio.h> /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); return 0; } 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 #include <stdio.h> /* 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; 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 Do not put “then” here! #include <stdio.h> /* 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; 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!

Do not put semicolon here! Example: oddnum.c #include <stdio.h> /* 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; 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!

Example: oddnum.c #include <stdio.h> /* 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; 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 }

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

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

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

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

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

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

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

The else statement Can only occur after an if statement Is only executed when the if block does not execute if ( expression ) statement1 else statement2

Example: oddnum.c #include <stdio.h> /* 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: oddnum.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”

Example: oddeven.c #include <stdio.h> /* 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", 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”

Example: oddeven.c No semicolons here! #include <stdio.h> /* 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", 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!

Example: oddeven.c #include <stdio.h> /* 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", 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”

Output of a program

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

Example: months.c Determine the number of days in a given month: 30 days = September, April, June and November. All the rest have 31, Excepting February alone, Which have 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”

Example: months.c int main() { return 0; } #include <stdio.h> /*************************\ Determine the number of days in a given month: 30 days = 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;

Example: months.c int main() { int month; printf("Enter number of month: "); scanf("%d", &month); return 0; } #include <stdio.h> /*************************\ Determine the number of days in a given month: 30 days = 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;

Example: months.c 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; #include <stdio.h> /*************************\ 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;

if (month==September || April || June || November ) Example: months.c 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; #include <stdio.h> /*************************\ 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; Common mistake: if (month==September || April || June || November )

Example: months.c 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; #include <stdio.h> /*************************\ 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;

Example: months.c 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; #include <stdio.h> /*************************\ 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. Example: months.c 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; #include <stdio.h> /*************************\ 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.

Example: months.c 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; #include <stdio.h> /*************************\ 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;

Q: Notes on Cascaded if What is the output if: letter is equal to ‘b’ 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”); Q: 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’

A: Notes on Cascaded if What is the output if: letter is equal to ‘b’ Answer=S1 letter is equal to ‘z’ letter is equal to ‘A’ Answer=S2 letter is equal to ‘X’ Answer = S2 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”);

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’);

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

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

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

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

If conditions summary The if statement The else statement Cascaded if Nested if Common mistakes

Switch Statement

Multi-way Selection In addition to two-way selection (if else), most programming languages provide another selection concept known as multi-way selection. Multi-way selection chooses among several alternatives. C programming can use switch statement for multi-way selection.

The Switch statement Expression here is user entered variable value

The Switch statement case is a default word.

The Switch statement Expression is matched with the constant of each case, wherever it is matched, next statements are executed The Switch statement

If the expression doesn’t The Switch statement If the expression doesn’t matched with any case default is executed.

Closed switch resembles executed case

Switch statement

printFlag is a variable

In order to stop next case to be automatically executed, break statement has been used.

Multiple cases being executed together

Summary of switch Statement Rules

statements separately. Develop the program for assigning the grades using if and switch statements separately.

#include<stdio.h> int main() { int marks; printf("%s", "Enter yours Marks"); scanf("%d",&marks); switch(marks) case 100:case 99:case 98:case 97:case 96:case 95:case 94: case 93:case 92:case 91:case 90: printf("%s", "You got A grade"); break; Switch statement Remember you need constants here, so explicitly write all statements for which this case may be executed.

case 89:case 88:case 87:case 86:case 85:case 84: printf("%s", "You got B grade"); break; case 79:case 78:case 77:case 76:case 75:case 74: case 73:case 72:case 71:case 70: printf("%s", "You got C grade"); case 69:case 68:case 67:case 66:case 65:case 64: case 63:case 62:case 61:case 60: printf("%s", "You got D grade"); In case any of these cases execute, control from case statement will be returned.

If user enters some wrong value default will be executed! printf("%s", "You got F grade"); break; } return 0; If user enters some wrong value default will be executed!

Alternative If statements

First Check, if true rest of the statements will not be executed #include<stdio.h> int main() { int marks; printf("%s", "Enter yours Marks"); scanf("%d",&marks); if (marks >=90) printf("%s", "you got A grade"); } else if (marks >=80) User inputs marks First Check, if true rest of the statements will not be executed If first check is false, this statement will be checked and so on!

Logical comparison in if statement, which is not permitted in case { printf("%s", "you got B grade"); } else if (marks >=70) printf("%s", "you got c grade"); else if (marks >=60) printf("%s", "you got D grade"); Logical comparison in if statement, which is not permitted in case

Integer value is returned by this program! else printf("%s", "you got F grade"); return 0; } Integer value is returned by this program!

Summary If conditions are used to execute statement or a group of statements based upon logically tested values. Alternative to if condition is switch statement. Both if and switch statements form logical structure of a program.