C Language Ch2
C Language Ch2
Unit – 2
DECISION MAKING WITH IF STATEMENTS
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