Vous êtes sur la page 1sur 54

Conditional structures A statement or set of statements that is executed when a particular condition is true and ignored when a condition

is false. Sequential statements are executed in the same order in which they are written Control is automatically transferred to the next instruction one by one All statements are executed only once C++ provides branching or conditional structures for making decision These structures check the condition before executing the statement or set of statement

Engr: Sajida Introduction to computing

Relational operators The relational operators are used to specify condition in programs A relation operator compares two values It produces result as true or false The relational operator is called conditional operator or comparison operator They test the conditions that are either true or false
Engr: Sajida Introduction to computing

> Greater than operator < Less than operator == equal to operator >= greater than or equal to operator <= less than or equal to operator != the not equal to operator

Engr: Sajida Introduction to computing

Relational expression An expression that uses relational operators to compare two values The result of relational operator is true or false Both sides of relational operator can be a variable or constant or arithmetic expression 40>=20 true

Engr: Sajida Introduction to computing

If structure If is a key word in c++ language If statement is a decision making statement It is used to execute or skip the statement or set of statement by checking the condition The condition is given in the relational expression If condition is true the statement or set of statement is executed after the if statement If condition is false than statement or set of statement after the if statement is not executed

Engr: Sajida Introduction to computing

Syntax The syntax of if statement is If (condition Statement; The above syntax is used for single statement A set of statements can also be made conditional The set of statement is written in the curly braces The set of statement is called compound statements Syntax If (condition) { statement 1; statement 2; . . Statement N; } Engr: Sajida
Introduction to computing

Condition

Statement (s)

If else structure

Engr: Sajida Introduction to computing

If else structure If else statement is another type of if statement It executes one block of statements if given condition is true and the other when it is false In if else statement Both blocks of statements can never be executed Both blocks of statements can never be skipped Syntax If (condition) statement; else statement;
Engr: Sajida Introduction to computing

The syntax for compound statement in if else statement is If (condition) { statement 1 ; statement 2 ; : statement N ; } Else { Statement1; Statement 2 ; : Statement N; }
Engr: Sajida Introduction to computing

Condition F Statement (s)

Statement (s)

If else structure

Engr: Sajida Introduction to computing

Multiple if- else- if structure if else- if statement can be used to choose one block of statement from many blocks of statements It is used when there are many options and only one block of statements should be executed on the basis of condition

Engr: Sajida Introduction to computing

Syntax If (condition) { block 1; } else if (condition) { block 2; } :


else block N; }
Engr: Sajida Introduction to computing

Condition

Statement block 1

Condition

Statement block 2

T
Condition F

Statement block N

Statement (s)

Multiple If else if structure


Engr: Sajida Introduction to computing

Working of if- else- if The test condition in if else statement with multiple alternatives are executed in a sequence until a true condition is reached If a condition is true the block of statement following that condition is executed The remaining blocks are skipped If condition is false the block of statements followed that condition is skipped The statement after the last else are executed if all the conditions are false

Engr: Sajida Introduction to computing

Nested if structure An if statement within an if statement is called nested if statement In nested structure the control enters to the inner if only when the outer condition is true Only one block of statement is executed and the remaining blocks are skipped automatically Many if statements inside another if statement can be used

Engr: Sajida Introduction to computing

Outer if

If (condition)
If (condition) { Statement (s); } else { Statement (s); } else { Statement (s); }
Engr: Sajida Introduction to computing

Inner if

Working of Nested If In nested if statement the condition of outer if statement is tested first ,if it is true then the statement of inner if block is executed If the condition is false the inner if is skipped and control moves to the else part of the outer if If outer if is true then control moves to the inner if statement. the inner if statement is evaluated according to the simple if statement

Engr: Sajida Introduction to computing

Inner if True condition

condition F

Statement (s)

Statement (s)

Statement (s)

Engr: Sajida Nested if structure

Introduction to computing

Compound condition A type of comparison in which more than one conditions are evaluated It is used to execute a statement or set of statement by testing more than one condition Compound statement is executed by using logical operators

Engr: Sajida Introduction to computing

There are three logical operator in c++ AND operator (&&) OR operator (||) NOT operator (!) AND operator(&&) It is used to evaluate the two conditions It produces true result if both conditions are true It produces false result if any of the condition is false

Engr: Sajida Introduction to computing

Condition 1 False False

Operator && &&

Condition 2 False False

Result False False

True
True

&&
&&

False
True

False
True

Engr: Sajida Introduction to computing

OR operator(||) It is used tom evaluate two conditions If any one condition is true than it produces the true result It produce the false result if both conditions are false

Engr: Sajida Introduction to computing

NOT operator (!) the symbol used for not operator is (!) It is used to reverse the result of a conditions it produces true result if the condition is false It produces the false result if the condition is true

Engr: Sajida Introduction to computing

Switch Structure The switch statement is another conditional structure It is good alternative of nested if- else. It can be used easily when there are many choices available and only one should be executed

Engr: Sajida Introduction to computing

Switch (expression) { case constant 1; statement (s); break; case constant 2; statement (s); break; : case constant N; statement (s); break; default: statement (s); }

Switch statement Engr: Sajida


Introduction to computing

Switch statement compares the result of single expression with multiple cases expression can be any valid expression that results in integer or character value The expression is evaluated at the top and result is compared with different cases Each case represents one choice if result matches with any case the corresponding block is executed The break statement in each case is used to exit to switch body It is used to at the end of each case block When the result of expression matches with the case block the corresponding statements are executed
Engr: Sajida Introduction to computing

The break statement comes after these statements and the control exits from the switch body If break is not used than all case blocks that comes after the matching case will also be executed The default label is used at the end of all the case blocks It is executed only when the result of expression does not match with any case label Its use is optional

Engr: Sajida Introduction to computing

Difference between nested if else and switch These statements are used when there are many choices and only one should be executed But there are some difference in these statement

Engr: Sajida Introduction to computing

Switch
It is easy to use when there are multiple choices It uses single expression for multiple choices

Nested if
It is difficult to use when there are multiple choices It uses multiple expression for multiple choices

It can not check range of values


It checks only constant values .variables can not use with case statement

It can check range of values


It checks variable also. Constant and variable values are used in relational expression

Engr: Sajida Introduction to computing

Go to statement Is used to move the control directly to the particular location of the program by using label A label is the name given to the particular line of the program A label is created with a valid identifier followed by a colon(:) Syntax goto label ;
Engr: Sajida Introduction to computing

Loops A type of control structure that repeats a statement or set of statement is known as looping structure It is also known as repetitive or iterative structure in sequential structure all statements are executed once ,conditional structural may execute or skip a statement on the basis of some given condition Looping structures are used to repeat a statement or set of statements for a specified no of times

Engr: Sajida Introduction to computing

Loops are basically used for two purposes 1- To execute a statement or number of statements for a specified number of time User wants to display his name for 10 times 2- to use a sequence of values User may display a se of natural numbers from 1 to 10

Engr: Sajida Introduction to computing

Three types of loops available in c++ While loop Do-while loop For loop

Engr: Sajida Introduction to computing

Counter controlled loops This type of loop depends on the value of a variable known as counter variable The value of counter variable is incremented or decremented each time as the body of the loop is executed This loop terminates as the counter variable reaches at the particular value

Engr: Sajida Introduction to computing

Sentinel loop A sentinel-controlled loop is a loop that terminates when some thing happen inside the loop body indicating that the loop should be executed This type of loop depends upon the special value known as sentinel value These types of loops are also known as conditional loop A loop may execute while the value of a variable is not -1. -1 is the sentinel value that is used to terminate loop

Engr: Sajida Introduction to computing

While loop While loop is the simplest loop in the c++ This loop executes one or more statements while the given condition remains true It is useful when the number of iteration is not known in advance Syntax While (condition) statement;

Engr: Sajida Introduction to computing

Condition is given as a relational expression This statement executed only if the condition is true if the condition is not true this statement is never executed Statement is the instruction that is executed when the condition is true if two or more statements are used these are given in the braces { } it is called body of the loop the syntax for compound statement is while (condition ) { Statement 1; Statement 2; : : Statement N; }
Engr: Sajida Introduction to computing

condition

Loop body

Flow chart of while loop

Engr: Sajida Introduction to computing

First of all condition is evaluated if it is true control enters the body of the loop and executes all the statement in the body after executing the statements it again move to the start of the loop and evaluates the condition again. This process continues as long as the condition remains true when the condition becomes false the loop is terminated While loop terminates only when the condition becomes false if the condition remains true ,the loop never terminates

Engr: Sajida Introduction to computing

Do while loop Do-while loop is an iterative control in c++ This loop executes one or more statements while the given condition is true In this loop the condition comes after the body of the loop It is important loop in which body of the loop is executed at least once

Engr: Sajida Introduction to computing

Syntax Do { statement 1; statement 2; : : statement N; } while (condition);

Engr: Sajida Introduction to computing

Do is the key word in the c++ and indicates the beginning of the loop Condition is given in the relational expression the statement is executed only if the given condition is true, if the condition is false the statement is never executed Statement is the instruction that is executed when the condition is true If two or more statements are used ,these are written in braces{ } It is called body of the loop

Engr: Sajida Introduction to computing

condition

Loop body

Flow chart of do while loop

Engr: Sajida Introduction to computing

Difference between While loop while and do while loop Condition comes before the body of the loop
If condition is false in the beginning ,while loop is never executed

Do-while loop Condition comes after the body of the loop If condition is false in the beginning , do while loop is executed at least once

Engr: Sajida Introduction to computing

Pretest and post test in loops A pretest is a condition that is used to test for the completion of loop at the top of the loop In this type of loop the statements inside the body of the loop can never execute if the terminating condition is false the first time it is tested

Engr: Sajida Introduction to computing

For loop For loop executes one or more than one statement for a specified number of times This loop is called counter controlled loop It is flexible loop Syntax for(initialization;condition;increment/decrement) { Statement1; Statement2; : : Statement N; }
Engr: Sajida Introduction to computing

Initialization specifies the starting value of counter variable one or many variable are initialized in this part. To initializes more than one variable ,each variable is separated by comma Condition is given as the relational expression The statement is executed only if the condition is true ,if the condition is false the statement is never executed

Engr: Sajida Introduction to computing

Increment/decrement part specifies the change in counter variable after each execution in the loop Statement is the instruction that is executed when the condition is true if two or more statements are used these are given in the braces .it is called body of the loop for (x=1,y=1;x<=10;x++,y++)

Engr: Sajida Introduction to computing

The number of iterations depends upon the initialization condition and increment /decrement parts the initialization part is executed only once when the control enters the loop. After initialization the given condition is evaluated .if it is true control enters the body of the loop and execute all the statement in it Then the increment /decrement part is evaluated it changes the value of counter variable The control moves to the condition part this process continues while condition remains true ,the loop is terminated when the condition becomes false

Engr: Sajida Introduction to computing

initialization

Increment/decrement

condition

Loop body

F
Flow chart of for next exit
Engr: Sajida Introduction to computing

loop

Nested loops A loop within a loop is called nested loop In nested loop, the inner loop is executed completely with each change in the value of counter variable of outer loop The nesting can be done up to any level The increase in the level of nesting increases the complexity of nested loop Any loop can be used as the inner loop of another loop While loop can be used as the outer loop and for loop can be used as inner loop in nested loop
Engr: Sajida Introduction to computing

Syntax for (initialization ;condition; increment/decrement) { for (initialization ;condition; increment/decrement) { statement (s); } }

Engr: Sajida Introduction to computing

Int i ,j; for (i=1;i<=2;i++) for( j=1;j<=3;j++) cout<<outer :<<i<<:inner<<j<<endl; The above example uses nested for loops the outer loop executes two times and the inner loop executes three times with each iteration of the outer loop. it means that the inner loop executes six times in total when the value of i is one in first iteration of outer loop ,the value of j changes from 1 to 3 in three iterations of inner loop .similarly when the value of i is 2 in the second iteration of outer loop the value of j changes from 1 to 3 in three iterations of inner loop
Engr: Sajida Introduction to computing

Engr: Sajida Introduction to computing

Vous aimerez peut-être aussi