0% found this document useful (0 votes)
27 views56 pages

Copy of Session 4 - Anybody can code_20231024_174623_0000

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views56 pages

Copy of Session 4 - Anybody can code_20231024_174623_0000

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

PROBLEM SOLVING AND

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

THINK OF YOU GOING TO PLAY OUTSIDE


AND YOUR MOM CHECKING THE WEATHER
CONDITION.
ANYBODY CAN

C<>DE
IF-ELSE STATEMENT

a conditional construct that allows you to execute


different blocks of code based on whether a specified
condition is true or false.
If the condition is true, the code within the if block is
executed; otherwise, the code within the else block is
executed.
syntax of if-else

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) ?:

There is also a short-hand if else, which is known as the


ternary operator(?:) because it consists of three operands.
It can be used to replace multiple lines of code with a
single line. It is often used to replace simple if else
statements:
Syntax Short Hand If Else (Ternary Operator) ?:

Instead of writing:

int time = 20;


if (time < 18) {
printf("Good day.");
} else {
printf("Good evening.");
}
Syntax Short Hand If Else (Ternary Operator) ?:

variable = (condition) ? expressionTrue : expressionFalse;


Syntax Short Hand If Else (Ternary Operator) ?:

You can simply write:

int time = 20;


(time < 18) ? printf("Good day.") : printf("Good evening.");

It is completely up to you if you want to use the traditional if...else


statement or the ternary operator.
Logical conditions in if-else
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
example
checking if 5 is less than 4
if (5<4)
{
printf(”True”); 5<4 ,so it’ll not print True.

}
else
{
5<4 ,so it’ll print False.
printf(”False”);
}
PRACTICE QUESTION

1. Write a C program to accept two integers and check whether


they are equal or not.

Test Data: 15 15
Expected Output :
Enter two Numbers: 15 15
Number1 and Number2 are equal.
PRACTICE QUESTION

2.Write a User input C program to read the age of a candidate


and determine whether he is eligible to cast his/her own vote.

Test Data : 21
Expected Output :
Enter Your Age: 21
Congratulation! You are eligible to cast your vote.
PRACTICE QUESTION

3. Write a User Input C program to check whether a given


number is positive or negative.

Test Data: 15
Expected Output :
Enter a Number: 15
15 is a positive number
PRACTICE QUESTION

4. Write a user-input C program to check whether a given


number is even or odd.

Test Data: 15
Expected Output :
Enter a Number: 15
15 is an odd number
DECISION MAKING AND
BRANCHING IN C

The decision-making process in C Programming involves


the use of conditional statements to control the flow of
program execution.
Branching allows the program to execute different sets
of code based on the outcome of certain conditions.
In C Programming, decision making, and branching are
achieved through:

if Statement
if-else Statement
switch Statement
Nested if
else-if Ladder
break Statement
continue Statement
What is Flow Charts?

Graphic representation or Symbolic representation of a


process is called Flow charts.

Different symbols are used to denote the process which


contains a little description of the process. These
symbols are linked together via arrows showing the flow
of the process.
there are lot of variations and
common alternatives names of
the flow charts. Those names
includes.
Flow charts
Process flow charts
Process map
Process chart
Process model
Process flow diagram
Flow diagram.
PRACTICE QUESTION

1. Draw a Flow Chart for User Input Addition of


Two Number
2. Draw a Flow Chart for User Input Checking a
Number is Odd or even.
Simple if statement

‘if’ is the most powerful decision making statement in C


language. Thus it is used to control the execution of
statements. ‘If’ is used along with an expression. It is a
two way branching or decision making statement.
syntax for if

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.

We use else-if Ladder when you have Multiple


Conditions to Check
syntax for else-if ladder
flow chart of else-if ladder
example of else-if ladder
#include <stdio.h>
int main()
{
int marks; Output:
printf(" Enter Marks \n");
scanf("%d",&marks); Enter Marks: 80
if((marks<=100) && (marks >=70)){
printf("Distinction"); }
Distinction
else if(marks >=60){
printf("First Class");}
else if(marks >=50){
printf("Second Class");}
else if (marks >=40){
printf ("Pass Class \n");}
else{
printf("Fails");
}
return 0;
}
if-else statement when you have a single condition to check
(given number is odd or even) It's a basic decision-making
structure.

else-if ladder is used when you have multiple conditions to


check, If none of the conditions is true, you can provide an "else"
block as a fallback.
Nested if
Nested if statements are branching statements that
are included within other if statements.
It allows for more complex branching logic by
checking multiple conditions within other
conditional statements.
The inner if statements are only executed if the
outer if statements evaluate to true.
Syntax of Nested if
if(condition)
{
if(condition)
{
Block of statements;
}
else {
Block of statements;
}
}
else
{
Block of statements;
}
Flow Chart of Nested if
PRACTICE QUESTION

1.Find the Greatest of 3 Numbers using Nested if

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

The switch case statement is an alternative to the if


else if ladder that can be used to execute the
conditional code based on the value of the variable
specified in the switch statement.
The switch block consists of cases to be executed
based on the value of the switch variable.
SWITCH STATEMENT

The switch statement checks for specific conditions


just like if-else statements do, but it can check
multiple possibilities for that condition.

This is helpful when we’re dealing with lots of


different outcomes.
SWITCH STATEMENT
This is how it works:

The switch expression is evaluated once


The value of the expression is compared with the values of
each case
If there is a match, the associated block of code is executed
The break statement breaks out of the switch block and
stops the execution
The default statement is optional, and specifies some code
to run if there is no case match
SYNTAX FOR SWITCH STATEMENT
FLOW CHART FOR SWITCH STATEMENT
EXAMPLE FOR SWITCH STATEMENT
#include <stdio.h>

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

There are control statements that do not


need any condition to control the program
execution flow.

These control statements are


called as unconditional control statements.
Jump Statements

C provides three types of Jump Statements

break
continue
goto.
break Statement

The break statement is an important control


flow statement in C Programming that allows
programmers to control the behavior of loops
and switch statements.
break Statement
The break statement has two applications in C programming:

When a loop reaches a break statement, it is terminated


immediately, and program control is handed to the
statement that follows the loop.
It can be used to end a case by using it to the switch
statement.
break Statement

When C reaches a break keyword, it breaks out of the


switch block.
This will stop the execution of more code and case
testing inside the block.
When a match is found, and the job is done, it's time for
a break. There is no need for more testing.
default Statement

The default keyword specifies some code to run if there


is no case match.
it is similar to else .
The default keyword must be used as the last
statement in the switch, and it does not need a break.
default Statement example
int day = 4;

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

The continue statement is used to move the program


execution control to the beginning of the looping statement.
it is similar to else .
The default keyword must be used as the last statement in
the switch, and it does not need a break.
CONCLUSION

Branching and Decision Making in C


THANK YOU

You might also like