Conditionals.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
True or false A variable of type char can hold the value 301. ( F )
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
The If/Else Statement, Boolean Flags, and Menus Page 180
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Recitation 1 Programming for Engineers in Python.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Introduction to Python
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Lecture2.
Math operations 9/19/16.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
The Ohio State University
CMSC201 Computer Science I for Majors Lecture 03 – Operators
INC 161 , CPE 100 Computer Programming
CSCE 206 Structured Programming in C
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
Chapter 3 Control Statements
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
INC 161 , CPE 100 Computer Programming
Administrative things
Debugging and Random Numbers
Variables and Arithmetic Operators in JavaScript
The Selection Structure
Intro to C Tutorial 4: Arithmetic and Logical expressions
Week 4 – Repetition Structures / Loops
Introduction to C++ October 2, 2017.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Introduction to Programming
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CSCE 206 Lab Structured Programming in C
Coding Concepts (Sub- Programs)
IFS410 Advanced Analysis and Design
Conditions and Boolean Expressions
Module 4 Loops.
Lecture3.
If Statements.
Algorithms computer as the tool process – algorithm
An Introduction to Linux
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Loops.
Let’s start from the beginning
Introduction to Computing Lecture 04: Booleans & Selection
EECE.2160 ECE Application Programming
CHAPTER 5: Control Flow Tools (if statement)
Arrays.
EECE.2160 ECE Application Programming
More Loops Topics Relational Operators Logical Operators for Loops.
An Overview of C.
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
CSCE 206 Lab Structured Programming in C
Introduction to Python
Presentation transcript:

Conditionals

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

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

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

Conditionals in UML 2018

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

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

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

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

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