FOP Unit 3
FOP Unit 3
UNIT 3
if(expression)
{
statement inside;
}
statement outside;
If statement flow diagram
Example programs of if statement
Example -
Input
Input num1: 10
Input num2: 20
Output
Maximum = 20
Logic –
Number is Odd
If – else statements
if(boolean expression)
{
// Body of if
// If expression is true then execute this
}
else
{
// Body of else
// If expression is false then execute this
}
Flowchart of if...else statement
Write a program to input two numbers from user. Print
maximum between both the given numbers?
#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter two numbers: "); Output of the above program
scanf("%d%d", &num1, &num2);
if(num1 > num2) Enter two numbers: 10 20
{ Second number is maximum.
printf("First number is maximum.");
}
else
{
printf("Second number is maximum.");
}
return 0; }
Q) C program to check whether a number
is divisible by 5 and 11 or not?
• Number is exactly divisible by some other
number if it gives 0 as remainder. To check if a
number is exactly divisible by some number we
need to test if it leaves 0 as remainder or not.
• C supports a modulo operator %, that evaluates
remainder on division of two operands. You can
use this to check if a number is exactly divisible
by some number or not.
• For example - if(8 % 2), if the given expression
evaluates 0, then 8 is exactly divisible by 2.
Step by step logic -
1)Input a number from user. Store it in some variable
say num.
2)To check divisibility with 5, check if(num % 5 ==
0) then num is divisible by 5.
3)To check divisibility with 11, check if(num % 11 ==
0) then num is divisible by 11.
4)Now combine the above two conditions using
logical AND operator &&. To check divisibility with 5 and
11 both, check if((num % 5 == 0) && (num % 11 == 0)),
then number is divisible by both 5 and 11.
#include <stdio.h>
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if((num % 5 == 0) && (num % 11 == 0))
{
printf("Number is divisible by 5 and 11");
}
else
{
printf("Number is not divisible by 5 and 11");
}
return 0;
}
Output -
else if (boolean_expression_2)
{
// If expression 1 is false and
// expression 2 is true then execute
// this and skip other if
}
else if (boolean_expression_n)
{
// If expression 1 is false,
// expression 2 is also false,
// expression n-1 is also false,
// and expression n is true then execute
// this and skip else.
}
else
{
// If no expressions are true then
// execute this skipping all other.
}
Ladder if...else...if statement flowchart
Write a simple C program to input an integer from user. Check if the given
integer is negative, zero or positive?
#include <stdio.h>
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if(num < 0)
{
printf("NUMBER IS NEGATIVE.");
}
else if(num == 0)
{
printf("NUMBER IS ZERO.");
}
else
{
printf("NUMBER IS POSITIVE.");
}
return 0;
}
Output of the above program -
Run 1:
Enter the marks of a student:78
Grade=C
Run 2:
Enter the marks of a student:98
Grade=A
Nested if…else statement in C
Num3 is max
Logic of the program -
• .The first outer if condition if(num1 > num2) is false since 10 > 20 is false.
Hence, outer if statement is skipped, executing the outer else part.
• Inside the outer else, condition if(num2 > num3) is also false, since 20 > 30
is false. Hence, the inner if statement is skipped, executing inner else part.
• Inside the inner else there is nothing much to do. Just a simple printf()
statement, printing "Num3 is max."
Switch case statement in C
switch(week)
{
case 1:
printf("Its Monday.\n");
printf("Its a busy day.");
break;
case 2:
printf("Its Tuesday.");
break;
case 3:
printf("Its Wednesday.");
break;
case 4:
printf("Its Thursday.\n");
printf("Feeling bit relaxed.");
break;
case 5:
printf("Its Friday.");
break;
case 6:
printf("Its Saturday.\n");
printf("It is weekend.");
break;
case 7:
printf("Its Sunday.\n");
printf(" Its holiday.");
break;
default:
printf(" Please enter week number between 1-7.");
}
return 0;
}
#include <stdio.h>
int main()
{
int month;
printf("Enter month number(1-12): ");
scanf("%d", &month);
switch(month)
{
case 1:
printf("31 days");
break;
case 2:
printf("28/29 days");
break;
case 3:
printf("31 days");
break;
case 4:
printf("30 days");
break;
case 5:
printf("31 days");
break;
case 6:
printf("30 days");
break;
case 7:
printf("31 days");
break;
case 8:
printf("31 days");
break;
case 9:
printf("30 days");
break;
case 10:
printf("31 days");
break;
case 11:
printf("30 days");
break;
case 12:
printf("31 days");
break;
default:
printf("Invalid input! Please enter month number between 1-12");
}
return 0;
}
What is loop and why to use loops ?
• In real life we come across situations when we need to perform a set of
task repeatedly till some condition is met.
• For example - sending email to all employees, deleting all files, printing
1000 pages of a document. All of these tasks are performed in loop. To do
such task C supports looping control statements.
• In programming, a loop is used to repeat a block of code until the
specified condition is met.
• C programming has three types of loops:-
1) for loop
2) while loop
3) do...while loop
For loop in C programming
• Syntax of for loop
Output
1 2 3 4 5 6 7 8 9 10
C program to print all Odd numbers from 1 to n?
#include <stdio.h> short hand assignment operator
int main() i+=2 means i = i+2
{
int i, n;
printf("Print odd numbers till: ");
scanf("%d", &n);
printf("All odd numbers from 1 to %d are: \n", n);
while(condition)
{
// Body of while loop
}
Parts of while loop
Syntax of do...while loop –
do
{
// Body of do while loop
} while (condition);
Flowchart of do...while loop
C program to print natural numbers using do while
loop?
#include <stdio.h>
int main()
{
int n=1;
do
{
printf("%d ", n);
n++;
} while(n <= 10);
return 0;
}
OUTPUT -
1 2 3 4 5 6 7 8 9 10
Jump statements in C
Types of jump statements in C language –
1) break statement
2) continue statement
• continue is a jump statement used inside loop. It skips loop body and
continues to next iteration. continue statement on execution immediately
transfers program control from loop body to next part of the loop. It works
opposite of break statement.
continue ;
How to use break statement
You must follow some rules while using break statement.
• You must write break statement inside a loop or switch. Use of break
keyword outside the switch or loop will result in compilation error.
• break statement is generally used with condition(if statements) inside
loop.
• In case of nested loop or switch, break only terminates the innermost loop
or switch.
• Use of break inside switch...case will transfer the program control outside
the switch.
• break statement only terminate program control from nearest matched
loop or switch.
How break statement works inside a switch case –
• You must use continue keyword inside a loop. Use of continue outside
loop will result in compilation error .
• continue is not used with switch...case statement.
• In case of nested loop, continue will continue next iteration of nearest
loop.
• Do not confuse that continue transfer program control to loop condition.
It transfer program control to next part of the loop.
• Use of continue statement without condition is worthless. Hence it is
often used with if statements.
How continue statement works with for loop –