Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.

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

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
true (any other value but zero) false (zero) expression Statement 2
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
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.
C++ for Engineers and Scientists Third Edition
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Chapter 4 Program Control Statements
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
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.
Flow of Control Part 1: Selection
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow 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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Java Programming Fifth Edition
Decision Making in C.
Chapter 4: Making Decisions.
Condition Statements.
Programming Fundamentals
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Control Structures.
IF if (condition) { Process… }
Control Structures: Selection Statement
Control Structures Part 3
Conditionals.
Control Structures: Selection Statement
Presentation transcript:

Conditional Statement Chapter 8

Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement depending on the result of the condition.

Types of Conditional Statement The If Statement The If-Else Statement The Nested-If Statement The If-Else-If Ladder The Switch Statement The Nested Switch Statement

The If Statement The general form of the If statement is: if ( expression) statement; Expression is relational or Boolean expression that evaluates to a TRUE (1) or False (0) value. Statement may either be a single C statement or a block of C statements.

The If Statement The general form of the If statement with block statement is: if ( expression) { statement_sequence; } In an if statement, if the expression evaluates to TRUE (1), the statement or the block of statements that forms the target of the if statement will be executed. Otherwise, the program will ignore the statement or the block of statements.

The If-Else statement The general form of the if-else statement is: If (expression) statement_1; else statement_2; Where: – If and else are reserved words – Expression is relational or Boolean expression that evaluates to a TRUE (1) or False (0) value. – Statement_1 and statement_2 may either be a single C statement or a block of c statements.

The If-Else statement The general form of the if-else statement with block of statement is: If (expression) {statement_sequence; } else { statement_sequence; }

The If-Else statement If an if-else statement, if the expression is TRUE (1), the statement or block of statement after the if statement will be executed; otherwise, the statement or block of statement in the else statement will be executed. Note: Only the code associated with the if or the code that is associated with the else executes, never both.

Nested-If statement One of the most confusing aspects of the if statement in any programming language is nested ifs. A nested if is an if statement that is the object of either an if or else. This is sometimes referred to as “an if within an if.” The reason that nested ifs are so confusing is that it can be difficult to know what else associates with what if.

Nested-If statement Fortunately, C provides a very simple rule for resolving this type of situation. In C, the else is linked to the closest preceding if that does not already have an else statement associated with it.

Nested-If statement Consider the following situations: – Situations 1. The else at number 3 is paired with the if in number 2 since it is the nearest if statement with the else statement. 1. if….. 2. if …… … 3. else

Nested-If statement Consider the following situations: – Situations 2. The else in number 5 is paired with the if in number if …. 2. { 3. if …. 4. } 5. else

Nested-If statement Note that there is a pair of braces found in number 2 and number 4. The pair of braces defined the scope of the if statement in number 1 starting from the { in number 2 and ends with } in number 4. Therefore, the else statement in number 5 cannot paired with the if statement in number 3 because the else statement is outside the scope of the first if statement. This makes the if statement in number 1 the nearest if statement to the else statement in number 5.

The if-else-if Ladder A common programming construct in C is the if-else- if ladder. The general form of the if-else-if ladder statement is: if ( expression_1) statement_1; else if (expression_2) statement_2; else if (expression_3; statement_3; : else statement_else;

The if-else-if Ladder Where: – If and else are reserve words in C – Expression_1, expression_2 up to expression_n in relational or boolean expression that evaluates to a TRUE (1) or False (0) value. – Statement_1, statement_2 up to statement_else may either be a single C statement or a block of C statement. In an if-else-if ladder statement, the expression are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder will not be executed. If none of the condition is true, the final else is executed.

The if-else-if Ladder The final else acts as a defaults condition. If all other conditions are false, the last else statement is performed. If the final else is not present, then no action takes place. Note: The final else is optional, you may include this part if needed in the program or you may not include if not needed.

The switch statement The switch statement is a multiple-branch decision statement. The general form of the switch statement is: switch (variable) { case constant1: statement sequence_1; break; case constant2: statement_sequence_2; break; : default: statement_sequence_default; }

The switch statement In a switch statement, a variable is successively tested against a list or integer or character constants. If a match is found, a statement or block of statement is executed. The default part of the switch is executed if no matches are found.

The switch statement According to Herbert Schildt (1992), there are three important things to know about switch statements: – 1. The switch differs from if statements in such a way that switch can only test fro equality whereas if can evaluate a relational or logical expression. – 2. No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch may have case constant that are the same.

The switch statement – 3. If character constants are used in the switch, they are automatically converted to their integer values. Note: The break statement is used to terminate the statement associated with each case constant. It is a C keyword which means that at that point of execution, you should jump to the end of the switch statement by the symbol }.

The Nested Switch Statement The general form of the nested switch statement is:

The Nested Switch Statement switch (variable) { case constant:{ switch (variable) { case constant1: statement sequence_1; break; case constant2: statement_sequence_2; break; } break; } case constant2: statement sequence; break; default: statement sequence; }