Lecture Introduction to computer and programming - Lecture No 8

The contents of this chapter include all of the following: Example programs – printf, scanf; decision control structure; relational operator in C; if statement; if-else statement; nested if else statement; logical operator in C; conditional operator. | CSC103: Introduction to Computer and Programming Lecture No 8 Previous Lecture Example programs – printf, scanf Decision control structure Relational operator in C if statement if-else statement Today’s lecture outline Nested if else statement Logical operator in C Conditional operator Nested if else It is possible to write an entire if-else with either the if statement block or else statement block Nested if else flowchart Start Display “Enter either 1 or 2” Read x Display “you entered other than 1 or 2” Go to program x == 1 Yes No x == 2 No Yes Display “you entered 1” End Display “you entered 2” Forms of if statement Cont. Logical Operators && (logical AND) true if both conditions are true if ( gender == 1 && age >= 65 ) senior++; || (logical OR) true if either of condition is true if (semesterAvg >= 90 || finalExam >=90 ) printf("Student grade is A“); Cont. ! (logical NOT, logical negation) Returns true when its condition is false, & vice versa if ( !( grade == 20 ) ) printf(“hello world“); Alternative: if ( grade != 20 ) printf(“hello world“); False ! if (True) True ! if (False) Logical Operators Sample Program -1 To calculate the division Input: marks of 5 different subjects Rules Percentage above or equal to 60 - First division Percentage between 50 and 59 - Second division Percentage between 40 and 49 - Third division Percentage less than 40 – Fail Solution Nested if-else Logical operators Go to Program Go to Program Sample Program -2 A company insures its drivers in the following cases: If the driver is married If the driver is unmarried, male & above 30 years of age If the driver is unmarried, female & above 25 years of age If the marital status, gender and age of the driver are the inputs, write a program to determine whether the driver is to be insured or not. Flowchart Go to Program without logical operator Go to Program with logical operator (ms is married) OR (ms is unmarried and g is male and age is > 30) OR (ms is unmarried and g is . | CSC103: Introduction to Computer and Programming Lecture No 8 Previous Lecture Example programs – printf, scanf Decision control structure Relational operator in C if statement if-else statement Today’s lecture outline Nested if else statement Logical operator in C Conditional operator Nested if else It is possible to write an entire if-else with either the if statement block or else statement block Nested if else flowchart Start Display “Enter either 1 or 2” Read x Display “you entered other than 1 or 2” Go to program x == 1 Yes No x == 2 No Yes Display “you entered 1” End Display “you entered 2” Forms of if statement Cont. Logical Operators && (logical AND) true if both conditions are true if ( gender == 1 && age >= 65 ) senior++; || (logical OR) true if either of condition is true if (semesterAvg >= 90 || finalExam >=90 ) printf("Student grade is A“); Cont. ! (logical NOT, logical negation) Returns true when its condition is false, & vice versa if ( !( grade == 20 ) ) .

Bấm vào đây để xem trước nội dung
TÀI LIỆU MỚI ĐĂNG
153    82    4    27-04-2024
Đã 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.