0% found this document useful (0 votes)
84 views

Unit IV-Control and Iterative Structure

The document discusses control and iterative structures in C programming. It covers if, if-else, nested if, switch statements for decision making and conditional branching. It also discusses various loop structures like while, do-while and for loops. The key types of control structures covered are decision making statements, selection statements and iteration statements. Examples and practice programs are provided for different conditional and loop structures.

Uploaded by

Farzana Sheikh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Unit IV-Control and Iterative Structure

The document discusses control and iterative structures in C programming. It covers if, if-else, nested if, switch statements for decision making and conditional branching. It also discusses various loop structures like while, do-while and for loops. The key types of control structures covered are decision making statements, selection statements and iteration statements. Examples and practice programs are provided for different conditional and loop structures.

Uploaded by

Farzana Sheikh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Unit IV- Control and Iterative Structure

 If, If- Else Statements, Nested If Statements,


 Conditional Branching – switch statement,
 Loop (while, do…while, for),
 break, continue, goto statements

Introduction:
Control statements enable us to specify the flow of program control; i.e., the
order in which the instructions in a program must be executed. They make it
possible to make decisions, to perform tasks repeatedly or to jump from one
section of code to another.
There are four types of control statements in C:

1. Decision making statements/Conditional Statements


2. Selection statements
3. Iteration statements
4. Jump statements

Decision making statements/Conditional Statements

Conditional statements help you to make a decision based on certain conditions.


These conditions are specified by a set of conditional statements having Boolean
expressions which are evaluated to a Boolean value true or false. There are
following types of conditional statements in C.
1. If statement
2. If-Else statement
3. Nested If-else statement
4. If-Else If ladder

Prof. Farzana Azeem Sheikh Page 1

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

If statement:
The single if statement in C language is used to
execute the code if a condition is true. It is also
called one-way selection statement.
Syntax
if (condition)
Statements;
 If the expression is evaluated to nonzero
(true) then if block statement(s) are executed.
 If the expression is evaluated to zero (false)
then Control passes to the next statement
following it.

If-Else Statement:
The if-else statement in C language is used to execute the code if condition is true
or false. It is also called two-way selection
statement.
Syntax
if (expression)
{
True block of statements
}
else
{
False block of statements
}

 If the expression is evaluated to nonzero (true) then if block statement(s) are


executed.
 If the expression is evaluated to zero (false) then else block statement(s) are
executed.
Nested If-else statement

Prof. Farzana Azeem Sheikh Page 2

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

The nested if...else statement is used when a program requires more than one test
expression. It is also called a multi-way selection statement. When a series of the
decision are involved in a statement, we use if else statement in nested form.

Syntax
if( expression )
{
if( expression1 )
{
statement-block1;
}
else
{
statement-block 2;
}
}
else
{
statement-block 3;
}

If..else If ladder
The if-else-if statement is used to execute one code from multiple conditions. It is
also called multipath decision statement. It is a chain of if..else statements in which
each if statement is associated with else if statement and last would be an else
statement.
Syntax
if(condition1)
{
//statements
}

Prof. Farzana Azeem Sheikh Page 3

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

else if(condition2)
{
//statements
}
else if(condition3)
{
//statements
}
else
{
//statements
}
Example:
#include<stdio.h>
void main( )
{
int a;
printf("enter a number");
scanf("%d",&a);
if( a%5==0 && a%8==0)
{
printf("divisible by both 5 and 8");
}
else if( a%8==0 )
{
printf("divisible by 8");
}
else if(a%5==0)
{
printf("divisible by 5");
}
else
{
printf("divisible by none");
Prof. Farzana Azeem Sheikh Page 4

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

}
}

Practice Programs on Decision making statements/Conditional Statements

1. Program to check no. is greater than 5.


2. Program to check no. is even or odd.
3. Program to accept a character from user and check whether it is vowel or
not.
4. Program to check the no. is positive, negative or zero.
5. Program to print the no. is divisible by 5 and 11 or not.
6. Program to find greatest no. among 2 nos.
7. Program to find greatest no. among 3 nos.
8. Program to accept total marks of a student and display grade.
Per>=60First Class
Per<=59 and per>=55 Higher Second class
Per<=54 and per>=40  Second class
Fail

Prof. Farzana Azeem Sheikh Page 5

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

Selection statements/Conditional Branching


Switch Statement
Switch statement is used for multiple choice or selection. It is used as a substitute
of if-else statements. It is used when multiple choices are given & one choice is
to be selected.
Syntax
switch(expression)
{
case const-1:
statements;
break;
case const-2:
statement;
break;
.
case const-n:
statement;
break;
default:
statements:
}
 The expression is evaluated once and compared with the values of
each case label.
 If there is a match, the corresponding statements after the matching label are
executed.
 If there is no match, the default statements are executed.
 If we do not use break, all statements after the matching label are executed.
Prof. Farzana Azeem Sheikh Page 6

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

 The default clause inside the switch statement is optional.

Example:
int a = 1;
switch (a) {
case 1:
printf("a is 1");
break;
case 2:
printf ("a is 2");
break;
default:
printf("a is neither 1 nor 2");
break;
}
Example:
#include <stdio.h>
main()
{
int a;
printf("Enter Your choice");
scanf("%d",&a);
switch (a) {
case 1 ...6:
printf("%d in range 1 to 6\n", a);
break;
case 15 ... 20:
printf("%d in range 15 to 20\n", a);
Prof. Farzana Azeem Sheikh Page 7

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

break;
default:
printf("%d not in range\n", a);
break;
}
}
Practice Programs on Switch Statement
1. Write a program to implement basic calculator operations.
2. Write a program to read a number from 0-9 and display it in word.
3. Write a program to accept a character and check if it vowel or not.
4. Write a Menu Driven program to display the price of the selected item based
on no. of items. (Price per item is given in bracket)
B=BURGER (100Rs.)
F=FRENCH FRY (50Rs.)
P=PIZZA (300Rs.)
S=SANDWICHES (150 Rs.)
5. Write a menu driven program to read time in hrs and display Greeting
Message accordingly.
Example: hrs < 12
greet = 'Good Morning';
hrs >= 12 && hrs <17
greet = 'Good Afternoon';
hrs >= 17 && hrs <= 24
greet = 'Good Evening';

Trace the Output Questions:


Program 1:

Prof. Farzana Azeem Sheikh Page 8

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

#include <stdio.h>
void main()

    int a=2;
    switch(a)
    {
        printf("Message\n");
        default:
            printf("Default\n");
        case 2:
            printf("Case-2\n");
        case 3:
            printf("Case-3\n");
    }
    printf("Exit from switch\n");
}

Case-2
Case-3
Exit from switch
Program 2:
#include <stdio.h>
int main()

Prof. Farzana Azeem Sheikh Page 9

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

{
    int x;
    float y=7.0;
    switch(x=y+1)
    {
        case 8: printf("It's Eight."); break;
        default: printf("Oops No choice here!!!");
    }
}
 It's Eight
Program 3
#include<stdio.h>
void main()
{
int movie = 1;
switch (movie << (2 + movie))
{
default:
printf(" Traffic");
case 4:
printf(" Sultan");
case 5:
printf(" Dangal");
case 8:
printf(" Bahubali");
}
}
 Bahubali

Prof. Farzana Azeem Sheikh Page 10

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

Program 4
#include<stdio.h>
#define L 10
void main()
{
auto a = 10;
switch (a, a*2)
{
case L:
printf("ABC");
break;
case L*2:
printf("XYZ");
break;
case L*3:
printf("PQR");
break;
default:
printf("MNO");
case L*4:

Prof. Farzana Azeem Sheikh Page 11

[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure

printf("www");
break;
}
}
 XYZ

Iteration statements:

Refer notes given and Program taken in lecture

Prof. Farzana Azeem Sheikh Page 12

[email protected]
B.C.A. (Science) Department, AISC

You might also like