Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

Class 3

Topic Covers
Control Statements
Control Statements are mainly divided into three.
Conditional Expressions: The conditional expressions are mainly used for decisio
n making. The following statements are used to perform the task of the condition
al operations.
if statement
if-else statement
nested if statement
else-if Ladder
switch statement
Loop Statements: The loop statements are essential to construct systematic blloc
k styled programming. In C, three various ways one may define the control struct
ures using different types of loop operations. The following are the loop struct
ures in C.
while loop
do-while loop
for loop
Breaking control statements/Jump statements:
For effective handling of the loop statements, C allows the use of the following
3 types of control break statements.
Break Statement
Continue Statement
goto statement
if statement in C
if statement
The if statement is used to write conditional expressions. If the given
condition is true then it will execute the statements. Otherwise it will
execute optional statements.
syntax:
if<conditon>
{
-----
-----
}
The expression is evaluated and if it is "true", the statement following the
if is executed. In case the given expression is "false", the statement is
skipped and execution continues with the next statement.
example:
/*EXAMPLE 1: Even Number Odd Number Checking in C*/
/*EXAMPLE 2: Comparing 2 Numbers Using Simple if Condition in C*/
if-else statement in C
if-else statement
The if-else statement is used to write conditional expressions. If the given
condition is true then it will execute the statements. Otherwise it will
execute else block statements.
syntax:
if<conditon>
{
-----
-----
}
else
{
-----
-----
}
The expression is evaluated and if it is "true", the statement following the
if is executed. In case the given expression is "false", the statement . under
else block will be executed.
example:
/* EXMAPLE 3: Leap Year Using Simple if-else Condition in C*/
nested if statement in C
The if statement within if statement is nested if.
syntax:
if(condition-1)
{
if(condition-2)
{
statement-1
}
}
else
statement-2
if the condition-1 is satisfied, then only it checks the condition-2. Otherwise
else block will be executed.
example:
/*EXAMPLE 4 : big no among three numbers*/
else-if Ladder
else-if ladder statement format
if(condition-1)
statement-1
else if(condition-2)
statement-1
else
statement-2
example:
/* EXAMPLE 5 : Biggest Number Among 4 Numbers*/
Switch statement in C
The switch statement is a multiway decision maker that tests whether an
expression matches one of the number of constant values, and braces accordingly.
syntax: switch(expression)
{
case constant1: statement; break;
case constant2: statement; break;
.
.
case constantn: statement; break;
default: statement;
}
* The expression whose value is being compared, may be any valid expression
including the value of the variable, an arithimatic expression, logical
comparision, a bitwise expression or the return value from a function
call, but not the floating point expression.
* The constants in each of the case statements must obiviously be of the
same type.
* The expression value is checked against each of the specified cases and
when a match occurs, the statement following that is executed. Again to
maintain generality, the statement can be either a simple or a compound
statment.
* The value that follows the keyword case may only be constants they can't
be expressions. They may be integers or characters, but not floating point
numbers or character strings.
* The last case of this statement which is called the default case is
optional and should be used according to the program's specific
requirement.
* Execution or the switch constant in C follows this logic. No statements
are executed until a case has been matched or the default case has been
encountered.
example:
/*EXAMPLE 6 Menu Driven Program For Arithematic Operations*/
While Loop
The while loop is used when we are not certain that the loop will be executed.
After checking whether the initial condition is true or false and finding it to
be true, only then the while loop will enter into the loop operations.
syntax:
For single condition
while<condition>
statement;
For a block of statements
while<condition>
{
statement-1;
statement-2;
......
}
* The condition cab be any valid expression including the value of a variable
a unary or binary expression, or the value returned by a function. The
statement can be a single or compound statement. The condition is actually
test condition.
* The while loop does not explicitly contain the initilization or
incrementation parts of the loop. These two statements are normally provided b
y the programmers.
initial condition
while(test condition)
{
statement-1
statement-2
change of initial condition
}
EXAMPLE:
/*EXAMPLE 8: Printing first 10 natural no's using while loop*/
/* EXAMPLE 9:Printing Table using while loop*/
/*EXAMPLE 10:Finding Sum Of Digits Using While Loop*/
/*EXAMPLE 11:Amstrong Number Using While Loop*/
do-While Loop
The do-while loop is used whenever one is certain about a test condition, then t
he do-while loop can be used. As it enters into the loop atleast once and then c
hecks whether the given condition is true or false. As long as the test conditio
n is true, the loop operations or statements will be repeated again and again.
Syntax:
do
{
statement-1
statement-2
----------
}while(condition);
EXAMPLE:
/* EXAMPLE 12 Menu driven program having the following options 1. Factorial of a
number 2. prime number 3. Even number*/

for Loop
The for loop is most commonly used in C. This loop consists of three
expressions. The first expression is used to initialize the index value,
the second to check whether or not the loop is to be continued again and the
third to change the index value for further iteration.
Sometimes the oprations carried out by the while loop can also be done by
using the for loop. Depending on the situation programmer can decide the loop.
Syntax:
for(exp1;exp2;exp3)
statement;
where expression-1 is the initialization of the expression or condition;
expression-2 is the condition checked as long as the the given
expression is true.
expression-3 is the incrementer or decrementer to change the index
value of the for loop variable.
EXAMPLE:
/*EXAMPLE 13 Printing Number And its Squares Using For Loop*/
/* EXAMPLE 14 printing using for loop*/
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
/* EXAMPLE 15 printing using for loop*/
12345
2345
345
45
5
/* EXAMPLE 16 printing using for loop*/
* * * * *
* * * *
* * *
* *
*
What are the differences between while and for loop?
Both while and for loops are iteration statements. The following are few
differences between both loops.
while loop
*it's a generic loop
* The while loop starts with the keyword while and valid condition and iterates
until the specified condition has been satisfied.
* The while loop endless loop.
for loop
* it's a numeric loop
* The for loop starts with the keyword for and iterates no.of times until the c
ondition is true.
* By using for loop we can stop the loop after some no.of iterations.
What are the differences between while and do-while loops?
while loop
* The condition will be at the begining of the loop
* The body of the loop will be executed after condition has been satisfied.
* The body of the loop will be executed if and only if the condition is
satisfied.

do-while
* The condition will be at the ending of the loop
* condition will be terminated with the semicolon.
* The body of the loop will be executed before conditon
* The body of the loop will be executed at least once even if the condition is
not satisfied
break statement
The break statement is used to terminate the control from the loop statements of
the case-structure. The break statement is normally used in the switch-case loo
p and in each case condition, the break statement must be used. If not, thecontr
ol will be transfered to the subsequent case condition.
syntax:
break;
example:
main()
{
int i=1;
while(i<=10)
{
if(i==5)
{
break;
}
printf("%d\n",i);
i++;
}
continue statement
The continue statement is used to repeat the same operations once again even
if it checks the error.
syntax:
continue;
The continue statement is used for the inverse operation of the break statement.
example:
main()
{
int i=1;
while(i<=10)
{
if(i==5)
{
continue;
}
printf("%d\n",i);
}
}
goto statement
The goto statement is used to alter the program execution sequence by
transfering the control to some other part of the program.
syntax:
goto label;
Here, label is the valid C identifier. There are 2 ways of goto statements
* Conditional goto * Unconditional goto
UnConditional goto:The unconditional goto statement is just used to transfer the
control from one part of the program to other part without checking any conditi
on.
example:
main()
{
start: printf("Welcome to c world");
goto start;
}
Conditional goto: The conditional goto is used to transfer the control of the
execution from one part of the program to the other in certain conditional
cases.
example:
main()
{
int value,i;
i=0;
while(i<=10)
{
printf("enter number:");
scanf("%d",&value);
if(value<=0)
{
printf("Zero or -ve value found");
goto error;
}
i++;
}
error: printf("Input data error");
}

You might also like