Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.

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

Fundamental of C programming
Decision Making EE2372 Software Design I Dr. Gerardo Rosiles.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
Control Flow C and Data Structures Baojian Hua
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
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.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Fundamental Programming Fundamental Programming More on Selection.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CIS 375—Web App Dev II JavaScript II.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Decision Making Selection structures (if....else and switch/case)
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
LOGO Conditional Statements & Loops in JavaScript CHAPTER 10 Eastern Mediterranean University School of Computing and Technology Department of Information.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript Javascript is a scripting language produced by Netscape for use within HTML Web pages. JavaScript is loosely based on Java and it is built into.
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Decision Statements, Short- Circuit Evaluation, Errors.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 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.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
JavaScript Tutorial Second lecture 22/2/2016.
DKT121: Fundamental of Computer Programming
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Control Structures.
JavaScript Selection Statement Creating Array
IF if (condition) { Process… }
During the last lecture we had a discussion on Data Types, Variables & Operators
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Conditionals.
PROGRAM FLOWCHART Selection Statements.
CIS 136 Building Mobile Apps
Presentation transcript:

Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make use of conditional statements that allow your program to make correct decisions and perform right actions. JavaScript supports conditional statements which are used to perform different actions based on different conditions.

Forms: Conditional Statements JavaScript supports following forms of conditional statements: if statement if...else statement if...else if... statement. Switch Case

if Statement The if statement is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally. Syntax: if (expression) { Statement(s) to be executed if expression is true }

Syntax: if Statement Here JavaScript expression is evaluated. If the resulting value is true, given statement(s) are executed. If expression is false then no statement would be not executed. Most of the times you will use comparison operators while making decisions.

Example: var age = 20; if( age > 18 ) { document.write(" Qualifies for driving "); }

if...else statement The if...else statement is the next form of control statement that allows JavaScript to execute statements in more controlled way. Syntax: if (expression) { Statement(s) to be executed if expression is true } else{ Statement(s) to be executed if expression is false }

Syntax: if...else statement Here JavaScript expression is evaluated. If the resulting value is true, given statement(s) in the if block, are executed. If expression is false then given statement(s) in the else block, are executed.

Example: var age = 15; if( age > 18 ) { document.write(" Qualifies for driving "); } else { document.write(" Does not qualify for driving "); }

if...else if... statement The if...else if... statement is the one level advance form of control statement that allows JavaScript to make correct decision out of several conditions.

Syntax: if...else if... statement if (expression 1) { Statement(s) to be executed if expression 1 is true } else if (expression 2) { Statement(s) to be executed if expression 2 is true } else if (expression 3){ Statement(s) to be executed if expression 3 is true } else{ Statement(s) to be executed if no expression is true }

Example: var book = "maths"; if( book == "history" ){ document.write(" History Book "); } else if( book == "maths" ){ document.write(" Maths Book "); } else if( book == "economics" ){ document.write(" Economics Book "); } else{ document.write(" Unknown Book "); }

Switch Case You can use multiple if...else if statements, as in the previous slides, to perform a multi-way branch. However, this is not always the best solution, especially when all of the branches depend on the value of a single variable. Starting with JavaScript 1.2, you can use a switch statement which handles exactly this situation, and it does so more efficiently than repeated if...else if statements.

Syntax: Switch Case switch (expression) { case condition 1: statement(s) break; case condition 2: statement(s) break;... case condition n: statement(s) break; default: statement(s) }

Syntax: Switch Case The basic syntax of the switch statement is to give an expression to evaluate and several different statements to execute based on the value of the expression. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, a default condition will be used. The break statements indicate to the interpreter the end of that particular case. If they were omitted, the interpreter would continue executing each statement in each of the following cases.

Example_01: var grade='A'; document.write("Entering switch block "); switch (grade) { case 'A': document.write("Good job "); break; case 'B': document.write("Pretty good "); break; case 'C': document.write("Passed "); break; case 'D': document.write("Not so good "); break; case 'F': document.write("Failed "); break; default: document.write("Unknown grade ") } document.write("Exiting switch block");

Example_02: Consider a case if you do not use break statement: var grade='A'; document.write("Entering switch block "); switch (grade) { case 'A': document.write("Good job "); case 'B': document.write("Pretty good "); case 'C': document.write("Passed "); case 'D': document.write("Not so good "); case 'F': document.write("Failed "); default: document.write("Unknown grade ") } document.write("Exiting switch block");

Example_02 will produce following result: Entering switch block Good job Pretty good Passed Not so good Failed Unknown grade Exiting switch block