Essay Conditional Programming

Submitted By vasseurtim
Words: 3198
Pages: 13

Introduction
COMP 202 – Introduction to Computing 1

COMP 202 – Introduction to Computing 1

COMP-202
Unit 3: Conditional
Programming
CONTENTS:
Boolean Expressions
The if and if-else Statements

• Suppose we want to write a program which asks the user to enter two numbers and then displays only the larger of the two
• This will involve executing certain statements in some circumstances, and different statements in other circumstances
• Problem: So far, all the programs we have written executed all the statements they contained all the time
– We do not yet know the tools to make decisions about which statements should be executed

COMP-202 - Conditional Programming

Aside: Fundamental Structures (1)

Control Flow

– decide whether or not to execute some statements, or
– perform some statements over and over repetitively

• The order of statement execution is called control flow or flow of control

3

COMP 202 – Introduction to Computing 1

COMP 202 – Introduction to Computing 1

• The default order of statement execution through a method is linear: one statement after the other, in the order they are written (from the top of the page down towards the bottom)
• Some programming statements modify that order, allowing us to: COMP-202 - Conditional Programming

2

• A control flow structure is a basic unit of programming logic
• Any program can be constructed using only three structures:
– Sequence
– Selection / decision / conditional
– Repetition / iteration / loop

• The most common programming languages support these three structures

COMP-202 - Conditional Programming

4

Aside: Fundamental Structures (3)

• In the sequence structure, statements are executed in the order they appear in the code

• In the selection / decision / conditional structure, one of two courses of action is taken depending on whether a condition is true or false

• This is what we have seen so far

COMP 202 – Introduction to Computing 1

COMP 202 – Introduction to Computing 1

Aside: Fundamental Structures (2)

false

condition

true

(rest of the program)
COMP-202 - Conditional Programming

COMP-202 - Conditional Programming

5

Aside: Fundamental Structures (4)

false

condition

true

(rest of the program)
COMP-202 - Conditional Programming

Conditional Statements
COMP 202 – Introduction to Computing 1

COMP 202 – Introduction to Computing 1

• In the repetition / iteration / loop structure, a group of statements is executed repeatedly until a condition becomes false 7

6

• Sometimes, one wants a statement to be executed only if some condition is satisfied
– If this condition is not satisfied, either this statement should simply be skipped, or some other statement should be executed instead

• A conditional statement lets us choose which statement will be executed next
• Therefore, they are sometimes called selection statements
• Conditional statements give us the power to make basic decisions • Java's main conditional statements are the if statement and the if-else statement
COMP-202 - Conditional Programming

8

Boolean Expressions
COMP 202 – Introduction to Computing 1

COMP 202 – Introduction to Computing 1

Part 1: Boolean Expressions

• Like an arithmetic expression, a boolean expression is a combination of operators and operands, and it evaluates to a value • However, the type of the value a boolean expression evaluates to is not numeric, but boolean (true or false)
• A boolean expression can be:






The comparison of two values using a comparison operator
A variable which has type boolean
A literal which has type boolean (true or false)
The negation of another boolean expression using the ! operator
The combination of two or more other boolean expressions using the
&& or || operators

COMP-202 - Conditional Programming

Comparison Operators (1)

Comparison Operators (2)