Module Ii

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

MODULE II

Control flow: If statement, if….else statement, nested if ..else statement, switch statements, looping
– for loop , while loop, do … while statements, nested loop structure, break, continue and go to
statements.

Decision Making and Branching

A program contains a sequence of instuctions. C conditional statements allow you to make a


decision, based upon the result of a condition. These statements are called Decision Making
Statements or Conditional Statements.

 if statement
 switch statement
 conditional operator statement (? : operator)
 goto statement

Decision making with if statement

The if statement may be implemented in different forms depending on the complexity of conditions
to be tested. The different forms are,

1. Simple if statement
2. if....else statement
3. Nested if....else statement
4. Using else if statement
5. Simple if statement

Simple if statement

The general form of a simple if statement is,

if(expression)

True block statements;

statement x;

If the expression returns true, then the statement-inside will be executed, otherwise statement-
inside is skipped and only the statement-outside is executed.
Example:

Write a program to find biggest of 2 numbers

#include <stdio.h>

void main( )

int x, y;

x = 15;

y = 13;

if (x > y )

printf("x is greater than y");

}
#include<stdio.h>

void main()

int number;

printf("Type a number:");

scanf("%d", &number);

if (number < 0)

number = -number;

printf("The absolute value is %d\n", number);

if...else statement

The general form of a simple if...else statement is,

if(expression)

True block statements;

else

False block statements;

If the expression is true, the statement-block1 is executed, else statement-block1 is skipped and
statement-block2 is executed.
Example:

Write a program to find biggest among 2 numbers

#include <stdio.h>

void main( )

int x, y;

x = 15;

y = 18;

if (x > y )

printf("x is greater than y");

else

printf("y is greater than x");

}
}

Write a program to find the given number is positive or negative

#include<stdio.h>

void main() {

int num;

printf("Enter the number:");

scanf("%d", &num);

if (num < 0)

printf("The number is negative.");

else

printf("The number is positive.");

Write a program to find the given number is even or odd

#include <stdio.h>

#include<conio.h>

void main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

if(num % 2 == 0)

printf("%d is even.", num);

else

printf("%d is odd.", num);

getch();

Nested if....else statement

The general form of a nested if...else statement is,

if( test condition1 )

if(test condition2 )
{

statement block1;

else

statement block2;

else

statement block3;

Ststement x;

if expression is false then statement-block3 will be executed, otherwise the execution continues and
enters inside the first if to perform the check for the next if block, where if expression 1 is true the
statement-block1 is executed otherwise statement-block2 is executed.

Example:

Write a program to find the biggest of 3 numbers

#include <stdio.h>

void main( )

int a, b, c;
printf("Enter 3 numbers...");

scanf("%d%d%d",&a, &b, &c);

if(a > b)

if(a > c)

printf("a is the greatest");

else

printf("c is the greatest");

else

if(b > c)

printf("b is the greatest");

else

printf("c is the greatest");

}
Switch statement

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the
case match is found, a block of statements associated with that particular case is executed.

Syntax:

switch( expression )

case value-1:

Block-1;

Break;

case value-2:

Block-2;

Break;

case value-n:

Block-n;

Break;

default:

Block-1;

Break;

Statement-x;

The expression can be integer expression or a character expression.

Value-1, 2, n are case labels which are used to identify each case individually. Remember that case
labels should not be same as it may create a problem while executing a program. Case labels always
end with a colon ( : ). Each of these cases is associated with a block.

Whenever the switch is executed, the value of test-expression is compared with all the cases which
we have defined inside the switch. Suppose the test expression contains value 4. This value is
compared with all the cases until case whose label four is found in the program. As soon as a case is
found the block of statements associated with that particular case is executed and control goes out
of the switch.

The break keyword in each case indicates the end of a particular case. The default case is an optional
one. Whenever the value of test-expression is not matched with any of the cases inside the switch,
then the default will be executed. Otherwise, it is not necessary to write default in the switch.Once
the switch is executed the control will go to the statement-x, and the execution of a program will
continue.
Program to find the given number is even or odd

#include<stdio.h>

#include<conio.h>

void main()

int num, rem;

printf("Enter positive integer: ");

scanf("%d", &num);

rem = num%2;

switch(rem)

case 1: printf("%d is ODD.", num);

break;

default: printf("%d is EVEN.", num);

break;

getch(); }
Program to print week days

#include<stdio.h>

#include<conio.h>

void main()

int day;

printf("Enter day number: ");

scanf("%d", &day);

switch(day)

case 1: printf("SUNDAY.");

break;

case 2: printf("MONDAY.");

break;

case 3: printf("TUESDAY.");

break;

case 4: printf("WEDNESDAY.");

break;

case 5: printf("THURSDAY.");

break;

case 6: printf("FRIDAY.");

break;

case 7: printf("SATURDAY.");

break;

default: printf("INVALID DAY.");

break;

getch();

}
program to implement simple calculator using switch statement

#include<stdio.h>

#include<conio.h>

void main()

float a,b, r;

char op;

printf("Available Operations.\n");

printf("+ for Addition.\n");

printf("- for Subtraction.\n");

printf("* for Multiplication.\n");

printf("/ for Division.\n");

printf("Which Operation?\n");

scanf("%c", &op);

switch(op)

case '+':

printf("Enter two numbers:\n");

scanf("%f%f",&a, &b);

r = a+b;

printf("%f + %f = %f", a, b, r);

break;

case '-':

printf("Enter two numbers:\n");

scanf("%f%f",&a, &b);

r = a-b;

printf("%f - %f = %f", a, b, r);

break;

case '*':

printf("Enter two numbers:\n");

scanf("%f%f",&a, &b);
r = a*b;

printf("%f * %f = %f", a, b, r);

break;

case '/':

printf("Enter two numbers:\n");

scanf("%f%f",&a, &b);

if(b!=0)

r = a/b;

printf("%f/%f = %f",a,b,r);

else

printf("Division not possible.");

break;

default: printf("Invalid Operation.");

break;

getch();

Looping

Looping Statements in C execute the sequence of statements many times until the stated condition
becomes false. A loop in C consists of two parts, a body of a loop and a control statement. The
control statement is a combination of some conditions that direct the body of the loop to execute
until the specified condition becomes false. The purpose of the C loop is to repeat the same code a
number of times.

Types of Loops in C

Depending upon the position of a control statement in a program, looping statement in C is classified
into two types:

1. Entry controlled loop

In an entry control loop in C, a condition is checked before executing the body of a loop. It is also
called as a pre-checking loop.
2. Exit controlled loop

In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called
as a post-checking loop.

1. No termination condition is specified.

2. The specified conditions never meet.

The specified condition determines whether to execute the loop body or not.

‘C’ programming language provides us with three types of loop constructs:

1. The while loop

2. The do-while loop

3. The for loop

while Loop

A while loop is the most straightforward looping structure. While loop syntax in C programming
language is as follows:

Syntax

while (condition)

statements;

It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the
loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop
is executed then control again goes back at the beginning, and the condition is checked if it is true,
the same process is executed until the condition becomes false. Once the condition becomes false,
the control goes out of the loop.

After exiting the loop, the control goes to the statements which are immediately after the loop. The
body of a loop can contain more than one statement

Program to print 1 to 10

#include<stdio.h>

#include<conio.h>

void main()

int num=1;

while(num<=10)
{

printf("%d\n",num);

num++;

getch();

Sum of n numbers using while

#include <stdio.h>

#include<conio.h>

void main() {

int n, i, sum = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

i = 1;

while (i <= n) {

sum += i;

++i;

printf("Sum = %d", sum);

getch();

Sum of digits of a number

#include<stdio.h>

#include<conio.h>

void main()

int n,sum=0,m;

printf("Enter a number:");

scanf("%d",&n);

while(n>0)

{
m=n%10;

sum=sum+m;

n=n/10;

printf("Sum is=%d",sum);

getch();

Revrese a number

#include<stdio.h>

#include<conio.h>

void main()

int n, reverse=0, rem;

printf("Enter a number: ");

scanf("%d", &n);

while(n!=0)

rem=n%10;

reverse=reverse*10+rem;

n/=10;

printf("Reversed Number: %d",reverse);

getch();

Program to check the given number is palindrome or not

#include<stdio.h>

#include<conio.h>

void main()

int n,r,sum=0,temp;

printf("enter the number=");


scanf("%d",&n);

temp=n;

while(n>0)

r=n%10;

sum=sum*10 + r;

n=n/10;

if(temp==sum)

printf("palindrome number ");

else

printf("not palindrome");

getch();

do-While loop in C

A do…while loop in C is similar to the while loop except that the condition is always executed after
the body of a loop. It is also called an exit-controlled loop.

Syntax

do

statements

} while (expression);

Here, the body is executed only if the condition is true. In some cases, we have to execute a body of
the loop at least once even if the condition is false. This type of operation can be achieved by using a
do-while loop.

In the do-while loop, the body of a loop is always executed at least once. After the body is executed,
then it checks the condition. If the condition is true, then it will again execute the body of a loop
otherwise control is transferred out of the loop.

Program to print a table of number 2

#include<stdio.h>

#include<conio.h>
void main()

int num=1;

do

printf("%d\n",2*num);

num++;

}while(num<=10);

getch();

for loop

A for loop is a more efficient loop structure in ‘C’ programming. The general structure of for loop
syntax in C is as follows:

Syntax

for (initial value; condition; incrementation or decrementation )

statements;

Steps in for loop

1. The initial value of the for loop is performed only once.


2. The condition is a Boolean expression that tests and compares the counter to a fixed value
after each iteration, stopping the for loop when false is returned.
3. The incrementation/decrementation increases (or decreases) the counter by a set value.

Program to print numbers from 1-10

#include<stdio.h>

#include<conio.h>

void main()

int number;

for(number=1;number<=10;number++)

{
printf("%d\n",number);

getch();

Program to print multiplication table

#include <stdio.h>

#include<conio.h>

void main() {

int i, j;

int table = 2;

int max = 5;

for (i = 1; i <= table; i++)

for (j = 0; j <= max; j++)

printf("%d x %d = %d\n", i, j, i*j);

printf("\n");

getch(); }

Sum of n mumbers using for loop

#include <stdio.h>

#include<conio.h>

void main()

int n, i, sum = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

for (i = 1; i <= n; ++i) {

sum += i;
}

printf("Sum = %d", sum);

getch();

Factorial of a number

#include <stdio.h>

#include<conio.h>

void main()

int i,fact=1,number;

printf("Enter a number: ");

scanf("%d",&number);

for(i=1;i<=number;i++)

fact=fact*i;

printf("Factorial of %d is: %d",number,fact);

getch();

Fibonacci series

#include<stdio.h>

#include<conio.h>

void main()

int n1=0,n2=1,n3,i,number;

printf("Enter the number of elements:");

scanf("%d",&number);

printf("\n%d %d",n1,n2);

for(i=2;i<number;++i)

n3=n1+n2;
printf(" %d",n3);

n1=n2;

n2=n3;

getch();

Nested Loops in C

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of
statements inside another loop. Let's observe an example of nesting loops in C.

Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any
number of loops. The nesting level can be defined at n times.

Syntax of Nested loop

Outer_loop

Inner_loop

// inner loop statements.

// outer loop statements.

Nested for loop

The nested for loop means any type of loop which is defined inside the 'for' loop.

for (initialization; condition; update)

for(initialization; condition; update)

// inner loop statements.

// outer loop statements.


}

Example of nested for loop

#include <stdio.h>

int main()

int n;

printf("Enter the value of n :");

for(int i=1;i<=n;i++)

for(int j=1;j<=10;j++)

printf("%d\t",(i*j)); }

printf("\n");

break, continue and goto statements

The break; continue; and goto; statements are used to alter the normal flow of a program.

Loops perform a set of repetitive task until text expression becomes false but it is sometimes
desirable to skip some statement/s inside loop or terminate the loop immediately without checking
the test expression. In such cases, break and continue statements are used.

break Statement

Break statement is used to break the process of a loop (while, do while and for) and switch case.

Syntax:

break;

Example

while(test Expression)

Statements;

if(condition for break)

break;
}

Statements;

continue Statement

The Continue statement is used to continue the execution of the loop. It is used inside if condition.
We can use it with while, do while and for loop.

Syntax:

continue;

example

#include <stdio.h>

void main(){

int i;

for(i=1;i<=5;i++){

if(i==2)

continue;

printf("%d \n",i);

goto Statement

Goto statement is used to transfer the unconditional program control to a labeled statement.

Syntax:

Goto identifier;

Example:

#include <stdio.h>

void main(){

printf("Hello We are Learning C Language\n");

goto label;
printf("How Are You?"); // skipped

printf("Are You Okey?"); // skipped

label:

printf("Hope you are fine");

You might also like