Download as pdf
Download as pdf
You are on page 1of 21
xe Clasext Computer Science Conditional Statements Notes Types of statements in Python : Statements are the instructions given to the computer to perform any kindof ction, Python statements can belong o one ofthe following three types. Empty Statement A statement that does nothing In Python an empty statement is pas statement It takes the following form: Pass Whenever Python encounters pass statement, Python does nothing and moves to next, statement inthe flow of conto x BF nW OF CON x + ‘Simple Statement ‘ny executable statement is a simple statement in Python, For example Name=input(“enter your name") ‘Compound Statement ‘compound statement represents a group of statements executed as a unit, The ‘compound statement in Python are written ina specific pattern Iehas > A header line which begins with keyword and ends with colon, ‘A body consisting of one or more Python statements, each indented inside the header line, Flow Control : in a program statements may be executed sequentially selectively or iterative sequence, selection or iteration Every programming language provides constructs to support x BF nW OF CON x + Sequential Statements: + Sequential statements ae set of statements where the execution process will happen in sequence manne. So, these kind of statements are called as seen statements The problem in sequential statements i, ifthe logic has broken in any one of the line, then complete source cade execution wll got broken. So, we are ‘going to decision making iterative and jump statements to avoid this kind of problems, = Decision Making Statements: x BF Decision Making Statements: Decision Making Statements allow the program 10 take the decision as ‘which statement should be executed nex, Decision Making statemenis are used when we want a set of instructions should be executed in one situation and different instructions should be ‘executed in another situation Decision making can be implemented in python using, if statements ielse statemes nested if statements x BF nested if staten ‘The iteration constructs mean repetition of a set of statements depending ‘upon a condition-tst, Till the time @ condition is true, a set of statements are repeated again and again, As soon asthe condition becomes False, the repetition stops. The iteration construct i also called looping construct. x BF The iteration constructs mean repetition of a set of statements depending ‘upon a conditon-est Till the time a condition is true, a set of statements are repeated again and again, As soon asthe condition becomes False, the repetition stops The iteration construct is also called looping construct % BB Rawor con x + oe Program Logic Development Tools: [Before developing the solution of problem in terms of a program, you should read and analyze the given problem and decide about basic sub tasks needed to solvea problem and the order ofthese subtasks. The varios logic development tools are Algorithm and Flowchart Algorithm : An Algorithm isa step by step procedure to solve a given problem, % BB Rawor con x Algorithm : An Algorithm i a step by step procedure to solve a given problem, For example the algorithm to find the remainder of given two numbers is 1. Input frst the number Input second number Divide frst number with second number and store the remainder as third number: 4, Display the result This set of ordered and finite steps to solve a given problem, Flowchart: A flowchart is a graphical representation of an algorithm. A flowchart rent subtasks with different symbols. Some commonly used flowchart shows dif symbols are G Google Account x BF Con onal statement In programming, very often we want to check the conditions and change the behavior ofthe program, ‘We can write programs that has more than one choice of ations depending on avariable's value Perhaps the most well-known statement type i the if statement. You use the if statement to perform one action ifone thing is tre, ‘or any number of other actions, i something ele is true We must use indcotation to define that code that i executed, based on whether ‘condition is met. The if statements of Python : The if statements are the conditional statements in Python and these implement selection constructs An if statement texts a particular condition; ifthe condition evaluates to true, a ‘couse-of action is followed ie. a statement or et-of statements is executed Syntax if x BF The if statements of Python : The i statements are the conditional statements in Python and these implement selection constructs An if statement tests @ particular condition, if the condition evaluates to te, a course-of action is followed ic. a statement or se-of statements is executed Syntax it if grade—"A print(*well done”) Example Example2: if ab prin(“A has more than B has”) Example 3: ifage>= 18 % BB Rawor con x + oe < 6 ctaswor google com/lMskaNOIOTIWN Tn meet ao print "Person eligible for vote’ print "Person not eligible for vote’ prin *eeseeeseeseeseseenenseesee ourPut Person not eligible for vote Person eligible for vote Person not eligible for vote FLOW CHART a a a ee x BF FLOW CHART STATEMENT N| lve statemes An else siatement can be combined with an if statement. % BB Rawor con x + oe else statems An cle statement can be combined with an if statement. An cle statement contains the block of code that executes if the conditional ‘expression in the if statement resolves to 0 ora false value The else statement i an optional statement and there could be at most only one clse statement following i The syntax off. else is % BB Rawor con x + o- 5 * Example ifgrade rin (“well dane”) else print ry again") ELOW CHART x BF iragets print "Person eligible for vote" elifage==18. print "Person jst eligible for vote™ else print "Person not eligible for vote™ Example 2: This srit will compare two strings based on the input from the use x BF Example 2: This srit wll compare two strings based on the input from the use Example 3: 1 Program wo check whether the given number is odd or even, ‘num in(“Enter an integer”) iFeoumaa—=0) print(num, “is even mum") x BF Example 3 Program to check whether the given number is odd or even ‘num in(“Enter an integer”) it(ounti2=0 print{num, “is even number") print(num, “is od number) “The i-eli Statem ‘Sometimes there are more than two possibilities; in tha ease we can use the % BB Rawor con x + o- 5 * The if-lif Statement Sometimes the elif statement © are more than two possiblities; in that ase we can use the Ie stands for “else if" which means that ifthe original if statement is false and the lif statement is tru, execute the block of code following the elif statement The Syntax ofthe if... statement is " * BB Rawor con x + o- 5 *

You might also like