0% found this document useful (0 votes)
28 views21 pages

C Language Ch2

The document discusses various decision making and looping statements in C language including if, if-else, nested if-else, switch case, while, for, do-while loops. It provides examples and explanations of how each statement works, the basic syntax and flow. It also covers goto, break, and continue statements used to control program flow within loops.

Uploaded by

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

C Language Ch2

The document discusses various decision making and looping statements in C language including if, if-else, nested if-else, switch case, while, for, do-while loops. It provides examples and explanations of how each statement works, the basic syntax and flow. It also covers goto, break, and continue statements used to control program flow within loops.

Uploaded by

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

C language

Unit – 2
DECISION MAKING WITH IF STATEMENTS

The if statements is a powerful decision–making


statements and is used to control the flow of execution
of statements. It is basically a two–way decision
statement and is used with an expression. It takes the
following form:
if(condition)
It allows the computer to evaluate the expression
first and then, depending on whether the value of
the expression (relation or condition) is ‘true’ (or
non–zero) or ‘false’ (zero), it transfers the control
to a particular statement
DECISION MAKING WITH IF STATEMENTS
The different forms are:

Simple if statement
Simple if…else statement
Multiple if statement
Nested if…else statement
if…else if…else if…else ladder
SIMPLE IF STATEMENT
The general form of a simple if statements is:
 if (test expression)
{
 statement–block;
}
 statements;

 The ‘statement–block’ may be a single statement or a group of


statements. If the test expression is true, the statement–block will be
executed, otherwise the statement–block will be skipped and the
execution will jump to the statements following if block.
THE IF…ELSE STATEMENT
 The if…else statement is an extension of the simple if statement. The general
from of if…else statement is :
 if (test expression)
{ true-block statement(s);
}
 else
{ false-block statement(s);
}
 statements;
 If the test expression is true, then the true-block statement(s),
immediately following the if statements are executed; otherwise,
the false-block statement(s) are executed. In either case, either
true-block or false-block will be executed, not both.
MULTIPLE IF
 When a number of conditions are checked, we may use more than one if statements as below:
 if (test–expression1)
 { statement–block1;
 }

 if (test–expression2)
 { statement–block2;
 }
 …
 …
 …
 if (test–expressionN)
 { statement–blockN;
 }
 In multiple if structure, all the conditions written in each if are
checked.
NESTING OF IF…ELSE STATEMENTS
 When a series of decisions are involved, we may have to use more than one if…else statement in
nested form as shown below:

 If the condition-1 is false, the statement-3 will be executed; otherwise it continues to


perform the second test. If the condition-2 is true, the statement-1 will be evaluated;
otherwise the statement-2 will be evaluated and then the control is transferred to the
statement-x.
THE IF…ELSE IF…ELSE IF…ELSE LADDER
 A multipath decision is a chain of ifs in which the statement associated
with each else is an if. It takes the following general form:

 This construct is known as the else if ladder. The conditions are evaluated
from the top of the ladder to downwards. As soon as a true condition is
found, the statement associated with it is executed and the control is
transferred to the statement-x ( skipping the rest of the ladder). When all
the n conditions become false, then the final else containing the default-
statement will be executed.
THE SWITCH STATEMENT
 C has a built-in multiway decision statement known as a switch. The switch statement tests the
value of a given variable ( or expression ) against a list of case values and when a match is found, a
block of statements associated with that case is executed. The general form of the switch
statement is as shown below:
 switch ( expression )
 {
 case value-1;
 block-1
 break;
 case value-2;
 block-2
 break;
 ……….
 ……….
 default:
 default-block
 break;
 }
 statement-x;
THE SWITCH STATEMENT
 The expression is an integer expression or characters, which is known
as case value.
 block-1, block-2 ….. are statement lists and may contain zero or
more statements. There is no need to put braces around these blocks.
Note that case labels end with a colon (:).
 When the switch is executed, the value of the expression is
successfully compared against the values value-1, value-2,... If a case is
found whose value matches with the value of the expression, then the
block of statements that follows the case are executed.
 The break statement at the end of each block signal the end of a
particular case, and moves to statement-x.
 The default will be executed if the value of the expression does not
match with any of case values.
Looping statements
 When we want to execute a block of statements repeatedly, it is called
looping.
 In looping, sequences of statements are executed until some conditions for
termination of the loop are satisfied.
 A program containing the loop consists of two segments, one known as
the body of the loop and the other known as the control statement. The
control statement test certain conditions and then directs the repeated
execution of the statements contained in the body of loop.
 A looping process, in general, would include the following four steps:
 Initialization of a looping variable.
 Execution of the statements in the loop.
 Test for a specified value of the looping variable for execution of the
loop.
 Incrementing, decrementing of looping variable.
While loop
 They 3 types of loop:
1. The while statement.
2. The do statement.
3. The for statement.

 1. while loop:
 The simplest of all the looping structures in C is the while statement. The basic format of while
statement is:

 while (test condition)
 {
 body of the loop;
 }

 The test-condition is evaluated (checked) and if the condition is true, then the body of the loop is
executed. After execution of the body, the test-condition is once again evaluated (checked) and if it is
true, the body of the loop is executed once again. This process of repeated execution of the body
continues until the test-condition becomes false and the control is transferred out of the loop. On
exit, the program continues with the statement immediately after the body of the loop.
For loop
2 for loop:
 The for loop is another entry-controlled (Pre Tested) loop.
 for ( Initialization ; Test-Condition ; Increment / Decrement )
{
 body of the loop;
}
 Initialization of the control variables is done first, using assignment
statements such as a=1 and count = 0. The variables a and count are known
as loop-control variables.
 The value of the control variable is tested using the test-condition. The test-
condition is a relational expression, such as a<=10.
 the control variable is incremented using an assignment statement such as
a=a+1 and the new value of the control variable is again tested to see whether
it satisfies the loop condition.
For loop
 Example:
 Int x;
 For (x=1 ; x<=10 ; x=x+1)
{ printf (“\nWelcome students”);
}
 Printf (“End of program”);
 This for loop is executed 10 times and prints the digits 1 to 10 on new
line.
Do while loop
 The for and while loop tests the condition before executing the loop.
If the condition satisfies, the loop will be executed.
 In do statement, evaluate the body of the loop first. At the end of the
loop, the test-condition in the while statement is evaluated (checked).
 Since the test-condition is evaluated at the bottom of the loop, the
body of the loop is always executed at least once.
 do
 {
 body of the loop;
 }
 while ( test-condition );

All 3 loops
For While do
for (n=1;n<=10; ++n) n = 1; n = 1;
{ while (n <= 10) {
----------------- { ------------------
----------------- ----------------- ------------------
} ----------------- n = n + 1;
n = n + 1; }
while (n<= 10);
}

Nested loop: the loop within a loop is called nested loop. The outer loop controls
The rows and inner loop controls the column.
Go to statement
C supports the goto statement to branch
unconditionally from one point to another in the
program.
The goto requires a label in order to identify the place
where the branch is to be made. A label is any valid
variable name, and must be followed by a colon. The
label is placed immediately before the statement
where the control is to be transferred.
Go to statement
The label: can be anywhere in the program either
before or after the goto label;
go to label; label:

------------- statement;

-------------

------------- go to label;

label:

statement;
Break statement
Break statement is used to stop executing further statement and exit
from current block.
This break statement can be used in switch case, while, for and do
while loop.
When a break statement is encountered inside a loop, the loop is
immediately exited and the program continues with the statement
immediately following the loop.
Example:
For (i=1;i<=10;i++)
{ if (i ==5)
 break;
}
In above example, break will stop the execution of loop when value
of “i” will be 5.
Continue
During the loop operations, it may be necessary to skip
a part of the body of the loop under certain conditions.
Like the break statement, C supports another similar
statement called the continue statement.
However, unlike the break which causes the loop to be
terminated, the continue causes the loop to be
continued with the next iteration after skipping current
statement. The continue statement tells the compiler,
“SKIP THE FOLLOWING STATEMENTS AND
CONTINUE WITH THE NEXT ITERATION”.
Assignment - 2
Q. No. Questions Marks
1 Explain if … else statement with its type 5
2 Explain switch case statement in detail. 5
3 Explain Conditional (Ternary) Operator. (unit – 1) 3
4 What is loop? Explain types of loop. 5
5 Explain Break statement. 3
6 Explain continue statement. 3
7 Explain goto statement. 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy