Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection Structures (if & switch statements) (CS1123)
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Previously Repetition Structures While, Do-While, For.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
Chapter 05 (Part III) Control Statements: Part II.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Original Source : and Problem and Problem Solving.ppt.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Switch Statements Comparing Exact Values
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
 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.
משפטי תנאי ( לוגיקה ) קרן כליף. 2 © Keren Kalif ביחידה זו נלמד :  משפטי תנאי  משפט switch  משפט if מקוצר.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Branching statements.
Introduction to Computer Programming
Chapter 3 Selection Statements
Chapter 3 Control Statements
LESSON 4 Decision Control Structure
Programming Fundamentals
לולאות קרן כליף.
Control Structures – Selection
Chapter 4: Control Structures I (Selection)
Additional Control Structures
Counting Loops.
Chapter 7 Conditional Statements
Chapter 4: Control Structures I (Selection)
Control Structures Part 3
By Hector M Lugo-Cordero September 3, 2008
2.6 The if/else Selection Structure
Decisions, decisions, decisions
Branching statements Kingdom of Saudi Arabia
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Presentation transcript:

Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator: expression1 ? expression2 : expression3 If expression1 is true, the result of the conditional expression is expression2. Otherwise, the result is expression3

Conditional Operator (?:) if(a>=b) Max=a; else Max=b; ******************* Max=(a>=b)?a:b;

switch ( test ) { case 1 : // Process for test = 1 ... break; case 5 : // Process for test = 5 ... default : // Process for all other cases. }

switch works as follows:- The expression, just test in this case, is evaluated. The case labels are checked in turn for the one that matches the value. If none matches, and the optional default label exists, it is selected, the break statement is normally added before the next case label to transfer control out of the switch statement.

#include <iostream> using namespace std; int main() { char grade; cout << "Enter your grade: "; cin >> grade; cout << endl; switch (grade) case 'A': cout << "Your grade is A." << endl; break; case 'B': cout << "Your grade is B." << endl; case 'C': cout << "Your grade is C." << endl; case 'F': cout << "Your grade is F." << endl; default: cout<<" The grade is invalid."<<endl; } return 0;

switch with break #include <iostream> using namespace std; int main() { Int place ; cout << "Enter your place : "; cin >> place; cout << endl; switch (place) case 1: cout << "we're first" << endl; break; case 2: cout << "we're second" << endl; break; default: cout << "we're not first or second" << endl; } return 0;

switch with break break; #include <iostream> using namespace std; int main() { int place ; cout << "Enter your place : "; cin >> place; cout << endl; switch (place) case 1: cout << "we're first" << endl; break; case 2: cout << "we're second" << endl; default: cout << "we're not first or second" << endl; } cout<<endl<<endl; return 0;

switch with break

switch without break #include <iostream> using namespace std; int main() { int place ; cout << "Enter your place : "; cin >> place; cout << endl; switch (place) case 1: cout << "we're first" << endl; case 2: cout << "we're second" << endl; default: cout << "we're not first or second" << endl; } cout<<endl<<endl; return 0;

What is the output #include <iostream> using namespace std; int main() { int place ; cout << "Enter your place : "; cin >> place; cout << endl; switch (place) case 1: cout << "we're first" << endl; case 1: cout << "we're second" << endl; default: cout << "we're not first or second" << endl; } cout<<endl<<endl; return 0;

assert q=n/d; If d is 0,the program would terminate with error message stating that an illegal operation has occurred Solution: assert(d); q=n/d You need preprocessor directive #include<cassert> Assert statement identifies the expression where assertion failed ,the name of file containing the source code ,and the line number where the assertion failed