TAILIEUCHUNG - Lecture Elementary programming with C - Session 5: Condition

Lecture Elementary programming with C - Session 5: Condition. After completing this chapter, students will be able to: Explain the selection construct: if statement, if – else statement, multi if statement, -nested if statement; switch statement. | Condition Session 5 Objectives Explain the Selection Construct - If Statement - If – else statement - Multi if statement - Nested if statement Switch statement Conditional Statement Conditional statements enable us to change the flow of the program A conditional statement evaluates to either a true or a false value Example : To find whether a number is even or odd we proceed as follows : Accept a number Find the remainder by dividing the number by 2 If the remainder is zero, the number is “EVEN” Or if the remainder is not zero the number is “ODD” Selection Constructs C supports two types of selection statements The if statement The switch statement The if statement-1 Syntax: If the if expression evaluates to true, the block following the if statement or statements are executed The if statement-2 #include void main() { int x, y; char a = ‘y’; x = y = 0; if (a == ‘y’) { x += 5; printf(“The numbers are %d and \t%d”, x, y); } } Program to display the values based on a condition Example The if – else statement-1 Syntax: The if – else statement-2 If the if expression does not evaluate to true then the statements following the else expression take over control The else statement is optional. It is used only if a statement or a sequence of statements are to be executed in case the if expression evaluates to false If the if expression evaluates to true, the block following the if statement or statements are executed The if – else statement -3 #include void main() { int num , res ; printf(“Enter a number :”); scanf(“%d”,&num); res = num % 2; if (res == 0) printf(“Then number is Even”); else printf(“The number is Odd”); } Program to display whether a number is Even or Odd Example The if–else–if statement-1 Syntax: The if – else – if statement is also known as the if-else-if ladder or the if-else-if staircase The conditions are evaluated from the top downwards The if–else–if statement-2 #include main() { int x; x = 0; clrscr (); printf(“Enter

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.