1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if...

9
1.5 Conditional Statements academy.zariba.com 1

Transcript of 1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if...

Page 1: 1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if statements 3.Switch-case statements 2.

1

1.5 Conditional Statements

academy.zariba.com

Page 2: 1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if statements 3.Switch-case statements 2.

2

Lecture Content

1. If-else statements2. If-else-if statements3. Switch-case statements

Page 3: 1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if statements 3.Switch-case statements 2.

3

Comparison Operators

Operator Notation

Equals ==

Not Equals !=

Greater Than >

Greater Than or Equals >=

Less Than <

Less Than or Equals <=

Page 4: 1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if statements 3.Switch-case statements 2.

4

Logical Operators

Operator Notation

Not !

And &&

Or ||

XOr ^

Page 5: 1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if statements 3.Switch-case statements 2.

5

1. If-else and if-else-if Statments

Page 6: 1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if statements 3.Switch-case statements 2.

6

2. Switch Case statements

Page 7: 1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if statements 3.Switch-case statements 2.

7

Homework

1. Write an if statement that exchanges the values of 2 numbers if the first is bigger than the second

2. Write a program that finds the largest of 3 integers, using if statements.

3. Sort 3 integer numbers using if statements.4. Write a program that enters a number from 10 to 19 and prints

on the console the name of the number. E.g. 11 – “eleven”. Use switch.

5. Write a boolean expression for finding if the bit 2 (counting from 0) of a given integer is 1 or 0.

6. Write an expression that checks if a given point (x,y) is within a circle with radius 4 and centre at (0,0)

7. *We are given 5 integer numbers. Write a program that checks if there is a subset of these 5 numbers which sums to zero. List all such sums. E.g. 5,0,-2,-3,1 should print 1.) 0 =0 2.) 5-2-3=0.

Page 8: 1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if statements 3.Switch-case statements 2.

8

References

Page 9: 1.5 Conditional Statements academy.zariba.com 1. Lecture Content 1.If-else statements 2.If-else-if statements 3.Switch-case statements 2.

9

Zariba Academy

Questions