Download presentation
Presentation is loading. Please wait.
Published byAlexis Perkins Modified over 6 years ago
1
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection
2
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
3
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
4
Today’s Topics The if statement The else statement Cascaded if
Nested if Switch statement
5
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.
6
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.
7
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
8
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.
9
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 }
10
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 }
11
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 }
12
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 }
13
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!
14
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!
15
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 }
16
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"); }
17
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"); }
18
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"); }
19
Notes on if Common mistake if (number % 2 != 0); {
printf("%d is an odd ", number); } printf("number\n");
20
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.
21
Notes on if Common mistake if (number = 0) { printf("%d\n", number); }
22
Notes on if Common mistake if (number = 0) { printf("%d\n", number); }
Should be ==
23
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
24
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”
25
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”
26
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!
27
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”
29
Output of a program
30
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.
31
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”
32
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;
33
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;
34
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;
35
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 )
36
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;
37
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;
38
“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.
39
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;
40
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’
41
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”);
42
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’);
43
Example: Nested if if ( coldWeather ) { wearJumper = 1;
wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) wearThermal = 1; }
44
Example: Nested if if ( coldWeather ) { wearJumper = 1;
wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) wearThermal = 1; }
45
Example: Nested if if ( coldWeather ) { wearJumper = 1;
wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) wearThermal = 1; }
46
Example: Nested if if ( coldWeather ) { wearJumper = 1;
wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) wearThermal = 1; }
47
If conditions summary The if statement The else statement Cascaded if
Nested if Common mistakes
48
Switch Statement
49
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.
51
The Switch statement Expression here is user entered variable value
52
The Switch statement case is a default word.
53
The Switch statement Expression is matched with the constant of each
case, wherever it is matched, next statements are executed The Switch statement
54
If the expression doesn’t
The Switch statement If the expression doesn’t matched with any case default is executed.
55
Closed switch resembles
executed case
56
Switch statement
57
printFlag is a variable
59
In order to stop next case to be automatically executed, break
statement has been used.
60
Multiple cases being executed together
61
Summary of switch Statement Rules
62
statements separately.
Develop the program for assigning the grades using if and switch statements separately.
63
#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.
64
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.
65
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!
66
Alternative If statements
67
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!
68
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
69
Integer value is returned by this program!
else printf("%s", "you got F grade"); return 0; } Integer value is returned by this program!
70
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.