1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Computing for management - Chapter 25

70 32 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 70
Dung lượng 1,35 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Lecture 25

Trang 2

Summary 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 3

Summary 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 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.

Trang 7

The 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 8

What 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 9

Read 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 16

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) {

Trang 17

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) {

Trang 18

Notes on if

 Which of the following code fragments are equivalent?

if (number % 2 != 0)

{ printf("%d", number);

Trang 19

Notes on if

 Common mistake

if (number % 2 != 0);

{ printf("%d is an odd ", number); }

printf("number\n");

Trang 20

Notes 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 21

Notes on if

 Common mistake

if (number = 0) {

printf("%d\n", number); }

printf("%d\n", number);

Trang 22

Notes on if

 Common mistake

if (number = 0) {

printf("%d\n", number); }

printf("%d\n", number);

Should be  ==

Trang 23

The 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 29

Output of a program

Trang 31

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”

}

Trang 32

int 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 33

int 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 34

int 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 36

int 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 37

int 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 38

int 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 39

int 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 40

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

Trang 41

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

Trang 48

Switch Statement

Trang 49

multi- Multi-way selection chooses among

several alternatives C programming can use switch statement for multi-way

selection.

Trang 51

The Switch statement

Expression here is user entered variable value

Trang 52

The Switch statement

case is a default word.

Trang 53

The Switch statement Expression is matched with the constant of each

case, wherever it is matched, next statements

are executed

Trang 54

The Switch statement

If the expression doesn’t matched with any case

default is executed.

Trang 55

Closed switch resembles

executed case

Trang 56

Switch statement

Trang 57

printFlag is a variable

Trang 59

In order to stop next case to

be automatically executed,

break

statement has been used.

Trang 60

Multiple cases being executed together

Trang 61

Summary of switch Statement Rules

Trang 62

Develop the program for assigning the grades using if and switch

statements separately.

Trang 63

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;

Remember you need constants

here, so explicitly write

all statements for which this

case may be executed.

Switch statement

Trang 64

case 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 66

Alternative If statements

Trang 67

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!

Trang 68

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"); }

Trang 69

else 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.

Ngày đăng: 30/01/2020, 16:36

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN