More on conditional statements. Conditionals In some situations the typical if-else statements may become cumbersome Depending on the situation, there.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Flow Charting. Goals Create Algorithms using Flow Charting procedures. Distinguish between Flow Charting and Pseudocode. Top-Down Design Bottom-up Design.
COMP Flow of Control: Branching 2 Yi Hong May 19, 2015.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Fundamental Programming Fundamental Programming More on Selection.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions Continued.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Chapter 4 Selection Structures: Making Decisions.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Flow of Control Part 1: Selection
How to start Visual Studio 2008 or 2010 (command-line program)
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
Previously Repetition Structures While, Do-While, For.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
Conditionals CS 103 February 16, Blast from the Past: C14 Dating Problem Statement: Calculate the age of a fossil from its C-14 radioactivity Problem.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Instructor - C. BoyleFall Semester
Gator Engineering 1 Chapter 2 C Fundamentals (Continued) Copyright © 2008 W. W. Norton & Company. All rights reserved.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
CS 240 Computer Programming 1
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Lecture2.
Algorithm: procedure in terms of
Chapter 7: User-Defined Functions II
Compound Condition Break , Continue Switch Statements in Java
Decisions Chapter 4.
The if Statement Format or No Condition satisfied? Yes
Chapter 4: Making Decisions.
SELECTION STATEMENTS (1)
Control Statement Examples
CSCE 206 Lab Structured Programming in C
The switch statement: an alternative to writing a lot of conditionals
Selection Control Structure: Switch Case Statement
Variables & Data types Selection Statements Additional Operators
Conditionals.
More on conditional statements
Fundamental Programming
Clear and Unclear Windows
Arrays.
EECE.2160 ECE Application Programming
CSCE 206 Lab Structured Programming in C
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Module 3 Selection Structures 12/7/2019 CSE 1321 Module 3.
Presentation transcript:

More on conditional statements

Conditionals In some situations the typical if-else statements may become cumbersome Depending on the situation, there are 2 possible constructs that may become useful switch () If / else if / else 20152

Conditionals in UML 20153

The code switch () switch (expression) { case constant or expression: statements break; case constant or expression: statements break; default: statements break; } if / else if (condition) { statements } else if (condition) { statements } else { statements } 20154

Create an algorithm for a program… … that mimics a typical food scale in the supermarket Get the weight of the product (from user input) Get the type of the product (e.g. banana, cucumber, …) (from user input) Price will be calculated based on the weight and the product type The cost will be outputted along with the product type An error is printed when an unknown product number is chosen. An error is printed when the cost will calculate to

#include int main(void) { float weight; int product; printf(„Insert the weight of the product\n> „); scanf("%f", weight); switch (product) { case 0: printf(„Product %d – Banana\n“, valik); hind = 1.28; break; default: hind = 0; break; } cost = price * weight; printf(„Cost: %f“, cost); return 0; } Create a product that would emulate a food scale in a supermarket 5 products (4 of them create by yourself) Show the list of available products to the user User must be able to input the product weight and type Verify that all of the variables needed are declared Create an exception when cost will be 0