Switch, loop, goto, break,
continue
Switch
Switch
hi hello
Switch
Switch
65
hihello
The value of case 2,3 and default will be printed
Compilation error, the value of switch should be integer
bihellohiend
biend
Nothing will be printed as there is no case
Ordering of case does not matter
For input 1 and 2 hello will be printed
DECISION MAKING AND LOOPING
In looping, a sequence of statements are executed
until some conditions for termination of the loop are
satisfied.
A program loop therefore consists of two segments,
one known as the body of the loop and the other
known as the control statements.
Cont..
Depending on the position of the control statements in the
loop, a control structure may be classified either as an
entry-controlled loop or as the exit-controlled loop.
Loops In C
The C language provides three loop
constructs for performing loop operations.
They are:
The for statement
The while statement
The do while statement
The for Loop
The for loop allows us to specify
three things in a single line:
1. Setting a loop counter to an initial value.
2. Testing the loop counter to determine whether its
value has reached the number of repetitions desired.
3. Increasing/decreasing the value of loop counter each
time the program segment within the loop is executed.
General form of for loop
Example of for loop
This program prints number 1 to 10
Structure of for loop
Variation of for loop
Variation of for loop
Variation of for loop
Variation of for loop:
Infinite loop
Observing for loops
The initialization, loop-continuation
condition and increment/decrement can
contain arithmetic expressions. Assume
that x=2 and y=10. The statement-
for(j=x; j<=4*x*y; j=j+y/x)
is equivalent to-
for(j=2; j<=80; j=j+5)
Observing for loops
There can be a decrement as well (or
you
can say a negative increment). For
example-
Observing for loops
If the loop continuation condition is
initially
false, the body portion of the loop is not
performed.
Observing for loops
Nested for loops
Output
THE WHILE STATEMENT
The basic format of the while statement is
while(test condition)
{
body of the loop
}
The while is an entry–controlled loop statement.
The test-condition is evaluated 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 and if it
is true, the body is executed once again.
This process of repeated execution of the body continues until the test-condition
finally becomes false and the control is transferred out of the loop.
Example of WHILE Loop
sum = 0;
n = 1;
while(n <= 10)
{
sum = sum + n;
n = n + 1;
}
printf(“sum = %d \n”,sum);
-----------
-----------
THE DO STATEMENT
Cont..
Since the test-condition is evaluated at the bottom of
the loop, the do…..while construct provides an
exit-controlled loop and therefore the body of the
loop is always executed at least once.
Eg:
-----------
do
{
printf(“Input a number\n”);
Scanf(“%d”, &number);
}
while(number > 0);
-----------
What will be the output?
What will be the output?
The break Statement
We often come across situations
where we want to jump out of a loop
instantly, without waiting to get back
to the conditional test.
The keyword break allows us to do
this.
The break Statement
When break is encountered inside any
loop, control automatically passes to the
first statement after the loop.
When the loops are nested, the break
would only exit from the loop containing it.
That is, the break will exit only a single
loop.
break statement should be within loop or
within switch.
The break Statement
THE GOTO STATEMENT
C supports the goto statement to
branch unconditionally from one
point of the program to another.
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 GOTO STATEMENT
Why goto should be
avoided
Though, using goto statement give power
to jump to any part of program, it often
leads to code that is difficult to understand.
So In modern programming, goto
statement is considered a harmful
construct and a bad programming practice.
The continue
Statement
In some programming situations we
want to take the control to the
beginning of the loop, bypassing the
statements inside the loop, which have
not yet been executed.
The continue Statement
The keyword continue allows us to do
this. When continue is encountered
inside any loop, control automatically
passes to the beginning of the loop.
The continue Statement
The continue Statement
The continue Statement
Continue statement in Loops
References
1. http://www.computer-books.us/c_0008.php
2. http://www.computer-books.us/c_0009
3. http://www.computer-books.us/c_2.php
4. www.tutorialspoint.com/cprogramming/cprogramming_pdf.
5. Programming in C by yashwant kanitkar
6. ANSI C by E.balagurusamy- TMG publication
7. Computer programming and Utilization by sanjay shah Mahajan Publication
8. www.cprogramming.com/books.html
9. Rushdi Shams