Copy of Session 4 - Anybody can code_20231024_174623_0000
Copy of Session 4 - Anybody can code_20231024_174623_0000
PROGRAMMING IN
SESSION 5
ANYBODY CAN
OUTLINE
Introduction to if-else
Logical Conditions used in if-else
Switch Statement
ANYBODY CAN
C<>DE
ANALOGY OF IF-ELSE
C<>DE
IF-ELSE STATEMENT
if (condition)
{
//true statement
}
else
{
//false statement
}
SYNTAX FOR IF-ELSE
if (condition)
checking condition . if the condition
{ is true it’ll print ‘True’.
printf(”This is true”);
}
if the condition is not true, it
else goes to the else part
{
printf(”This is false”); then it’ll print ‘False’.
}
Short Hand If Else (Ternary Operator) ?:
Instead of writing:
}
else
{
5<4 ,so it’ll print False.
printf(”False”);
}
PRACTICE QUESTION
Test Data: 15 15
Expected Output :
Enter two Numbers: 15 15
Number1 and Number2 are equal.
PRACTICE QUESTION
Test Data : 21
Expected Output :
Enter Your Age: 21
Congratulation! You are eligible to cast your vote.
PRACTICE QUESTION
Test Data: 15
Expected Output :
Enter a Number: 15
15 is a positive number
PRACTICE QUESTION
Test Data: 15
Expected Output :
Enter a Number: 15
15 is an odd number
DECISION MAKING AND
BRANCHING IN C
if Statement
if-else Statement
switch Statement
Nested if
else-if Ladder
break Statement
continue Statement
What is Flow Charts?
if(condition)
{
//Body of if;
}
flow chart of simple if
Using an ‘if’ a computer first evaluate the (condition)
and if it is true, the body of if is executed which means
the flow of control passes to the if’s body, or
if the test condition is false then the flow of control
passes to the immediate step next to if body.
Example of if
#include <stdio.h>
int main()
Output:
{ int num;
printf("Enter a Number:"); Enter a Number: -7
scanf("%d",&num); if Block passed
if(num>0){
printf("if block worked\n");
}
printf("if block passed");
return 0;
}
Example of if
#include <stdio.h>
int main()
Output:
{ int num;
printf("Enter a Number:"); Enter a Number: 7
scanf("%d",&num); if block worked
if(num>0){ if Block passed
printf("if block worked\n");
}
printf("if block passed");
return 0;
}
else-if Ladder
An extension of the "if-else" statement that allows you
to check multiple conditions sequentially. When the
initial "if" condition is false, the program evaluates the
next "else if" condition.
Test Data: 23 34 20
Expected Output :
Enter Three Numbers: 23 34 20
34 is the Greatest number
example of Nested if
#inc1ude <stdio.h> Output:
int main()
{ int a,b,c;
Enter the values for A,B,C
printf(" Enter the values for A,B,C:\n” );
10 20 30
scanf("%d %d %d", &a,&b,&c);
if( a > b )
C is the largest
{
if ( a > c){ Enter the values for A,B,C
printf(" A is the largest\n");} 30 10 20
else{ A is the largest
printf("C is the largest\n");
} else if (b> c){
Enter the values for A,B,C
printf(" B is the largest\n");}
20 30 10
else{
printf("C is the largest\n"); B is the largest
}
SWITCH STATEMENT
int main() {
int num = 2;
switch (num) {
case 1:
printf("number is 1");
break;
case 2:
printf("number is 2");
break;
case 3:
printf("number is 3");
break;
default:
printf("number other than 1, 2 and 3");
break;
}
return 0;
}
EXAMPLE FOR SWITCH STATEMENT
#include<stdio.h>
int main()
{ int dayno;
printf(" Enter Day number \n");
scanf("%d",&dayno);
switch(dayno)
{ 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
number");
}
}
example
Jump Statements
break
continue
goto.
break Statement
switch (day) {
case 6:
printf("Today is Saturday");
break;
case 7:
printf("Today is Sunday");
break;
default:
printf("Looking forward to the Weekend");
}
Output:
Looking forward to the Weekend"
Continue Statement