Presentation is loading. Please wait.

Presentation is loading. Please wait.

If Statements & Relational Operators Programming.

Similar presentations


Presentation on theme: "If Statements & Relational Operators Programming."— Presentation transcript:

1

2 If Statements & Relational Operators Programming

3 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 2 Conditional Statements l A conditional statement allows us to control whether a program segment is executed or not. l Two constructs if statement –if –if-else –if-else-if switch statement

4 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 3 The Basic if Statement l Syntax if(condition) action l if the condition is true then execute the action. l action is either a single statement or a group of statements within braces. condition action true false

5 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 4 Choice ( if ) l Put multiple action statements within braces if (it's raining){ }

6 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 5 Absolute Value // program to read number & print its absolute value #include using namespace std; int main(){ int value; cout << "Enter integer: "; cin >> value; if(value < 0) value = -value; cout << "The absolute value is " << value << endl; return 0; }

7 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 6 Relational Operators Relational operators are used to compare two values to form a condition. MathC++Plain English === equals [example: if(a==b) ] [ (a=b) means put the value of b into a ] << less than  <= less than or equal to >> greater than  >= greater than or equal to  != not equal to

8 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 7 Conditions Examples: Number_of_Students < 200 10 > 20 20 * j == 10 + i

9 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 8 Operator Precedence Which comes first? * / % + - = > == != = Answer:

10 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 9 The Boolean Type C++ contains a type named bool for conditions. l A condition can have one of two values: true (corresponds to a non-zero value) false (corresponds to zero value) l Boolean operators can be used to form more complex conditional expressions. The and operator is && The or operator is || The not operator is !

11 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 10 The Boolean Type l Truth table for "&&" (AND): Operand1Operand2Operand1 && Operand2 true false truefalse

12 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 11 The Boolean Type l Truth table for “||" (OR): Operand1Operand2Operand1 || Operand2 true falsetrue falsetrue false

13 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 12 The Boolean Type l Truth table for "!" (NOT): Operand!Operand truefalse true

14 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 13 A Boolean Type Assignments to bool type variables bool P = true; bool Q = false; bool R = true; bool S = P && Q; bool T = !Q || R; bool U = !(R && !Q);

15 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 14 More Operator Precedence l Precedence of operators (from highest to lowest) Parentheses ( … ) Unary operators ! Multiplicative operators * / % Additive operators + - Relational ordering = > Relational equality == != Logical and && Logical or || Assignment =

16 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 15 More Operator Precedence l Examples 5 != 6 || 7 <= 3 (5 !=6) || (7 <= 3) 5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24

17 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 16 Sorting Two Numbers int value1; int value2; int temp; cout << "Enter two integers: "; cin >> value1 >> value2; if(value1 > value2){ temp = value1; value1 = value2; value2 = temp; } cout << "The input in sorted order: " << value1 << " " << value2 << endl;


Download ppt "If Statements & Relational Operators Programming."

Similar presentations


Ads by Google