Title: Operators and IF Statement
1Operators and IF Statement
January 24 to Feb 1, 2008
Turbo C Programming
2Objectives
- To use relational and logical operators properly
in a C program - To use If statement correctly in a C program
- To determine the output of expressions involving
relational and logical operators
3Group work
Make an If statement/script that will evaluate
whether the user is a voter or not. Please note
that the voting age is 18 and above. If the
variable, AGE, holds a value that is equal or
greater than 18, display the message, You are a
voter otherwise, display the message You are
not yet a voter. Check the sites below to
review the previous lessons on If
statement http//www.vbtutor.net/lesson7.html htt
p//www.tizag.com/aspTutorial/aspIfStatement.php
4Type your answers in the corresponding boxes
below. Discuss the answers as a group. Use the
old groupings.
Be ready to share your answers to the class.
5What's up
If statement uses relational operators in
evaluating expression or condition. The
relational operators are listed in the table
below
6The 3 common logical operators are And(),
Or() and NOT(!).
pword123 is an example of expression or
condition.
7Directions Determine the result of the
expressions below. Use 1 for True and 0 for
False. Given x10 y5 1. xgt1 Result
______ 2. ygtx Result ______ 3. xgt11
ygt0 Result ______ 4. ygt5 xgt0 Result
______ 5. !(xgt10) Result ______ Be ready to
share your answers to the class.
8IF Conditional Statement If statement is an
example of a control structure. It controls the
flow of execution in a program or function. The
general form of If statement is If
(expression) statement else statement The
general form of If with blocks of statements
is If (expression) statement
sequence else statement sequence
The statement shall be processed if the
expression returns True
The statement shall be processed if the
expression returns False
9The if-else-if Ladder If (expression) Statemen
t else if (expression) Statement else if
(expression) Statement . . else Statement
10Hands-on1
- The Grade Activity
- Make the subfolder named If Grade in the 3rd term
subfolder. - Type the code below in the coding window
- / This program evaluates the value of the
variable Grade. If the value is greater than 74,
Passed must be displayed on the monitor
otherwise, Failed shall be seen./ - includeltstdio.hgt
- includeltconio.hgt
- void main()
-
- int grade
- printf(Pls enter your grade in Computer)
- scanf(d,grade)
- if (gradegt74) printf(Passed!)
- else printf(Failed!)
- getch()
113. Save the program as ifgrade.cpp in the If
Grade subfolder. 4. Make an EXE
file. Comprehension Questions Directions Type
your answers in the space provided after each
question. 1.What is the variable used in the
program? 2.What is the condition used in the
program? 3.What would be the output if the user
enters the grade 80?
12Hands-on2
The Password Activity 1. Make the subfolder
named If Password in the 3rd term subfolder. 2.
Type the code below in the coding
window /Programmed by Your name/ includeltstdi
o.hgt includeltconio.hgt void main() Int
pword printf(Pls enter your passwordr) scanf(
d,pword) if (pword123 pword321)
printf(Welcome!) else printf(Access
denied!) getch()
13- 3. Save the program as ifpassword.cpp in the If
Password subfolder. - 4. Make an EXE file.
- Comprehension Questions
- Directions Type your answers in the space
provided after each question. - What is the variable used in the program?
- 2. What are the conditions used in the program?
- 3. What would be the output if the user enters
the value 321?
14Get ready for
Quiz 1