Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 7

Lecture-08

Introduction to programming
Objectives
❏ Conditional Operator
❏ Logical Operators
❏ goto statement


Ghalib Univeristy: By Farman Ali 1


Conditional Operator
 The conditional operator is used as an alternative of a simple
if-else statement.
 The conditional operator consists of “?” (question-mark)
and a “:” (colon).
 Its syntax is:
 (condition)?(Exp-1):(Exp-2);
 Write a program to input two values. Use conditional
operator to find out which value is greater. Print the message
on the screen to indicate the greater value.
 Write a program to input an integer value if the entered
value is divisible by 2 print “Even” otherwise print “Odd” on
computer screen. Use conditional operator.
Ghalib Univeristy: By Farman Ali 2
Logical Operators
 The logical operators are used to combine relational
expressions or relational conditions.
 The expression containing logical operators is called logical
expression or logical condition or compound condition or
compound expression.
 In C/C++ the following Logical operators are used.
 (i) && AND Operator
 (ii) || OR Operator
 (iii) ! NOT Operator
 (i) && (AND Operator):
 The && (AND) operator is used to combine two or more
relational expressions.
Ghalib Univeristy: By Farman Ali 3
Cont…
 If all relational expressions are true then the output returned
by the compound expression is true. If any one of relation
expression in the compound expression is false, the output is
false.
 Example by program. Find the greatest value in three values.
 Write a program to find out the grade of a student based on
the marks obtained in three subjects using compound
condition.
 (ii) || (OR Operator):
 The || (OR) Operator is used to combine more than one
relational expression. If any one of the given relational
expression is true, the output will be true otherwise the output
will be false.
 Relevant Example if((a==2) || (a==3))
Ghalib Univeristy: By Farman Ali 4
Cont…
 iii- ! (NOT Operator):
 The ! (NOT) operator inverts the value returned by the
relational expression.
 for example if x=3 and y=4 then the logical expression !(x>y)
returns true.

Ghalib Univeristy: By Farman Ali 5


The “goto” Statement
 The “goto” statement is an unconditional control transfer
statement.
 It is used to transfer the control to a specified label in the
same program.
 The syntax of the “goto” statement is:
 goto label;
 “goto” is a keyword, and label can be any name followed by a
colon just like a variable name.

 Write a program to transfer the control from one statement to


another by using the “goto” statement.
 Write a program that print the first ten natural numbers, Use
“goto” and “if” statements.
Ghalib Univeristy: By Farman Ali 6
Have a nice day

Ghalib Univeristy: By Farman Ali 7

You might also like