LESSON 4 Decision Control Structure

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
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.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
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:
Computer Science Department Relational Operators And Decisions.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
Previously Repetition Structures While, Do-While, For.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
1 CS161 Introduction to Computer Science Topic #8.
Statements
Control statements Mostafa Abdallah
1 CS 101 Fall 2001 Lecture 3. 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y,
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
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.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
LESSON 2 Basic of C++.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Switch Statements Comparing Exact Values
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Branching statements.
Chapter 3 Selection Statements
Chapter 3 Control Statements
LESSON 2 Basic of C++.
Decisions Chapter 4.
Chapter 4: Making Decisions.
Introduction to programming in java
Decisions Given hours worked and pay rate, calculate total pay
Programming Fundamentals
Programming Fundamentals
Chapter 4: Making Decisions.
Intro to Programming Week # 4 Switch Statement Lecture # 7
Conditionals & Boolean Expressions
Decisions Given hours worked and pay rate, calculate total pay
Compound Assignment Operators in C++
Enumerations CS Fall 1999 Enumerations.
Control Structures: Selection Statement
Chapter#3 Structured Program Development in C++
Flow Control Statements
If Statements.
Control Structures Part 3
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
The Java switch Statement
Structured Program Development in C++
Third Grade Reading Block Lesson Plans
Control Structures: Selection Statement
Branching statements Kingdom of Saudi Arabia
Comparing Data & the ‘switch’ Statement
Control Structures contd… Selection
Selection Control Structure
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

LESSON 4 Decision Control Structure

Decision Control Structures if statement if-else statement nested if-else statement switch-case statement

if statement if (condition) { statement1; statement2; }

if Statement Sample #include <iostream> #include <conio.h> using namespace std; int main() { int grade = 77; if (grade >= 67) cout<<“You passed C++”; } getch(); return 0; You passed C++

if Statement Sample #include <iostream> #include <conio.h> using namespace std; int main() { int grade = 50; if (grade >= 67) cout<<“You passed C++”; } getch(); return 0;

If-else statement if (condition) { statementA1; statementA2; } else statementB1; statementB2;

If-else Statement Sample #include <iostream> #include <conio.h> using namespace std; int main( ) { int x= 6; int y= 2; if (x > y) cout<<“x is greater than y \n”; else cout<<“y is greater than x \n”; getch(); return 0; } x is greater than y

If-else Statement Sample #include <iostream> #include <conio.h> using namespace std; int main( ) { int x= 4; int y= 8; if (x > y) cout<<“x is greater than y \n”; else cout<<“y is greater than x \n”; getch(); return 0; } y is greater than x

Nested if Statement if (condition1) { statementA1; statementA2; } else if (condition2) statementB1; statementB2;

Nested if Statement Sample #include <iostream> #include <conio.h> using namespace std; int main( ) { int x= 6; int y= 2; if (x > y) cout<<“x is greater than y \n”; else if (y<x) cout<<“y is greater than x \n”; else cout<<“x and y are equal \n”; getch(); return 0; } x is greater than y

Nested if Statement Sample #include <iostream> #include <conio.h> using namespace std; int main( ) { int x= 2; int y= 6; if (x > y) cout<<“x is greater than y \n”; else if (x<y) cout<<“y is greater than x \n”; else cout<<“x and y are equal \n”; getch(); return 0; } y is greater than x

Nested if Statement Sample #include <iostream> #include <conio.h> using namespace std; int main( ) { int x= 6; int y= 6; if (x > y) cout<<“x is greater than y \n”; else if (y<x) cout<<“y is greater than x \n”; else cout<<“x and y are equal \n”; getch(); return 0; } x and y are equal

The switch-case Statement Clean way to implement multi-way selection The switch statement evaluates an expression, then attempts to match the result to one of several possible cases The match must be an EXACT match.

The switch - case syntax The general syntax of a switch statement is: switch (expression) { case constant1 : statement-list1 break; case constant2 : statement-list2 break …. default: statement-list3 }

switch Example int day =3; switch ( day ) { case 1: cout <<“Monday” ; break ; case 2: cout << “Tuesday”) ; case 3: cout << “Wednesday” ; case 4: cout << “Thursday” ; case 5: cout << “Friday” ; default: cout << “Invalid day.” ; }

switch Example int day =5; switch ( day ) { case 1: cout <<“Monday” ; break ; case 2: cout << “Tuesday”) ; case 3: cout << “Wednesday” ; case 4: cout << “Thursday” ; case 5: cout << “Friday” ; default: cout << “Invalid day.” ; }

switch Example int day =6; switch ( day ) { case 1: cout <<“Monday” ; break ; case 2: cout << “Tuesday”) ; case 3: cout << “Wednesday” ; case 4: cout << “Thursday” ; case 5: cout << “Friday” ; default: cout << “Invalid day.” ; }

To Switch or not to Switch The expression of a switch statement must result in an integral type, meaning an integer (whole number) or a char ONLY It cannot be a floating point value (float or double) You cannot perform relational checks with a switch statement

To Switch or not to Switch The expression of a switch statement must result in an integral type, meaning an integer (whole number) or a char ONLY char letter =‘b’; switch ( letter ) { case ‘a’: cout<<“A”; break ; case ‘b’: cout<<“B”; default: cout<< “Invalid Input.”; }

To Switch or not to Switch It cannot be a floating point value (float or double) float x =3.14; switch ( x ) { case 3.14: cout<<“A” ; break ; case 3.16: cout<<“B” ; default: cout<<“Invalid Input.”; }

To Switch or not to Switch You cannot perform relational checks with a switch statement int x =123; switch ( x ) { case x>5: cout <<“A” ; break ; case x>4: cout<<“B” ; default: cout<< “Invalid Input.”; }