[go: up one dir, main page]

0% found this document useful (0 votes)
61 views16 pages

C-Unit 3

This document discusses various control statements and structures in C programming including: 1. Decision making statements like if, if-else, if-else if, and switch case statements for controlling program flow based on conditions. 2. Looping/iterative statements like while, do-while, and for loops that repeat a block of code. 3. Examples are provided for each control structure to illustrate their syntax and usage. Key concepts like break statements, nested loops, and conditional operators are also covered.

Uploaded by

Sankaraiah Mudi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views16 pages

C-Unit 3

This document discusses various control statements and structures in C programming including: 1. Decision making statements like if, if-else, if-else if, and switch case statements for controlling program flow based on conditions. 2. Looping/iterative statements like while, do-while, and for loops that repeat a block of code. 3. Examples are provided for each control structure to illustrate their syntax and usage. Key concepts like break statements, nested loops, and conditional operators are also covered.

Uploaded by

Sankaraiah Mudi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

UNIT 3

DECISION MAKING, ITERATIVE AND OTHER CONTROL STATEMENTS/STRUCTURES

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:

/*C program to illustrate simple – if statement */

#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:

switch (variable or expression)


{
Case value1:
Statement-1;
break;
Case value2:
Statement-2;
break;
________
________

default: default – statement;


}

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:

/* C PROGRAM TO ILLUSTRATE CONDITIONAL OPEARTOR */

#include <stdio.h>
Void main()
{
int mark;
printf("Enter mark: ");
scanf("%d", &mark);
(mark>= 40 ? printf(“Pass”) :printf( "Failed") );
}

Output:
Pass

LOOPING /ITERATIVE STATAEMENTS:


The process of executing a block of statements repeatedly is known as looping. Looping statements are used to repeat
a block of statements again and again until certain condition satisfy.
Decision looping statements are of three types
1. while loop
2. do while loop
3. for loop
While loop:
The while loop will be executed repeatedly as long as the expression remains true. The general syntax of while loop is
as follows
Syntax:
while (test condition)
{
block of statements
}
While loop is also called as entry control loop. Here at first test condition is evaluated. If the test condition is
true, then the block of statements will get executed. If the test condition is false then it skips out of loop.
Flowchart:

Example:

/*C Program to print numbers from 1 to 100 in order using while loop*/

#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:

/*C Program to reverse given number using do while*/

#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:

/*C Programto print numbers from 1 to 100 using for loop*/


#include<stdio.h>    
void main()    
{    
  int n;
for(n=1;n<=100;n++)
{
Printf(“%d”,n);
 }    
}

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);
 }
}

2. DIFFERENCE BETWEEN WHILE AND DO WHILE LOOP

While loop Do while loop

While loop is an entry control loop Do while is an exit control loop

Do while loop executes the body of


while loop will test, condition first loop first
While loop Do while loop

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

Syntax(refer previous notes) Syntax(refer previous notes)

Flow chart(refer previous notes) Flow chart(refer previous notes)

3. UN-CONDITIONAL CONTROL STATEMENTS


 Break
 Continue
 Go to

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:

/* C program to illustrate break statement */


#include<stdio.h>  
void main ()  
{  
    int i;  
    for(i=1;i<=10;i++)
{
If(i==6)
{
Break;  
    }    
    printf("%d",i);  
}  

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;
}
}
    

4. DIFFERENCE BETWEEN BREAK AND CONTINUE STATEMENT

Break Statement Continue Statement

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:

Structured programming is a subset of procedural programming. It is also known as modular programming.The


structured programming language allows a programmer to code a program by dividing the whole program into smaller
units or modules. Structured programming is not suitable for the development of large programs and does not allow
the reusability of any set of codes. 
Flow of control through any given function is implemented with three basic types of control structures:

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

Advantages of Structured Programming Language:


 Structured programming is user-friendly and easy to understand.
 In this programming, programs are easier to read and learn.
 It avoids the increased possibility of data corruption.
 The main advantage of structured programming is reduced complexity.
 Increase the productivity of application program development.
 Application programs are less likely to contain logic errors.
 Errors are more easily found.
 It is easier to maintain.
 It is independent of the machine on which it is used, i.e. programs developed in high-level languages can be
run on any computer.
Disadvantages Of Structured Programming Language:
 Same code repetition
 Lack of information hiding

You might also like