Unit 2 of 2
Unit 2 of 2
Unit 2 of 2
20 Vnit Tue
Difference Between break and continue are listed below.
Break Continue
A break can appear inboth switch and A continue can appear only in loop (for,|
A break causes the switch or loop A continue doesn't terminate the loop, it
statements to terminate the moment it | causes the loop to go to the next iteration.
is executed. Loop or switch ends All iterations of the loop are executed
abruptly when break is encountered. even if continue is encountered. The
continue statement is used to skip
statements in the loop that appear after
the continue.
The break statement can be used in | The continue statement can appear only
both switch and loop statements. in loops. You will get an error if this
appears in switch statement.
When a break statement is When a continue statement sis
encountered, it terminates the block encountered, it gets the control to the
and gets the control out of the switeh next iteration of the
loop.
or loop
A break causes the innermost A continue inside a loop nested within a
enclosing loop or switch to be exited switch causes the next loop iteration
immediately.
Q.2. Write the syntax of ternary
operator and write a
program to find the biggest from
four numbers by using
ternary operator.
Ans. Syntax of ternary operator:
val =condition ? vall val2;
Program to find the biggest from four numbers using ternary operator
#includecstdio.h>
#include <conio.h>
int main)
getch(
return 0;
Q.3. Compare and contrast the switch with if-else-ladder with example.
Ans.
Ans. If-else-ladder and switch case both are used to control the flow of program.
Difference Between switch and if-else-ladder are listed below.
If-else switch
You can have multiple if statement for In switch you only have one expression for
multiple choice of statements. the multiple choices.
If-else statement checks for equality as switch checks only for equality.
well as for logical expression.
Ifexpression inside if turn outs to be false, If expression inside switch statement turn
statement inside else block will be out to be false then default statements is
executed. executed.
4Compare and contrast the entry controlled loop and exit controlled loop with
suitable example program. IDec 2017]
n5, Loop is a group of instructions compiler executes repeatedly while some condition
remains true.
Loops are classified into two types. They are Entry control loop and Exit cono
loop
Entry control loop:
n this control loop compiler will check condition first. If condition is true then
2.22 Vnit Tu
compiler move into the body. If condition fail compiler doesn't execute body atleast
once. Entry control loop is also called as Pre-checking control loop. Ex: while, for
Exit control loop:
In this kind of control loop compiler first executes the body then it go for the
condition. If condition is true again it goes to the body. If condition fails it goes out
of the body. So in exit control loop without knowing condition compiler execute
body minimum one time. Exit control loop is also as post control loop.
Q.5. Discuss about Multi-way conditional statement with suitable examples.
Ans. A multi-way selection statement is used to execute at most ONE of the choices of a
set of statements presented.
case CONSTANT1:
one or more statements;
break;
case CONSTANT2:
one or more statements;
break;