Lecture 25 - Selection. In the previous lecture we have been covered algorithms and programs, in this chapter you will be able to understand: The if statement, the else statement, cascaded if, nested if, switch statement.
Trang 1Lecture 25
Trang 2Summary of previous lecture
In the previous lecture we have been covered,
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
Trang 3Summary of previous lecture
Trang 5 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.
Trang 6Conditions 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.
Trang 7The if 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
Trang 8What is a statement?
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.
{ number = number + 1;
printf("%d\n", number);
}
• An empty statement is a single semicolon.
E.g.
Trang 9Read in a number, and print it
Trang 10#include <stdio.h>
/* Read in a number, and echo it
if it is odd */
int main() {
Trang 11#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);
Trang 12#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); }
Trang 13#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); }
Trang 14#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); }
Example: oddnum.c
Trang 15#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); }
Trang 16Notes 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) {
Trang 17Notes 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) {
Trang 18Notes on if
Which of the following code fragments are equivalent?
if (number % 2 != 0)
{ printf("%d", number);
Trang 19Notes on if
Common mistake
if (number % 2 != 0);
{ printf("%d is an odd ", number); }
printf("number\n");
Trang 20Notes on if
Common mistake
if (number % 2 != 0) ;
{ printf("%d is an odd ", number); }
printf("number\n");
No semi
colon here!
The semicolon is an empty statement.
Trang 21Notes on if
Common mistake
if (number = 0) {
printf("%d\n", number); }
printf("%d\n", number);
Trang 22Notes on if
Common mistake
if (number = 0) {
printf("%d\n", number); }
printf("%d\n", number);
Should be ==
Trang 23The else statement
Can only occur after an if statement
Is only executed when the if block does
Trang 24#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);
}
}
Read in a number, and
determine if it’s odd or
Trang 25#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", number);
}
}
Example: oddeven.c
Read in a number, and
determine if it’s odd or
Trang 26#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", number);
} }
Example: oddeven.c
Read in a number, and
determine if it’s odd or
Trang 27#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", number);
} }
Example: oddeven.c
Read in a number, and
determine if it’s odd or
Trang 29Output of a program
Trang 31Determine 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”
}
Trang 32int main() {
return 0; }
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;
Trang 33int main() {
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;
Trang 34int main() {
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;
Trang 35#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;
int main() {
Trang 36int main() {
printf("30 days\n");
} else if (month==February) {
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;
Trang 37int main() {
printf("30 days\n");
} else if (month==February) {
printf("28 or 29 days\n");
} else { printf("31 days\n");
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;
Trang 38int main() {
printf("30 days\n");
} else if (month==February) {
printf("28 or 29 days\n");
} else { printf("31 days\n");
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.
Trang 39int main() {
printf("30 days\n");
} else if (month==February) {
printf("28 or 29 days\n");
} else { printf("31 days\n");
} return 0;
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;
Trang 40Notes 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’) {
Trang 41Notes 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’) {
Trang 48Switch Statement
Trang 49multi- Multi-way selection chooses among
several alternatives C programming can use switch statement for multi-way
selection.
Trang 51The Switch statement
Expression here is user entered variable value
Trang 52The Switch statement
case is a default word.
Trang 53The Switch statement Expression is matched with the constant of each
case, wherever it is matched, next statements
are executed
Trang 54The Switch statement
If the expression doesn’t matched with any case
default is executed.
Trang 55Closed switch resembles
executed case
Trang 56Switch statement
Trang 57printFlag is a variable
Trang 59In order to stop next case to
be automatically executed,
break
statement has been used.
Trang 60Multiple cases being executed together
Trang 61Summary of switch Statement Rules
Trang 62Develop the program for assigning the grades using if and switch
statements separately.
Trang 63int 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;
Remember you need constants
here, so explicitly write
all statements for which this
case may be executed.
Switch statement
Trang 64case 89:case 88:case 87:case 86:case 85:case 84: case 83:case 82:case 81:case 80:
printf("%s", "You got B grade");
Trang 66Alternative If statements
Trang 67int 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!
Trang 68printf("%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"); }
Trang 69else printf("%s", "you got F grade");
Trang 70 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.