0% found this document useful (0 votes)
3 views

CH-3

Chapter 3 discusses control statements in programming, focusing on flow control, Boolean expressions, decision control structures (like if-else and switch statements), and iteration (loops such as for, while, and do-while). It explains the syntax and functionality of these control structures, including the use of break and continue statements. Additionally, the chapter introduces functions, their components, and the advantages of using functions to manage larger programs.

Uploaded by

Solomon Asfaw
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CH-3

Chapter 3 discusses control statements in programming, focusing on flow control, Boolean expressions, decision control structures (like if-else and switch statements), and iteration (loops such as for, while, and do-while). It explains the syntax and functionality of these control structures, including the use of break and continue statements. Additionally, the chapter introduces functions, their components, and the advantages of using functions to manage larger programs.

Uploaded by

Solomon Asfaw
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

CHAPTER -3 :- CONTOROL STATEMENT

 The order in which the statements in your program are performed is called flow of control.
 In this chapter we learn about:-
 Boolean expression
 Boolean logic.
 And (&&):-returns true if all arguments are true

Or (||):-returns false if all arguments are false
 !(negation):-returns true if results are false(inverse)
 Relation expression.
 >(greater than)
 <(lessthan)

 ==(equal)

 >=(greaterthan or equal)

 <=(lessthan or equal)

 Decision Control Structure/selection


 If statement

 Else if statement

 Nested if statement

 Switch

 Iteration (loop)
 For condition

 While condition

 Do while condition

 continue and break


BOOLEAN EXPRESSION
 A Boolean expression is an expression that can be thought of as
being true or false (that is, true if satisfied or false if not
satisfied).
 Thus far you have used Boolean expressions as the test condition
in if-else statements and as the controlling expression in loops,
such as a while loop.
 However, a Boolean expression has an independent identity apart
from any if-else statement or loop statement you might use it in.
 The C++ type bool provides you the ability to declare variables
that can carry the values true and false.
 A Boolean expression can be evaluated in the same way that an
arithmetic expression is evaluated.
 The only difference is that an arithmetic expression uses
operations such as +, *, and / and produces a number as the final
result, whereas a Boolean expression uses relational
operations such as == and < and Boolean operations such as
&&, ||, and ! to produce one of the two values true and false as
the final result.
IF STATEMENT
The syntax of if statement is as follows:

 if is a keyword. It tells the compiler that what follows is a decision


control structure.
 An if statement is always followed by an expression or condition
which is enclosed within a pair of parenthesis.
 The expression is evaluated and will be either true or false.
 If true then the statement following the if statement is executed, if
false then this statement is skipped and the execution continues
from next statement.
 Every non zero value will be considered true be it positive or
negative.
 Expression or condition is usually a combination of variables and or
constants and operators.
 Examples of expressions used in if statement:
 (a>b) // combination of variables & relational operator
 (x<5) // combination of variables & relational operator
IF STATEMENT EXAMPLE
A

Out put
Let the user enter number is 4
Nothing happens because the condition/expressions are
false
IF ELSE STATEMENT
General syntax:-

OR
IF ELSE STATEMENT
 The if-else statement is slightly different version of if
statement.
 Here if the condition is found to be false other
statements are executed.
 If the condition or expression evaluates to true the
statement (single)/ block of statements following if is
executed, if it evaluates to false then the
statement(single)/block of statement s following the
keyword else is executed.
 The expressions/conditions are the same as described
above in if statement and may be relational
expressions.
IF ELSE STATEMENT EXAMPLE

Q1 write program that determine students are pass or


fail based on average (average>=50 pass otherwise
fail)
NESTED IF-ELSE STATEMENTS:
 C++ allows nesting of if-else statements ie. We can
have another if or if-else statement inside either the if
part or else part as follows:
NESTED IF-ELSE STATEMENTS EXAMPLE:
 c
CONDITIONAL OPERATOR:
 It is also known as ternary operator since it takes
three parameters.
 It is represented as ?:
 In one sense it is short form of if
 Its syntax is as follows:

 It is read as follows: If Expression 1 is true. i.e non


zero, expression 2 is returned else expression 3 is
returned.
CONDITIONAL OPERATOR: EXAMPLE 2
 Compare three numbers
SWITCH STATEMENT
 We use if statements to choose one among the
available alternatives.
 When the number of alternatives goes on increasing
the complexity to implement also goes on increasing.
 C++ has a multiway decision statement called
switch.
 The possible values of expression/condition is
represented by case.
 A switch statement can have multiple cases
representing multiple decisions to be taken.
 The keyword break is used inside every case of
switch statement to exit the statement once the case
is matched and executed.
 The syntax of a switch statement is as follows:
SWITCH STATEMENT
 The syntax of a switch statement is as follows:
SWITCH STATEMENT DESCRIPTION
 The integer expression is any expression that will evaluate to give an integer
value.
 The keyword case is followed by an integer or character constant which may
represent the value of integer expression.
 Each constant character or integer must be unique.
 Every case contains a set of valid c++ statements.
 The keyword break is the last statement inside every case and forces the
execution to come out of the switch statement.
 Without break the execution is said to fall through the cases i.e if there is no
break the execution proceeds to the next case even if it is not supposed to
execute.
 When a program containing a switch statement is executed the integer
expression following the switch keyword is evaluated first.
 This value is then matched one by one with the constant values that follow the
case keyword.
 If a match is found the statements following the case are executed.
 If no match is found then the statements under the default case are executed
SWITCH STATEMENT FLOW CHART
SWITCH STATEMENT EXAMPLE
.

Out put let the user enter 4


LOOPING FLOW OF CONTROL
 It is used when a set of instructions is to be executed
multiple times or for a fixed number of times until a
particular condition is satisfied.
 This repetitive action is done through Loop Control

Instruction.
 C++ offers the following Loop Control Instructions:

1. For

2. while
3. do while
FOR LOOP
 The for loop allows to specify three things in a single
line:
 Create and initialize loop counter variable

 Test condition

 Increase or decrease the value of loop counter after

every iteration .
For loop syntax:-

OR
FOR LOOP
Flow chart of for loop
FOR LOOP EXAMPLE
.

Write the output of the above program


Q1. Write a program that checks the odd number less than 20 using
for loop
WHILE LOOP
 The while loop has the following syntax:-

 condition is an integer expression just like the one we have seen in the before.
 When the system reads the keyword while it evaluates the expression that follows.
 If the expression is evaluated as true the statement following the condition is
executed.
 It may be a simple statement or a compound statement enclosed in parenthesis
{ }.
 Condition Check: In a while loop, the condition is checked before the execution of
the loop body.
 Execution: If the condition is initially false, the loop body may never execute.
WHILE LOOP EXAMPLE
.

Write the output of the above


Q1 Write a program that display odd number less than
20 using while loop
WHILE LOOP
 Flow chart
DO WHILE LOOP
 The do while loop has the following syntax:-
DO WHILE LOOP

 The do while loop executes in the same way as the while loop.
 The only difference is that in do executed at least once even if the condition is

evaluated to be false.
 A do-while loop will execute the block of code at least once before checking the loop

condition
 After the block is executed, the loop checks the condition specified in the while
statement.
 If the condition is true, the block of statements is executed again.
 If the condition is false, the loop terminates, and the program continues with the
statement following the do-while block.
 The do while statement is always terminated with a semicolon (;) after the loop ends.
DO WHILE LOOP

 Flow chart of do while


DO WHILE LOOP EXAMPLE
DO WHILE LOOP EXAMPLE
BREAK AND CONTINUE

Break statement
 Normally a while loop, a do..while loop, or a for loop will terminate only at the
beginning or at the end of the complete sequence of statements in the loop’s block.
 But sometimes there may be situations when you require the control to exit the loop.
 This can be fulfilled by using the break keyword.
 A break statement terminates a loop.
 The control is transferred to the first statement immediately following the loop
construct.
 We have seen the use of break statement in switch statement.
 When a match is found the statements under the case are executed and then break
statement causes the switch loop to terminate.
 A break statement is usually associated with a condition, so there will be an if
statement.
BREAK AND CONTINUE
General form of break statement
BREAK AND CONTINUE
Example

out put
BREAK AND CONTINUE

continue statement
 Sometimes there may be situations when you require the control
to exit the loop or skip the particular iteration and go to the next.
 This can be done using the keyword continue.
 A continue statement causes the rest of the body of the loop to
be omitted, for the current iteration.
 The control is transferred to the code that evaluates the normal
test condition for loop termination.
 The continue statement is similar to the break statement but
instead of terminating the loop, it transfers execution to the next
iteration of the loop.
 It continues the loop after skipping the remaining statements in
its current iteration.
BREAK AND CONTINUE

continue statement
General form of continue statement:
BREAK AND CONTINUE
Exampel

Out put
REVIEW QUESTION
1. What is flow control?
2. Compare while and do while loop?

3. what is syntax and semantics?

4. Write c++ program that determine the


input year is leap year or not
5. Write c++ program that display even,odd
and prime number less than the given
number using for,while,do while loop.
6. Write c++ program that display the below
structure
a b
CH-3 FUNCTION AND PASSING ARGUMENT TO FUNCTION
INTRODUCTION
Usually programs are much larger than the programs that we have seen so far.
To make large programs manageable and less complicated, they are broken down
into subprograms. These subprograms are called functions.
The basic principle of Functions is DIVIDE AND CONQUER. Using functions
we can divide a larger task into smaller subtasks that are manageable.
What is a function?
 A function is a self contained block of statements.
A function is self contained in the sense it may have its own variables and
constants .
 It is designed to do a well defined task.
Since a function is a part of a larger program (i.e. a subprogram)
it has a particular job to perform.
 It has a name. A function can be used (invoked) by the name
given to it.
 It may have return type .A function invoked by a calling program may or may
not return a value to it. In case it returns a value the functions return type is the
same as the variables data type.
 A program that has functions should have the following three things in it:
 Function Declaration or Prototype .
 Function Call .
 Function definition.
Why use Function?
 Functions are a structured way to programming. Larger programs get divided
into smaller manageable units.
 If a specific block of statements has to be executed multiple times ( for
example. taking contact details from 100 users), it can be written as a function
and that function can be repeatedly executed. This implies that redundancy in
writing the same piece of code multiple times is removed.
 Dividing a large program into smaller subprograms using functions help to
easily code them and reduces the debugging effort.
COMPONENT OF FUNCTION
1. Function prototype or function declaration :-It is the
name of the function along with its return-type and parameter
list.
2. Function call :-Any function name inside the main() followed
by semicolon (;) is a Function Call.
3. Function Definition: -A function name followed by a pair of
parenthesis {} including one or more statements.
In case of built-in functions, function prototype and function
definition are not necessary, they have been already declared
and defined in the libraries. • Consider the following example:

You might also like