0% found this document useful (0 votes)
38 views47 pages

Decision Making

The document discusses various conditional statements in C programming such as if, if-else, else-if, and nested if statements. It provides the syntax and flowcharts to illustrate how each statement works. Key examples are provided to demonstrate how to use each conditional statement to execute code blocks based on different test conditions being true or false. The break statement is also covered, which can be used to exit a loop early if a certain condition is met before completing all iterations.

Uploaded by

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

Decision Making

The document discusses various conditional statements in C programming such as if, if-else, else-if, and nested if statements. It provides the syntax and flowcharts to illustrate how each statement works. Key examples are provided to demonstrate how to use each conditional statement to execute code blocks based on different test conditions being true or false. The break statement is also covered, which can be used to exit a loop early if a certain condition is met before completing all iterations.

Uploaded by

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

Decision Making / Conditional

Statements
Introduction
• Usually, the compiler executes C programs line by line in the order
that we write in our program.
• Apart from this, we might get into a situation where we have to
execute some statements repeatedly.
• For this, you can use the C Programming loops.
• But there are some situations where we have to run the statements
based on the condition.
• For that, we can use the conditional/decision making statements.
• Some common condition statements are; if, if- else, else if, nested if,
….
If Statement
• If statement in C programming is one of the most useful decision-
making statements in real-time programming.
• C If statement allows the compiler to test the condition first, and then,
depending upon the result, it will execute the statements.
• If the test condition is true, then only statements within the if
statement will be performed/executed by the C compiler.
If Statement syntax
/ /If statement Syntax in C
if (test condition)
{
Statement 1;
Statement 2;
Statement 3;
………….
………….
Statement n;
}
• From the above code, If the test condition in the If statement is true,
then the statements (Statement 1, Statement 2, Statement 3, …….,
Statement n) will be executed.
• Otherwise, all these statements will skip.
Example
/* If Statement in C Programming Example */
#include <stdio.h>
int main()
{
int number;
printf("Enter any integer Value\n");
scanf("%d",&number);
if( number >= 1 )
{
printf("You Have Entered Positive Integer\n");
}
return 0;
}
If Statement Flow Chart
• If the test condition is true, STATEMENT 1 executed followed by
STATEMENT N.
• If the condition is False, STATEMENT N execute because it is out of
the if statement block.
Example
• /* If Statement in C Programming Example */
#include <stdio.h>
int main(){
int number;
printf("Enter any integer Value\n");
scanf("%d",&number);
if( number >= 1 ){
printf("You Have Entered Positive Integer\n");}
printf("This Message is not coming from IF STATEMENT\n");
return 0;
}
IF ELSE Statement in C
• The If Else statement in C Programming is an extension to the If
statement.
• We already saw the If statement, and it will only execute the
statements when the given condition is true.
• And if the condition is false, it will not execute statements.
• In the real-world, it would be nice to execute something when the
condition fails.
• To do so, If else statement used. Here, Else statement will execute the
statements when the condition fails.
If Else statement Syntax
if (Test condition)
{
//If the condition is TRUE then these statements will be executed
True statements;
}
else
{
//If the condition is FALSE then these statements will be executed
False statements;
}
• If the test condition present in the above structure is true, the True
statements will execute.
• When the condition is false, False statements will execute.
The flow chart of the If Else Statement
Example in If Else Statement
• This program, is going to use 4 different printf statements.
• If the condition is true, it will print 2 separate statements.

• If the condition is false, Program will print another 2 statements.


• A user will be requested to enter a student score mark, and if the
marks is greater than or equal to 50 then s1, s2 will print.
• If the marks is less than 50 then s3 and s4 will print as an output.
Else If Statement
• The Else If Statement in C is usefully while we have to test several
conditions.
• Apart from Else If Statement in C, we can utilize the Nested If
statement to accomplish the same.
• However, as the total number of conditions rises, the code
complexity will further grow.
• Else If statement in C effectively handles multiple statements by
sequentially executing them.
• If the condition result is TRUE, then the statements present in that
block will run.
• If the result is FALSE, verifies the Next one (Else If condition) and so
on.
Else If Statement
if (condition 1)
statements 1
else if (condition 2)
statements 2
else if (condition 3)
statements 3
else if (condition n)
statements n
else
default statements
• There are some circumstances where both the condition1, condition2
are TRUE, for instance:
If we say that x= 20, y=10
Condition 1: x > y //TRUE
Condition 2: x != y //TRUE
• In these circumstances, code under the Condition1 will execute.
• Because ELSE IF conditions will only execute if it’s prior IF or ELSE IF
statement fails.
Else If Statement in C Flow chart
Else If Statement in C Example
• Program
• In this C else if program, the user is asked to enter their total six subject
marks.
• Using this Else if statement, we will decide whether the person is qualified
for scholarship or not.
Note that,
• If a total score marks >=540, a student qualify for full scholarship,
• If a total score marks >=480, a student qualify for 50% scholarship
• If a total score marks >=400, a student qualify for 10% scholarship
Class Work (10 mns)
• Write a C program that will allow a user to enter an average marks
scored in seven subjects seated for O’level examination. Basing to
the selection criteria, If a student scored an average marks
between;
100 and 81 inclusive, a student should get grade A,
80 and 61 inclusive, a student should get grade B,
60 and 41 inclusive, a student should get grade C,
40 and 21 inclusive, a student should get grade D, other wise the
grade scored will be F.
Note that if a student got A will be given a 100% scholarship for
further studies, B will get 70% scholarship, C will get 40%
scholarship and if the grade scored is D a student qualify for 10%
scholarship.
Nested If in C Language
• Nested If in C Programming is placing If Statement inside another IF
Statement.
• Nested If in C is helpful if you want to check the condition inside a
condition.
• If Else Statement prints different statements based on the expression
result (TRUE, FALSE).
• Sometimes we have to check even further when the condition is
TRUE.
• In these situations, we can use these C Nested IF statements, but be
careful while using it.
Nested If in C Language
• For example, every person is eligible to work if he is 18 years old or
above else he is not eligible.
• However, companies will not give a job to every person.
• So, we use another IF Statement, also called as Nested If Statement in
C, to check his education qualifications or any specific company
requirements.
Nested If Syntax
if ( test condition 1)
{
//If the test condition 1 is TRUE then these it will check for test condition 2
if ( test condition 2)
{
//If the test condition 2 is TRUE, these statements execute
Test condition 2 True statements;
}
else
{
//If the c test condition 2 is FALSE, then these statements execute
Test condition 2 False statements;
}
else
{
//If the test condition 1 is FALSE then these statements will be executed
Test condition 1 False statements;
}
Flow Chart for Nested if
Nested If
• If the Test Condition1 is FALSE, STATEMENT3 will execute.
• When Test Condition1 is TRUE, then C Programming will check for the
Test Condition2.
• If it is TRUE, STATEMENT1 will execute else STATEMENT2.
Example
• In this Nested If program, User can enter his age, and we are going to
store it in the variable age.
• If the age is less than 18, we are going to print two statements.
• If the condition fails, we will check one more condition (Nested If),
and if it succeeds, we print something.
• When the nested If the condition fails, we print some other thing.
• In our case, If the age of a person is less than 18, he/she is not eligible
to work.
• If the age is greater than or equal to 18, then the first condition fails,
it will check the else statement.
• Note that;
• In the Else statement, there is another if condition called Nested If in
C.
• In this example, the Nested IF Statement checks the person’s age if is
greater than or equal to 18 and less than or equal to 60.
• When the condition is TRUE, then he can apply for the job.
• If the condition is FALSE, then the statement – he/she is too old to
work as per the government.
Break Statement in C
• The Break statement in C Programming is very useful to exit from any
loop such as For Loop, While Loop, and Do While.
• While executing these loops, if the C compiler finds the break
statement inside them, then the loop will stop running the
statements and immediately exit from the loop.
• The C Break statement and Continue Statement statements are two
important keywords used to alter the flow of a program in any
programming language.
• Loops are useful to execute a particular block of statements for n
number of times until the test condition is false.
• There will be some situations where we have to terminate the loop
without executing all the statements.
• In these situations, we can use Break statement and Continue
statements in C programming.
• For example, we have five statements inside the loop, and we want
to exit from the loop when a certain condition is True; otherwise, it
has to execute them.
• In these situations, we can place the Break statement inside the If
condition.
• If the condition is True, the compiler will execute the break
statement
• It means the break statement will exit the controller from the loop
completely.
• Otherwise, it will run all the statements.
• Syntax: break;
Break Statement- Example
• Within this Break Statement example, We initialized the value of i as: i = 0
at the beginning of the code.
• Within the While loop, we check for the condition whether i is less than or
equal to 10 or not.
• Inside the While loop, we placed if statement to test whether i is equal to
4.
• If the condition is false, it will skip the Break statement. Next, it prints that
number as output (In Our case 0, 1, 2, 3).
• If this condition is True, the Break statement will execute.
• It means the iteration will stop at that number without printing the other
printf statement.
Continue Statement
• The Continue statement in C Programming is another one that
controls the flow of loops.
• This C Continue statement used inside For Loop, While Loop and Do
While Loops.
• While executing these loops, if the compiler finds the continue
statement inside them, then the loop will stop the current iteration
and starts the new iteration from the beginning.
• For example, we have 10 statements inside the loop.
• And we want to skip executing the second 5 statements (statement 6
—statement 10) when a certain condition is True or else it has to
execute all the 10 statements inside the loop.
• In these situations, we place the condition after the 5th statement,
followed by this C continue statement.
• If the condition is True, then it will stop executing statements 6 to 10.
Otherwise, it will execute statements 1 to 10.
• The syntax of the Continue Statement in C Programming is
continue;
• /* Continue Statement in C Programming example */
#include <stdio.h>
int main()
{
int i, number;
printf("\n Please Enter any integer\n");
scanf("%d", &number);
for(i=1;i<= number; i++)
{
if(i%2 != 0)
{
printf("\n Odd Numbers = %d(Skipped By Continue)\n",i);
continue;
}
printf("\n Even numbers = %d\n",i);
}
}
• Here we have for loop within the continue statement.
• Inside the for loop, we placed If Statement to test whether (i%2 != 0).
• If this condition is True, the continue statement will execute, and the
iteration will stop at that number without printing the other printf
statement: printf(“\n Even numbers = %d\n”, i).
• If the condition is false, it skips the continue statement and prints
that number as output (In Our case Even Number)
Continue Statement inside While Loop
Example
• The continue Statement inside while loop is the same as the we have
seen continue statement inside the for loop. However here, we
replace the for loop with a while loop.
/* Continue Statement in C Programming example */
#include<stdio.h>
int main()
{
int i=0;
while (i<= 10)
{
if (i== 5 || i == 9)
{
printf("Skipped Values = %d\n", i);
i++;
continue;
}
printf("Values = %d\n", i);
i++;
}
return 0;
}
• Inside the While loop, we placed If Statement to test whether i is
equal to 5 or 9.
• If this condition is True, the continue statement will run, and the
iteration will stop at that number without printing the other printf
statement: printf(“Values are: %d “, i);.
• If the condition is false, then it will skip the continue statement and
prints that number as output (In Our case 0,1,2,3,4,6,7,8,10)
Goto Statement
• The goto statement in C Programming is useful to alter the flow of a
program.
• When the compiler reaches the goto statement, then it will jump
unconditionally ( both forward and backward ) to the location
specified in the goto statement (we called it as a label).
• Unlike the Break and Continue Statement, the goto statement in C
doesn’t require any If Statements to perform.
Goto Statement Syntax
• The label specified after the goto statement in C Programming is the
location where we place the statements to execute.
• From the below syntax, you can note that we can place the label
anywhere in the program.
• It doesn’t matter if you put before the goto statement or after.
Example
• This program for c goto allows the user to enter his/her individual
subject marks.
• Next, it will check whether the person is pass or fail using Goto
Statement.
• Goto Statement program example
• If the condition is TRUE, C Goto statement inside the If block will take the
compiler to the Pass label and executed the statements inside the Pass label
• Else (If the condition is FALSE), then Goto statement inside the Else block will
take the compiler to the Fail label.
• Next, it executes the statements inside the Fail label.
Switch Case in C
• If Else in C allows us to choose between TRUE or FALSE, when there
are more than two options, we use Nested If.
• Say, What if we have ten alternatives to choose?, if we use Nested If,
in this situation, the programming logic will be difficult to understand.
• In C Programming, Else if statement and Switch statements can
handle these types of problems effectively.
• The working functionality of the switch case in C is almost the same
as if condition.
• The Switch statement may have n number of cases. So, the switch
case compares the expression value with the values assigned in the
case statements.
• If both the values expression value and case value match, then
statements present in that case statement will execute.
Syntax of Switch
• The value of expression should be either integer or characters (We
can write the expression as n/2…. also, but the result should be an
integer).
• Option 1, Option 2…… are constants.
• C Switch case allows us to add a default statement.
• If the variable value is not matching with any of the case statements,
the code present in the default will execute.
• A break is useful to come out from the switch statement.
• Otherwise, all the statements in the C switch condition will execute.
• Whenever a Break Statement encountered, the execution flow would
directly come out of the switch.
Flow Chart for Switch Case
Switch Case Example
• This program allows the user to enter two integer values.
• Next, it also allows them to select any Arithmetic Operator to
perform arithmetic operations using the Switch Case.
• The case statement operate inside the single quotation (‘ ’).

You might also like