CSE1301 Computer Programming: Lecture 27 Game Programming: Bingo.
1 CSC103: Introduction to Computer and Programming Lecture No 8.
-
Upload
godwin-miller -
Category
Documents
-
view
218 -
download
0
Transcript of 1 CSC103: Introduction to Computer and Programming Lecture No 8.
![Page 1: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/1.jpg)
1
CSC103: Introduction to Computer and Programming
Lecture No 8
![Page 2: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/2.jpg)
2
Previous Lecture
• Example programs – printf, scanf • Decision control structure• Relational operator in C• if statement• if-else statement
![Page 3: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/3.jpg)
3
Today’s lecture outline
• Nested if else statement• Logical operator in C• Conditional operator
![Page 4: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/4.jpg)
4
Nested if else
• It is possible to write an entire if-else with either the if statement block or else statement block
![Page 5: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/5.jpg)
5
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 == 1YesNo
x == 2No Yes Display “you
entered 1”
End
Display “you entered 2”
![Page 6: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/6.jpg)
6
Forms of if statement
![Page 7: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/7.jpg)
7
Cont.
![Page 8: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/8.jpg)
8
Logical Operators• && (logical AND)– true if both conditions are trueif ( gender == 1 && age >= 65 ) senior++;
• || (logical OR)– true if either of condition is trueif (semesterAvg >= 90 || finalExam >=90 )
printf("Student grade is A“);
![Page 9: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/9.jpg)
9
Cont.
• ! (logical NOT, logical negation)– Returns true when its condition is false, & vice
versaif ( !( grade == 20 ) ) printf(“hello world“);
Alternative:if ( grade != 20 ) printf(“hello world“);
False! if (True)
True! if (False)
![Page 10: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/10.jpg)
10
Logical Operators..
![Page 11: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/11.jpg)
11
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
![Page 12: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/12.jpg)
12
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.
![Page 13: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/13.jpg)
13
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 female and age is > 25)
![Page 14: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/14.jpg)
14
&& and ||
• && and || are useful in the following programming situations– When after testing several conditions the outcome
is only one of the two answers – When it is to be tested whether a value falls
within a particular range or not Write a Program
![Page 15: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/15.jpg)
15
Example program 3
• Write a program to calculate the salary as per the following table:
![Page 16: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/16.jpg)
16
Revised Hierarchy
![Page 17: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/17.jpg)
17
Caution
• Between assignment /equality operatorif ( i = 5 ) printf ( "You entered 5" ) ;else printf ( "You entered something other than 5"
);• Null statement
if ( i == 5 ) ; printf ( "You entered 5" )
![Page 18: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/18.jpg)
18
Conditional Operators
• General form is, (expression 1 ? expression 2 : expression 3);
• Conditional operators ? and : are sometimes called ternary operators
• if expression 1 is true, then the value returned will be expression 2, otherwise the value returned will be expression 3
![Page 19: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/19.jpg)
19
Examples
A
![Page 20: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/20.jpg)
20
Cont.B
C
D
![Page 21: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/21.jpg)
21
Cont..
E
The limitation of the conditional operators is that after the ? or after the : only one C statement can occur.
![Page 22: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/22.jpg)
22
Examplemain( ) { int x, y; printf ("Enter the salary" ) ; scanf ( "%d", &x ) ; if ( x > 10 ) { printf ( “%d“, x ) ; y = x+10; } else y = x; }
(expression 1 ? expression 2 : expression 3);
condition Single statement
Single statement
![Page 23: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/23.jpg)
23
Example Program
Using conditional operators determine: Whether the character entered through the keyboard is a Upper case alphabet or not.
Write a Program
![Page 24: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/24.jpg)
24
Nested conditional operator
(expression 1 ? expression 2 : expression 3);
Single condition or compound condition
(expression 1 ? expression 2 : expression 3);
(expression 1 ? expression 2 : expression 3);
![Page 25: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/25.jpg)
25
Example program
main( ) { float sal ; printf ("Enter the salary" ) ; scanf ( "%f", &sal ) ; if ( sal < 40000 && sal > 25000 ) printf ( "Manager" ) ; else if ( sal < 25000 && sal > 15000 ) printf ( "Accountant" ) ; else printf ( "Clerk" ) ; }
![Page 26: 1 CSC103: Introduction to Computer and Programming Lecture No 8.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649cf65503460f949c5cd8/html5/thumbnails/26.jpg)
26
Cont.main( ) { float sal ; printf ("Enter the salary" ) ; scanf ( "%f", &sal ) ;
( (sal < 40000 && sal > 25000) ? printf ( "Manager" ) :
(( sal < 25000 && sal > 15000 ) ? printf ( "Accountant") :
printf ( "Clerk" ) )); }