M5-CONDITIONALS PDF - If and If-Else Statements
Document Details
Uploaded by Deleted User
Tags
Summary
This document explains if and if-else statements in programming. It describes how control structures are used to define control flow based on conditions and choices.
Full Transcript
if and if-else Statements Control Structures are just a way to define the control flow of programs. Any algorithm or program can be more easily understood if it uses self-contained modules called logic or control structures. It essentially analyzes and selects the direction in which the progr...
if and if-else Statements Control Structures are just a way to define the control flow of programs. Any algorithm or program can be more easily understood if it uses self-contained modules called logic or control structures. It essentially analyzes and selects the direction in which the program flows on the basis of certain parameters or conditions. As in real life circumstances, the time comes when we need to make certain choices, and on the basis of those choices, we decide what we will do next. Different circumstances occur in programming where we need to make certain decisions, and on the basis of those decisions, we must execute the next block of code. In this chapter, we will identify the different fundamental control structures and eventually discuss the different decision control structures which is very vital in computer programming. This chapter discusses the following in general: Fundamental Program Structures If Statement If-Else Statement Learning Outcomes: At the end of the lesson, you are expected to: Identify the different fundamental control program structures Identify the form and structure of If Statement Apply the use of If statement using conditional operators; Identify the form and structure of If-else Statement Apply the use of If-else statement using conditional operators; Create programs using if and if-else statements Start your lesson here. Fundamental Control Structures Programmers often use a flowchart, a tool that helps them plan a program’s logic in diagram form, as a series of shapes connected by arrows. Shown below are the three fundamental control structures in programming depicted using a flowchart. There are three fundamental control structures: Sequence Control Structure: This refers to the line-by-line execution in which the statements are sequentially executed in the same order in which they appear in the script without branching off in another direction. They might, for example, perform a series of read or write operations, arithmetic operations, or variable assignments or in sequential flow as the code is written from top to bottom. Prompt the user to enter first number Prompt the user to enter second number Get the sum of the two numbers Decision Control Structures: A structure that allows various parts of the program to be executed, depending on the exact situation wherein the decisions are typically regulated whether a condition is true or false. The decision control structure may skip the execution of an entire block of statements or even execute one block of statements instead of another. is False True score > 75 Display Display “Pass” “Failed” Loop Control Structure: This is a control structure that allows multiple executions of a block of statements until a specified condition is met. Prompt user to enter a number False is the no equal to 5 True Decision Control Structures These are program structures also known as conditional structures. It simply involves a number of conditions or parameters which decides one out of several written modules/block of codes to be executed while skipping other sections. There are three basic types of decision control structures: ▪ if statement ▪ if–else statement ▪ switch statements if Statement The If statement is one of the powerful conditional statements. It is responsible for changing the program execution flow. The if statement specifies that a statement (of a block of code/statement) will be executed if and only if a certain boolean statement is true or if the condition is met. The condition is evaluated first before any statement or block of statement made within the body of the If statement. The diagram below presents the flowchart of the if statement. If-Statement Flowchart True Boolean_expression False Statement The if-statement has the form/syntax Code Example: if (boolean_expression) int grade = 78; statement; if (grade > 75) //body of if statement cout