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

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã 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.