{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

03--conditionals-2x2

# 03--conditionals-2x2 - Introduction COMP-202 Unit 3...

This preview shows pages 1–3. Sign up to view the full content.

COMP 202 – Introduction to Computing 1 COMP-202 Unit 3: Conditional Programming CONTENTS : Boolean Expressions The if and if-else Statements COMP 202 – Introduction to Computing 1 2 COMP-202 - Conditional Programming Introduction 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 – Introduction to Computing 1 3 COMP-202 - Conditional Programming Control Flow 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: 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 COMP 202 – Introduction to Computing 1 4 COMP-202 - Conditional Programming Aside: Fundamental Structures (1) 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
COMP 202 – Introduction to Computing 1 5 COMP-202 - Conditional Programming Aside: Fundamental Structures (2) In the sequence structure, statements are executed in the order they appear in the code This is what we have seen so far COMP 202 – Introduction to Computing 1 6 COMP-202 - Conditional Programming Aside: Fundamental Structures (3) In the selection / decision / conditional structure, one of two courses of action is taken depending on whether a condition is true or false condition (rest of the program) true false COMP 202 – Introduction to Computing 1 7 COMP-202 - Conditional Programming Aside: Fundamental Structures (4) In the repetition / iteration / loop structure, a group of statements is executed repeatedly until a condition becomes false condition (rest of the program) true false COMP 202 – Introduction to Computing 1 8 COMP-202 - Conditional Programming Conditional Statements 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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}