C-Unit 3
C-Unit 3
1 CONTROL STATEMENTS/STRUCTURES:
C program is a collection of statements, which are executed in sequentially in the order in which they appeared. If we
want to change the order of execution of statements based on certain conditions, then the kind of control structures are
used. Control Structures are of two types.
Decision making Statements/ Decision making Branching/ Selection Statements
Decision Looping Statements
Decision making Statements:
C Support the following conditional constructs
* Simple if statement
* if....else statement
* if...else if statement/Nested if statement
*Switch case statement
SIMPLE – IF STATEMENT:
If Statement is a powerful decision making statement is used to control the execution flow of statement in the
program. The general syntax of simple if statement is as follows.
Syntax:
if ( test condition )
{
Statement-1;
}
Statement-x(Next Statement);
From the above syntax, at first test condition is evaluated. If the test condition is true, then statement-1 will get
executed. If the test condition is false then it skips out of the program.
Flowchart:
Example:
#include<stdio.h>
Void main()
{
int A;
printf(“Enter A Value :”);
scanf(“%d”,&A);
if(A>0)
{
printf(“ A is Positive ”);
}
}
Output:
Enter A Value : 5
A is Positive
IF...ELSE STATEMENT:
An if...else statement is an extension of simple if statement. An if...else statement is constructed by adding else part to
simple if statement. The general syntax of if...else statement is as follows
Syntax:
if (test condition )
{
Statement-1;
}
else
{
Statement-2;
}
From the above syntax, at first test condition is evaluated. If the test condition is true, then statement-1 will get
executed. If the test condition is false, then statement-2 will get executed.
Flowchart:
Example:
/*C Program to check whether the given number is positive or negative using if-else*/
#include<stdio.h>
Void main()
{
int A;
printf("Enter A value:");
scanf("%d",&A);
if (A>0)
{
printf("A is positive");
}
else
{
printf("A is negative");
}
}
Output:
Enter A value: 56
A is Positive
IF...ELSE IF STATEMENT:
An if statement containing another if statement is known as Nested if satatement. The general syntax of if...else if
statement is as follows.
Syntax:
if ( test condition-1)
{
Statement-1;
}
else if ( test condition-2 )
{
Statement-2;
}
else
{
Statement-3;
}
}
In the above syntax, at first test condition-1 is evaluated, if the test condition-1 is true then statement-1 will
get executed. If the test condition-1 is false, it comes to else block, with in else block there is another test condition-2,
here this test condition-2 is going to be evaluated. If the test condition-2 is true then statement-2 will get executed, if
the test condition-2 is false then statement-3 will get executed.
Flowchart:
Example:
/* C Program to find biggest among 3 digits */
#include<stdio.h>
Void main()
{
Int A,B,C;
printf("Enter A Value:");
scanf("%d”,&A);
printf(“Enter B Value:”);
scanf(“%d”,&B);
printf(“Enter C Value:”);
scanf(“%d”,&C);
{
if(A>B && A>C)
{
printf("A is Biggest");
}
else if(B>C)
{
printf("B is Biggest");
}
else
{
printf("C is Biggest");
}
}
Output:
Enter A Value:25
Enter B Value:2
Enter C Value:16
A is Biggest
SWITCH CASE STATEMENT:
The Switch statement is another form of conditional control statement which is used to select one option from many
options based on given condition/expression. This is an alternative to the if-else-if ladder.
The switch statement is also called as multi-way decision making statement.
The switch statement evaluates expression and then looks for its value among the case constants.
If the value matches with case constant, then that particular case statement is executed.
If no one case constant not matched then default is executed.
The general syntax of switch case statement is as follows
Syntax:
Flowchart:
Example:
/*C Program to illustrate switch statement */
#include<stdio.h>
Void main()
{
int i;
printf("Enter i value:");
scanf(“%d”,&i);
switch(i)
{
case 1:
printf("ONE”);
break;
case 2:
printf("TWO”);
break;
case 3:
printf("THREE”);
break;
case 4:
printf("FOUR”);
break;
default:
printf("you have entered wrong choice");
}
}
OUTPUT:
Enter i Value: 3
THREE
IMPORTANCE OF BREAK STATEMENT WITH SWITCH
The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached,
the switch terminates, and the flow of control jumps to the next line following the switch statement.
The break statement is optional. If omitted, execution will continue on into the next case.
(Syntax & example program is same as switch).
CONDITIONAL OPERATOR:
C programming conditional operator is also known as a ternary operator. It takes three operands.
Conditional operator is closely related with if..else statement.
SYNTAX:
(condition) ? expression1 : expression2
Example:
(A>B)? “A is greater” : “B is greater” ;
PROGRAM:
#include <stdio.h>
Void main()
{
int mark;
printf("Enter mark: ");
scanf("%d", &mark);
(mark>= 40 ? printf(“Pass”) :printf( "Failed") );
}
Output:
Pass
Example:
#include<stdio.h>
void main()
{
int i;
i=1;
while(i<=100)
{
Printf(“%d”,i);
i++;
}
}
do while loop:
In do while loop, the body of loop is executed, if the condition is false for the first time. In some cases it is necessary
to execute body of loop when the condition is false, this can be handle with the help of do while. It is also known as
exit control loop statement. The general syntax of do while statement is as follows
Syntax:
do
{
Block of statements;
}
while( test condition ) ;
From the above syntax at first block of statements will get executed then the test condition is evaluated. If the
test condition is true, then the block of statements will get executed again. If the test condition is false it skips out of
the program.
Flowchart:
Example:
#include<stdio.h>
void main()
{
int n, rev, rem;
rev=0;
printf(“Enter n value:”);
scanf(“%d”,&n);
do
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}while(n!=0);
Printf(“Rev of number is:%d”,rev);
}
OUTPUT:
Enter n value: 123
Rev of number is: 321
FOR LOOP:
The general syntax of for loop is as follows
Syntax:
for ( initialization ; condition ; increment/decrement )
{
Block of statements;
}
Flowchart:
Example:
NESTED LOOPING:
Using a for loop within another for loop is said to be nested for loop. In nested for loop one or more statements can
be included in the body of the loop. In nested for loop, the number of iterations will be equal to the number of
iterations in the outer loop multiplies by the number of iterations in the inner loop.
Syntax:
for ( initialization ; condition ; increment/decrement )
{
for ( initialization ; condition ; increment/decrement )
{
//body of the loop
}
}
Flowchart:
Example:
#include <stdio.h>
void main()
{
int n, i, j;
printf("Enter the value of n:");
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
{
printf(" %d\t”, i*j);
}
}
It doesn’t execute the body of loop, if It will execute the body of loop, if
the specified condition is false for the the specified condition is false for
first time the first time
It is also called as pre- testing loop It is also called as post- testing loop
BREAK STATEMENT:
In C programming, break statement is used with conditional if statement. The break is used in terminating the loop
immediately after it is encountered. it is also used in switch...case statement.
Syntax:
Break;
Flowchart:
Example:
GOTO STATEMENT:
In C programming, goto statement is used for altering the normal sequence of program execution by transferring
control to some other part of the program.
Synatax:
goto lable;
…………………………;
………………………….;
lable:
statement;
Flowchart:
goto lable;
…………………………;
………………………….;
lable:
statement;
Example:
/*C program that illustrates goto statements */
#include<stdio.h>
Void main()
{
goto xyz;
printf("First statement");
xyz:
printf("Second statement”);
}
CONTINUE STATEMNET:
It is sometimes desirable to skip some statements inside the loop. In such cases, continue statement is used.
Syntax:
continue;
Flowchart:
Example:
/*C program to illustrate continue statement */
#include<stdio.h>
Void main()
{
int i, n;
i=1;
printf(“enter n value:”);
scanf(“%d”,&n);
while(i<=n)
{
Printf(“%d”,i);
Continue;
}
}
The Break statement is used to exit from the loop The continue statement is not used to exit
constructs. from the loop constructs.
The break statement is usually used with the switch The continue statement is not used with the
statement, and it can also use it within the while switch statement, but it can be used within the
Break Statement Continue Statement
loop, do-while loop, or the for-loop. while loop, do-while loop, or for-loop.
When a break statement is encountered then the When the continue statement is encountered
control is exited from the loop construct then the control automatically passed from the
immediately. beginning of the loop statement.
Syntax: Syntax:
break; continue;
5.STRUCTURED PROGRAMMING:
Sequential: default mode. Sequential execution of code statements (one line after another) -- like following a recipe
Selection: used for decisions, branching -- choosing between 2 or more alternative paths. In C++, these are the types
of selection statements:
If
if/else
switch
Repetition / Iteration: used for looping, i.e. repeating a piece of code multiple times in a row. In C++, there are three
types of loops:
while
do/while
for