CFP - Unit 3
CFP - Unit 3
UNIT 3
Syntax
if(condition1)
{
/* code to be executed if condition1 is true */
if (condition2)
{
/* code to be executed if condition2 is true */
}
else
{
/* code to be executed if condition2 is false */
}
}
else
{
/* code to be executed if condition1 is false */
}
/* A quick demo of nested if-else */
void main( )
{
int i;
printf ( "Enter either 1 or 2 " ) ;
scanf( "%d", &i) ;
if ( i== 1 )
printf ( "You Entered One !" ) ;
else
{
if ( i== 2 )
printf ( "You Entered Two !!" ) ;
else
printf ( "You Entered Other than One and
Two !" ) ;
}
Continue…..
#include <stdio.h>
void main()
{
int num1, num2, num3;
printf("Enter the values of num1, num2 and
num3\n");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2)
{
if (num1 > num3)
printf("%d is the largest number.", num1);
else
printf("%d is the largest number.", num3);
}
else
{
if (num2 > num3)
printf("%d is the largest number.", num2);
else
printf("%d is the largest number.", num3);
Cascaded If-else(If - else
Ladder)
Allows to check between Flow chart
multiple conditions and
execute different statemants.
SYNTAX
if(condition1)
{
//statement1
}
else if(condition2)
{
//statement2
}
else if(condition3)
{
//statement3
}
else
{
//statement4
}
simple calculator
// C Program to make a Simple Calculatorelse if(ch==2)
using
#include <stdio.h> {
sub = number1 - number2; // Calculate
int main() { substraction
int number1, number2, ch,sum,sub,mul,div,rem;
printf("Substraction: %d", sub);
printf("Enter number1: "); }
scanf("%d", &number1); else if(ch==3)
{
printf("Enter number2: ");
mul = number1 * number2; //
scanf("%d", &number2); Calculate multiplication
printf("Enterchoice:: "); printf("Multiplication: %d", mul);
scanf("%d", &ch); }
if(ch==1) else if(ch==4)
{
{ div = number1/ number2; // Calculate
sum = number1 + number2; // Calculate sum
division
printf("Addition: %d", sum); printf("Division: %d", div);
} }
Example Cascaded if -else
#include<stdio.h>
int main( )
{
int m, ;
printf ( "Enter marksof students " ) ;
scanf ( "%d ", &m ) ;
if ( m <= 100 && m>=90 )
printf ( "GREAD is= A ") ;
else if( m <90 && m>=80 )
printf ( "GREAD is =B ") ;
else if( m <80 && m>=70 )
printf ( "GREAD is =C ") ;
else if( m <70 && m>=60 )
printf ( "GREAD is =D") ;
else if( m <60 && m>=50 )
printf ( "GREAD is =E ") ;
else if( m <50 )
printf ( "GREAD is =FAIL ") ;
else
printf ( "Enter Valid Scor between 0 to 100 ") ;
return (0);
}
Decisions Using switch
The control statement that allows us to make a
decision from the number of choices is called a
switch, or more correctly a switch-case-default, since
these three keywords go together to make up the
control statement. They most often appear as
follows:
switch ( integer expression )
{
case constant 1 : do this ;
case constant 2 : do this ;
case constant 3 : do this ;
default : do this ;
Continue….
The integer expression following the keyword
switch is any C expression that will yield an integer
value. It could be an integer constant like 1, 2 or 3,
or an expression that evaluates to an integer.
The keyword case is followed by an integer or a
character constant.
Constant in each case must be different from all the
others.
The “do this” lines represent any valid C statement.
Continue…
What happens when we run a program containing a
switch?
First, the integer expression following the keyword switch
is evaluated.
The value it gives is then matched, one by one, against the
constant values that follow the case statements.
When a match is found, the program executes the
statements following that case, and all subsequent case
and default statements as well.
If no match is found with any of the case statements, only
the statements following the default are executed.
void main( )
{ The output:
I am in case 2
int i = 2 ;
I am in case 3
switch ( i )
I am in default
{
case 1 :
printf ( "I am in case 1 \n" ) ;
case 2 :
printf ( "I am in case 2 \n" ) ;
case 3 :
printf ( "I am in case 3 \n" ) ;
default :
printf ( "I am in default \n" ) ;
}
C Program to make a Simple Calculator using
switch-case statements
reached.
Syntax
for(initilization ; Test Expression; Update)
{
working of for loop
Initilization statement executed
only once
If test expression is evaluated false
then loop terminited.
If test expression is evaluated True
then the statements inside body of
for loop executed and update
expression updated.
Again test expression is evaluated.
This process is repeated until test
expression becomes false, when
test expression is false loop
Example: Print number from
1 to 10
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++) // for loop without braces
printf("%d ", i);
printf("\nThis statement executes after for loop end!!!!");
// Statement print only once
return 0;
}
Write a C code to display a table of 3
#include <stdio.h>
int main()
{
int i,num,table;
printf("Enter the number whose table you want to print");
scanf("%d",&num);
for(i = 1; i <= 10; i++)
{
table=num*i;
printf("%dx %d = %d\n",num, i, table);
}
return 0;
}
Write a C code to display a sum of 1 to
10 numbers.
#include <stdio.h>
int main()
{
int i,sum = 0;
for(int i = 1; i <= 10; i++)
{
sum = sum+i; // Add i to sum
}
// Display the sum
printf("The sum of numbers from 1 to10 is: %d\n", sum);
return 0;
}
Write a C code to display the Factorial of
the accepted number.
#include <stdio.h>
int main()
{
int n, factorial = 1;
// Ask user for input
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
factorial = factorial*i; // Multiply factorial by i
printf("Factorial of %d is: %d\n", n, factorial);
return 0;
}
Display Pattern
1
12
123
1234
#include <stdio.h>
int main()
{
// Loop for each row
for(int i = 1; i <= 4; i++)
{
// Loop to print numbers in each row
for(int j = 1; j <= i; j++)
{
printf("%d", j); // Print the number
}
printf("\n"); // Move to the next line after
each row
}
return 0;
}
Display Pattern
****
***
**
*
#include <stdio.h>
int main()
{
// Outer loop for rows
for (int i = 4; i >= 1; i--)
{
// Inner loop for printing '*' in each row
for (int j = 1; j <= i; j++)
{
printf("*");
}
printf("\n"); // Move to the next line after printing stars
}
return 0;
}
While loop
• Evaluates test expression
used when we dont know
in side braces()
exact number of • If the test expression is
iteration/repetation before
true then statements inside
hand.
the body of loop are
The loop terminated on the
executed, test expression is
basic of test condition. evaluated again.
Syntax • The process continue till
while (test expression) test expression is evaluated
{ true.
// body consisting of multiple • If test expression is
statements evaluated false loop
terminated.
Flow chart
//Program to print numbers from 0 to 5
#include <stdio.h>
int main()
{
// Initialization of loop variable
int i = 0;
// setting test expression as (i < 5)
while(i < 5)
{
printf("%d\n",i);
// updating the loop variable
i++;
}
return0;
}
}}};
}
Do-while loop
Use when code need to be executed
atleast once like in MENU driven
program.
Syntax
do
{
// body of do-while loop
}while(test expression);
Example Do-while
program to print table of given
number
int main() int main()
{ {
int i = 0; int i = 1,num=0;
do { printf(“Enter the
number”);
scanf(“%d”,&num);
printf("Sanjivani\n"); do {
i++; printf("%d",num*i);
} while (i < 3); i++;
return 0; } while (i <= 10);
} return 0;
infinite loop
Endless loop:piece of coding that lacks functional exit so
that it repeats indefinetly.
-Occurs when condition always evaluated to true.
-Usually this is an error.
Break statement
include<stdio.h>
void main()
{
int i;
for(i=0;i<10;i++)
{
printf(“%d”, i);
if(i==5)
break;
}
printf(“Came outside of loop i=%d”,i);
}
continue statement
-continue statement skip
current iteration of loop and
continue with next iteration.
-bring program control
begining of loop.
-skip some line of code
inside loop and continue
with next iteration.
Syntax
continue;
C program to demonstrate difference between
continue and break
int main()
{
printf("The loop with break produces output as: \n");
output
for (int i = 1; i <= 7; i++) { The loop with break
// Program comes out of loop when i becomes produces output as: 1 2
multiple of 3.
if (i == 3) The loop with continue
break; produces output as:
else
printf("%d ", i); 124567
}
printf("\nThe loop with continue produces output as: \
n");
for (int i = 1; i <= 7; i++)
// The loop prints all values except those that are
multiple of 3.
if (i == 3)
continue;
printf("%d ", i);
}
return 0;
}
Go to statement
Jump statement in C
Transfer program control to predefined label.
complicated.
Syntax
label:
some part of code;
go to label;
Drawbaks of go to
statement
It makes program logic complicated.
It make task of analyzing & verifying
correcteness of program very difficult.
Its use can be avoided by using break &
continue statements.
use of go to statement
void main()
{
int num, i=1;
printf(“Enter the number whose table you want to print”);
scanf(“%d”,&num);
table:
printf(“%dx%d=%d\n”,num,i,num*i);
i++;
if(i<=10)
go to table;
}
Reference
1. Brian W. Kernighan, Dennis M. Ritchie,
“The C Programming Language”,
Prentice Hall, ISBN 0131103628,
Second Edition
2. Yashwant Kanetkar, “Let Us C”, BPB
Publication, ISBN-10:81-8333-163-7
3. E Balagurusamy, “Programming in ANSI
C” McGraw Hill ,9th edition