0% found this document useful (0 votes)
6 views40 pages

Lec 2

The document provides an overview of control statements in C programming, detailing three types: decision control, iterative statements, and jump statements. It explains various decision control statements such as if, if-else, if-else-if, and switch-case, along with their usage and examples. Additionally, it covers iterative statements including for, while, and do-while loops, highlighting their functionality and examples for practical understanding.

Uploaded by

ramiwael476
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views40 pages

Lec 2

The document provides an overview of control statements in C programming, detailing three types: decision control, iterative statements, and jump statements. It explains various decision control statements such as if, if-else, if-else-if, and switch-case, along with their usage and examples. Additionally, it covers iterative statements including for, while, and do-while loops, highlighting their functionality and examples for practical understanding.

Uploaded by

ramiwael476
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Data Structure

Lecture (2)
Control
Statements
Control Statements
• The C program is executed sequentially from the first line of the program to its
last line.
• In some cases, only selected statements need to be executed.
• Control flow statements enable programmers to conditionally execute a
particular block of code.
• There are three types of control statements:
➢ Decision control (branching): decides what actions have to be taken.
➢ Iterative (looping): decides how many times the action has to be taken.
➢ Jump statements: transfer control from one point to another point.
Decision
Control
Statements
Decision Control Statements
• C supports decision control statements that can alter the flow of a sequence of
instructions.
• These statements help to jump from one part of the program to another
depending on whether a particular condition is satisfied or not.
• These decision control statements include:
➢ if statement.
➢ if-else statement.
➢ if-else-if statement.
➢ switch-case statement.
if statement
if statement
• The simplest decision control statement that is frequently used in decision
making.
• The test expression is evaluated. If the test expression is true, the statements of
the if block are executed, otherwise these statements will be skipped and the
execution will jump to statement x.
• Note that there is no semi-colon (;) after the test expression. This is because
the condition and statement should be put together as a single statement.
Example

Output
• In case the statement block contains only one
statement, putting curly brackets becomes
optional.
• If there are more than one statement in the

Note
statement block, putting curly brackets
becomes mandatory.
if-else statement
if-else statement
• First, the test expression is evaluated. If the expression is true, statement block
1 is executed and statement block 2 is skipped. Otherwise, if the expression is
false, statement block 2 is executed and statement block 1 is ignored.

• In any case after the statement block 1 or 2 gets executed, the control will pass
to statement x. Therefore, statement x is executed in every case.
Example
• Write a C program to find whether a number is even or odd.
if-else-if statement
if-else-if statement
• After the first test expression or the first if branch, the programmer can have as
many else–if branches as he wants depending on the expressions that have to
be tested.

• If the Test Expression 1 evaluates to a true value, then the rest of the
statements in the code will be ignored and after executing the statement block
1, the control will jump to statement y.
Example
• Write a C program to
test whether a number
entered by the user is
negative, positive, or
equal to zero.

Output
Nested if
Example
• Write a C program to print
the maximum number
between three numbers.

Output
switch-case statement
switch-case statement
• A switch-case statement is a multi-way decision statement that is a simplified
version of an if-else if block.

• The switch-case statement compares the value of the variable given in the switch
statement with the value of each case statement that follows.

• When the value of the switch and the case statement matches, the statement
block of that particular case is executed.

• The variable used in the switch statement can be integer or character only.
switch-case statement
• Default is the case that is executed when the value of the variable does not
match with any of the values of the case statements. The default case is
optional.

• The break statement must be used at the end of each case because if it is not
used, then the case that matched and all the following cases including default
will be executed.

• The break statement tells the compiler to jump out of the switch case statement
and execute the statement following the switch-case construct.
Example
Example
• Write a C program to
determine whether the
entered character is a vowel
or not.
Nested
switch-
case
Conditional / Ternary Operator
• Conditional operators make the program code more compact, more readable,
and safer to use as it is easier to both check and guarantee the arguments that
are used for evaluation.
Example
Example
Iterative Statements
Iterative Statements
• Iterative statements are used to repeat the execution of a sequence of
statements until the specified expression becomes false.
• C supports three types of iterative statements also known as looping
statements:
➢ for loop.
➢ while loop.
➢ do-while loop.
for loop
for loop
• When a for loop is used, the loop variable is initialized only once.
• With every iteration, the value of the loop variable is updated and the
condition is checked.
• If the condition is true, the statement block of the loop is executed, else the
statements comprising the statement block of the for loop are skipped and the
control jumps to the statement following the for loop body.
Example
• Write a C program to print the first n numbers using a for loop?

Output
while loop
while loop
• In the while loop, the condition is tested before any of the statements in the
statement block is executed.
• If the condition is true, only then the statements will be executed, otherwise if
the condition is false, the control will jump to statement y outside the while
loop block.
• The condition of the while loop needs to be constantly updated. It is this
condition which determines when the loop will end. The while loop will
execute as long as the condition is true.
• If the condition is never updated and the condition never becomes false, then
the computer will run into an infinite loop which is never desirable.
Example
• Write a C program to print the first 10 numbers using a while loop?

Output
Example

• Write a C program to
calculate the sum of
numbers from m to n
using while loop?
do-while loop
do-while loop
• The do-while loop is similar to the while loop. The only difference is that in a
do-while loop, the test condition is tested at the end of the loop.
• As the test condition is evaluated at the end, this means that the body of the
loop gets executed at least one time (even if the condition is false).
Example
• Write a C program to print the first 10 numbers using a do-while loop?

Output
Example

• Write a C program to
calculate the sum and
average of first n numbers
using do-while loop?

You might also like