Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditionals.

Similar presentations


Presentation on theme: "Conditionals."— Presentation transcript:

1 Conditionals

2 Odd or even? Solution? Modulo division! (checking for the remainder)
Verify the parity of a given number If the number is odd, print „ODD“ on the screen. If the number is even, print „EVEN“ on the screen. Solution? Modulo division! (checking for the remainder) 2018

3 Examples of conditions
Check if both of the values are equal (NB! 2 equal signs! – not to be mixed with assignments) if (a == b) Check whether the values are not equal if (a != b) Check if a is smaller than b (strict) if (a < b) Check if a is smaller or equal to b if (a <= b) Check the remainder of a modulo division – if it equals to zero if (a % 2 == 0) 2018

4 Conditional statements (if-else)
// Beginning a conditional statement if (condition) { printf("Odd \n "); // this will only be run when condition is true } else printf("Even\n"); // will only be run if the condition is false NB! Conditions can only evaluate to truth value (boolean) – true or false 2018

5 Conditionals in UML 2018

6 Conditionals (if-else if-else)
if (condition) { // executed if the condition is true } else if (condition) // executed when the first condition wasn’t true // but the second one is else // executed when none of the previous conditions were true 2018

7 Let’s start to code (parity.c)
#include <stdio.h> // standard input/output library (preprocessor directive) int main(void) // start of the main function/program { int number; // declaring an integer printf("Enter an integer to check\n"); scanf("%d", k); // TYPE YOUR CODE HERE return 0; // program finished successfully, code 0 is returned to indicate this } 2018

8 Advanced task (slightly more complex)
Ask the user for an integer Check if The number is divisible by 3 The number is divisible by 5 The number is divisible by both 3 and 5 In addition, separately output the multiples and remainder from the division with 3 and 5 E.g. Dividing 27 by 5 leaves us with 5 multiples of 3 and a remainder of 2. Dividing 27 by 3 leaves us with 9 multiples of 3 and a remainder of 0. Hint: logical expressions 2018

9 Parallel activities in UML
Only the things that are not dependent on each other can be done in parallel 2018

10 Homework for the next week
The homework will always be visible on the next weeks lab on the web! Convert a given algorithm into valid UML format. (link is on the web) Compose an algorithm of a day in Your life. It could be one of the schooldays in TUT. It must contain conditions and parallel activities! We will review them next week 2018


Download ppt "Conditionals."

Similar presentations


Ads by Google