7/28/2019 11.Lect 16-17control Statdfv
1/24
Control Statements
The C language programs until now followsa sequential form of execution ofstatements.
Many times it is required to alter the flow of
the sequence of instructions. C language provides statements that can
alter the flow of a sequence of instructions.These statements are called controlstatements.
These statements help to jump from onepart of the program to another. The controltransfer may be conditional orunconditional.
7/28/2019 11.Lect 16-17control Statdfv
2/24
C ProgrammingControl Statements
Lecture 16-17
Mohit Arora
Lecturer , LIECA
LPU
7/28/2019 11.Lect 16-17control Statdfv
3/24
If Statement
The if . then . else statement can beused with or without the else.
The two forms are:
if (expression)
statement1
if (expression)statement1
else
statement2
In both cases, when the expression is true, then statement1 isexecuted.
If the expression is false, then, in the first case, statement1 is skipped(not executed)
In the second case, statement2 after the else is executed.
7/28/2019 11.Lect 16-17control Statdfv
4/24
The simplest form of the control statement is the If statement.It is very frequently used in decision making and allowing theflow of program execution.
The If structure has the following syntax
if (condition)statement;
The statement is any valid C language statement and thecondition is any valid C language expression, frequentlylogical operators are used in the condition statement.
The condition part should not end with a semicolon, since the
condition and statement should be put together as a singlestatement.
If the condition is true then perform the following statement orIf the condition is fake the computer skips the statement andmoves on to the next instruction in the program.
7/28/2019 11.Lect 16-17control Statdfv
5/24
# include //Include the stdio.h file
void main () // start of the program
{
int numbers; // declare the variables
printf ("Type a number:"); // message to the userscanf ("%d", &number); // read the number from standard input
if (number < 0) // check whether the number is a negative number{
printf ("The value is %d \n", number); // print the value}getch();}
7/28/2019 11.Lect 16-17control Statdfv
6/24
If Statement
body of if
testexpression
exit
true
7/28/2019 11.Lect 16-17control Statdfv
7/24
IF-ELSE
The if else is actually just on extension of thegeneral format of if statement.
If the result of the condition is true, thenprogram statement 1 is executed, otherwise
program statement 2 will be executed. If any case either program statement 1 is
executed or program statement 2 is executedbut not both
When writing programs this else statement isso frequently required that almost allprogramming languages provide a specialconstruct to handle this situation.
7/28/2019 11.Lect 16-17control Statdfv
8/24
IfElse Statement
if (a>b)
{
}
else
{
}
the if part is executed if the test statement is
true, otherwise the else part is executed.
7/28/2019 11.Lect 16-17control Statdfv
9/24
If Else Statement
body of if
testexpression
exit
true
false
body of else
7/28/2019 11.Lect 16-17control Statdfv
10/24
IfElse Statement#include //include the stdio.h header file in your program
void main () // start of the main
{
int num; // declare variable num as integer
printf ("Enter the number"); // message to the user
scanf ("%d", &num); // read the input number from keyboard
if (num < 0) // check whether number is less than zero
{
printf ("The number is negative"); // if it is less than zero then it is negative
}
else // else statement
{
printf ("The number is positive"); // if it is more than zero then the given number ispositive
}
getch();
}
7/28/2019 11.Lect 16-17control Statdfv
11/24
Compound Relational test C language provides the mechanisms necessary to perform
compound relational tests A compound relational test is simple one or more simple
relational tests joined together by either the logical AND or thelogical OR operators. The compound operators can be used toform complex expressions in C.
Syntax
if (condition1 && condition2 && condition3)
The syntax in the statement a represents a complex ifstatement which combines different conditions using the andoperator in this case if all the conditions are true only then thewhole statement is considered to be true. Even if one condition
is false the whole if statement is considered to be false.
7/28/2019 11.Lect 16-17control Statdfv
12/24
b> if (condition1 || condition2 || condition3) The statement b uses the logical operator or (||) to
group different expression to be checked. In thiscase if any one of the expression if found to be truethe whole expression considered to be true, we
can also uses the mixed expressions using logicaloperators and and or together.
7/28/2019 11.Lect 16-17control Statdfv
13/24
Nested if Statement The if statement may itself contain another if statement is known as
nested if statement.
Syntax:
if (condition1)
if (condition2)statement-1;
else
statement-2;elsestatement-3;
The if statement may be nested as deeply as you need to nest it. Oneblock of code will only be executed if two conditions are true. Condition1 is tested first and then condition 2 is tested.
The second if condition is nested in the first. The second if condition is tested only when the first condition is true
else the program flow will skip to the corresponding else statement.
7/28/2019 11.Lect 16-17control Statdfv
14/24
Nested IfElse Statement#include
#include
void main(){
int a=12,b=13,c=15;
if (a>b)
{
if (b>c)
{
printf(sorted = %d %d %d\n,a,b,c);
}
else if (a>c)
{
printf(sorted = %d %d %d\n,a,c,b);
}
else
printf(sorted = %d %d %d\n,c,a,b);
}
else
{
if (a>c)
printf(sorted = %d %d %d\n,b,a,c);
else
7/28/2019 11.Lect 16-17control Statdfv
15/24
Switch Statement the switch statement is used if there are more than
two alternatives and the decision depends on the valueof the same variable
the switch statement can replace a ladder of multiplenested if..else statements
switch()
{
case :
break;
case :
break;
default :
}
7/28/2019 11.Lect 16-17control Statdfv
16/24
7/28/2019 11.Lect 16-17control Statdfv
17/24
Switch Statement
variableequalsconst 1
exit
true
false
first case body
variableequalsconst 2
true
false
second case body
default body
7/28/2019 11.Lect 16-17control Statdfv
18/24
Switch Statement
char c;printf(Enter your choice (a/b/c) : );
Scanf(%c,&c);
switch (c)
{
case a:printf(You picked a!\n);
break;
case b:
printf(You picked b!\n);
break;case c:
printf(You picked c!\n);
break;
default:
printf(You picked neither a,b,c !\n);
}
7/28/2019 11.Lect 16-17control Statdfv
19/24
goto statementThe goto statement is a control flow statementthat causes the CPU to jump to another spot in the
code.
This spot is identified through use of a statementlabel.
The goto statement and its corresponding
statement label must appear in the same function.
7/28/2019 11.Lect 16-17control Statdfv
20/24
Jumps
Forward jump Backward jump
FORWARD JUMP
goto label:
----------------
----------------
label:
statement;
BACKWARD JUMP
label:
statement;
---------------------------------------
goto label;
7/28/2019 11.Lect 16-17control Statdfv
21/24
#include
#include
void main (){
int n=10;
loop:printf(value is :%d, n);n--;
if(n>0)goto loop;
printf("Done!);getch();
}
7/28/2019 11.Lect 16-17control Statdfv
22/24
7/28/2019 11.Lect 16-17control Statdfv
23/24
The primary problem with goto is that it
allows a programmer to cause the point
of execution to jump around the codearbitrarily.
There are some restrictions on goto
use. For example, you cant jumpforward over a variable thats initialized
in the same block as the goto
7/28/2019 11.Lect 16-17control Statdfv
24/24
drawback of goto statement
#include#includevoid main()
{
int x ;
printf(Enter the value);scanf(%d,&x);
getch();}