Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Creating PHP Pages Chapter 7 PHP Decisions Making.
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Fundamental Programming Fundamental Programming More on Selection.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
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.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Selection Structures (if & switch statements) (CS1123)
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
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.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Copyright 2003 Scott/Jones Publishing Making Decisions.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
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.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Week 4 Program Control Structure
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow Statements
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
COMP Loop Statements Yi Hong May 21, 2015.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Control Statements: Part1  if, if…else, switch 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Branching statements.
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
Flow of Control.
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
DKT121: Fundamental of Computer Programming
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
JavaScript: Control Statements I
Selection (if-then-else)
Week 3 – Program Control Structure
Using Decision Structures
Branching statements Kingdom of Saudi Arabia
statement. Another decision statement, , creates branches for multi-
Presentation transcript:

Programming Fundamentals

Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators

Decisions In a program a decision causes a onetime jump to a different part of the program, depending on the value of an expression. Decisions can be made in C++ in several ways. The most important is with the if...else statement, which chooses between two alternatives. (e.g., if weather is cold then no cricket else play) This statement can be used without the else, as a simple if statement. (e.g., if hafiz-e-Quran then add 20 marks)

The if Statement The if keyword is followed by a test expression in parentheses.

The if statement The syntax of if is very much like that of while. The difference is that the statements following the if are executed only once if the test expression is true; the statements following while are executed repeatedly until the test expression becomes false.

Example Code

Operation

Example code

The if...else Statement

Example Code

Operation

\01 Nested if else statement

Matching the else

Task Prompt the user to enter a roll number. If he/she enters an odd roll number then print ”Your group is alpha” else print ”Your group is omega”

Solution

Decisions Another decision statement, switch, creates branches for multiple alternative sections of code, depending on the value of a single variable.

The switch Statement If you have a large decision tree, and all the decisions depend on the value of the same variable, you will probably want to consider a switch statement instead of a ladder of if...else or else if constructions.

Syntax

Explanation The keyword switch is followed by a switch variable in parentheses. Braces { } then delimit a number of case statements. Each case keyword is followed by a constant (i.e., integer or character constant), which is not in parentheses but is followed by a colon. The data type of the case constants should match that of the switch variable.

Explanation Before entering the switch, the program should assign a value to the switch variable. This value will usually match a constant in one of the case statements. When this is the case the statements immediately following the keyword case will be executed, until a break is reached.

The break Statement The break keyword causes the entire switch statement to exit. Control goes to the first statement following the end of the switch construction. Don’t forget the break; without it, control passes down (or “falls through”) to the statements for the next case, which is usually not what you want.

Example Break missing

Output

The break Statement If the value of the switch variable doesn’t match any of the case constants, control passes to the end of the switch without doing anything. The break keyword is also used to escape from loops.

switch Versus if...else In an else if construction you can use a series of expressions that involve unrelated variables and are as complex as you like.

switch Versus if...else In a switch statement, however, all the branches are selected by the same variable; the only thing distinguishing one branch from another is the value of this variable. The case constant must be an integer or character constant, like 3 or ‘a’, or an expression that evaluates to a constant, like ‘a’+32.

Conditional Operator

The Conditional Operator This operator consists of two symbols, which operate on three operands. Is same as we write:

The Conditional Operator The part of this statement to the right of the equal sign is called the conditional expression: The question mark and the colon make up the conditional operator. The expression before the question mark is the test expression.

The Conditional Operator If the test expression is true, the entire conditional expression takes on the value of the operand following the question mark: alpha in this example. If the test expression is false, the conditional expression takes on the value of the operand following the colon: beta.

Logical Operators These operators allow you to logically combine Boolean variables (that is, variables of type bool, with true or false values). For example, If today is Sunday and its not raining then I will play cricket. The logical connection here is the word and, which provides a true or false value to the combination of the two phrases. Only if they are both true then I will play cricket.

Logical Operators

Example

If input is ‘n’ then ?? If input is ‘Y’ then ?? If input is ‘N’ then ?? Example

Operator Precedence

Questions!!!