#9: If Else Statements in C | C Programming for Beginners
Summary
TLDRIn this educational C programming video, Padma from 'Programmies' introduces the concept of if-else statements, crucial for decision-making in code. The tutorial covers syntax, usage, and examples like checking voting eligibility based on age. It progresses to demonstrate else and else-if clauses for multiple conditions, enhancing logic with logical operators. The video concludes with a programming challenge to check if a number is positive, negative, or zero, encouraging viewers to apply their newfound knowledge.
Takeaways
- 📝 The video is a tutorial on 'if else' statements in C programming, focusing on decision-making in code.
- 🔍 It explains the syntax and usage of 'if' statements, which execute code based on a boolean condition being true or false.
- 👤 The tutorial uses a real-world example involving an election where the age of a person determines their eligibility to vote.
- 📈 The script demonstrates how to implement an 'if' statement to check if a person's age is 18 or above to print a message about eligibility.
- 📑 It shows the process of taking user input for the age variable and using 'printf' and 'scanf' statements in C.
- 🔄 The video covers the use of 'if' statements with 'else' clauses to handle scenarios where the condition is not met, like printing a message for ineligible voters.
- 🔧 The script introduces 'else if' clauses for handling multiple conditions, allowing the program to make decisions based on more than two options.
- 🔑 The tutorial provides a practical example of using 'else if' to check for invalid age values like being less than 0 or greater than 120.
- 💡 It offers a tip on simplifying 'if' and 'else' blocks by omitting curly braces when there's only one statement to execute.
- 📚 The video encourages viewers to practice creating test conditions and to review comparison and logical operators for building these conditions.
- 🎯 The script concludes with a programming challenge for viewers to create a program that checks if a number is positive, negative, or zero.
Q & A
What is the purpose of the 'if else' statement in C programming?
-The 'if else' statement in C programming is used for decision making, allowing a program to perform different sets of tasks based on whether a condition is true or false.
What is the basic syntax of an 'if' statement in C?
-The basic syntax of an 'if' statement in C is 'if (test condition)' followed by the body of the statement. The test condition is a boolean expression that evaluates to either true or false.
What happens if the test condition in an 'if' statement is true?
-If the test condition in an 'if' statement is true, the body of the 'if' statement is executed. If it is false, the body is skipped.
Can you provide an example of how to use an 'if' statement to check a person's eligibility to vote based on age?
-Yes, you can create a variable to store the person's age, ask the user to input their age using 'printf' and 'scanf', and then use an 'if' condition to check if the age is greater than or equal to 18. If true, print 'You are eligible to vote'.
What is the purpose of an 'else' clause in an 'if' statement?
-The 'else' clause in an 'if' statement is used to execute a different block of code when the test condition of the 'if' statement is false.
How can you modify the voting eligibility program to also handle cases where the age is less than 18?
-You can add an 'else' clause to the program that prints 'Sorry, you are not eligible to vote' when the age is less than 18.
What is the syntax for an 'if' statement with an 'else if' clause?
-The syntax for an 'if' statement with an 'else if' clause is 'if (test condition 1) { ... } else if (test condition 2) { ... }'. The program checks each condition sequentially and executes the block of code corresponding to the first true condition.
Why might you use 'else if' clauses instead of multiple 'if' statements?
-Using 'else if' clauses instead of multiple 'if' statements can make the code more efficient by avoiding repetitive checks of the same conditions and ensuring that only one block of code is executed.
Can you provide an example of using 'else if' clauses to handle multiple conditions in a program?
-Yes, in the voting eligibility program, you can use 'else if' clauses to check if the age is greater than 120 or less than 0, and print 'Invalid age' for both conditions.
What is the advantage of using logical operators in 'if else' statements?
-Using logical operators in 'if else' statements allows you to combine multiple conditions into a single test condition, making the code more concise and easier to read.
What is the final programming challenge presented in the video script?
-The final programming challenge is to create a program that checks whether a number is positive, negative, or zero using an 'if' statement based on user input.
How can you simplify an 'if else' block if it contains only one statement?
-If an 'if else' block contains only one statement, you can omit the curly braces, making the code less cluttered and easier to read.
Where can viewers find the answers to the programming challenge and additional resources for learning C programming?
-Viewers can find the answers to the programming challenge and additional resources on the GitHub repository mentioned in the video script, as well as on the website programmings.com.
Outlines
📝 Introduction to If-Else Statements in C Programming
This paragraph introduces the concept of decision making in C programming using if-else statements. It explains the syntax and usage of the if statement, which involves a boolean expression that evaluates to true or false. If the condition is true, the code block within the if statement is executed; otherwise, it is skipped. The paragraph provides an example of an election scenario where the program checks if a person's age is 18 or more to determine eligibility to vote. The example includes user input through printf and scanf functions and demonstrates how the if statement is used to print a message based on the age condition. It also touches on the limitations of using multiple if statements and hints at the use of else to handle conditions when the initial condition is not met.
🔁 Enhancing Decision Making with Else and Else If Clauses
This paragraph delves deeper into the if-else statement by introducing the else clause, which provides an alternative set of instructions to execute when the if condition is false. It explains the syntax and logic of using else in the context of the voting eligibility example, where a message is displayed if a person is not eligible to vote due to being under 18. The paragraph then extends the discussion to handle more complex decision-making scenarios with the else if clause, allowing for multiple conditions to be checked sequentially. It demonstrates how to modify the code to include additional conditions, such as invalid ages being greater than 120 or less than 0, and how to use logical operators to combine conditions for efficiency. The summary emphasizes the importance of understanding the logic behind test conditions and encourages practice to improve skill in creating them.
🔧 Practical Application and Simplification of If-Else Blocks
The final paragraph focuses on the practical application of if-else statements, including a tip on simplifying the syntax when the if-else block contains only a single statement, allowing the omission of curly braces. It also presents a programming challenge for the viewer to create a program that checks if a number is positive, negative, or zero, using an if statement based on user input. The paragraph encourages viewers to check the provided resources, such as the GitHub repository and the website programmings.com, for answers and further learning on nested if-else statements. It concludes with an interactive element, inviting viewers to comment their answers to a code output question and expresses well-wishes for continued learning and programming.
Mindmap
Keywords
💡if statement
💡else clause
💡boolean expression
💡decision making
💡scanf statement
💡printf statement
💡logical operator
💡else if clause
💡programming challenge
💡nested if else statement
Highlights
Introduction to if else statement in C programming for decision making.
Syntax of the if statement explained with an example of an election scenario.
Demonstration of a basic C program to check if a person is eligible to vote based on age.
Explanation of how the if statement executes based on the boolean expression result.
Running the program with different age inputs to show the if statement in action.
Improving the program by adding an else statement to handle cases where the age is less than 18.
Use of else clause to print a message when a person is not eligible to vote.
Introduction of the else if clause for handling multiple decision-making paths.
Syntax and functionality of the else if clause explained with a voting eligibility example.
Modification of the program to include additional conditions for invalid ages.
Combining conditions using logical operators to simplify the if else structure.
Running the modified program to demonstrate the new conditions in action.
Tip on omitting curly braces for single-statement if else blocks.
Emphasis on the importance of practice in creating effective test conditions.
Programming challenge presented to check if a number is positive, negative, or zero.
Invitation to find answers and further resources on GitHub and the Programmings website.
Teaser for the next video and a call to action for engagement and support.
Programming squeeze segment inviting viewers to comment their answers to a code output question.
Transcripts
hey guys this is me padma from
programmies and welcome back to this
series on c programming in this video
we'll learn about if else statement in c
programming we'll learn to create
decision making programs that perform
one set of tags under a certain
condition and another set of tags under
different condition so let's get started
in c programming we use the if statement
to create programs that can make
decision let's start by looking at the
syntax of the if statement the if
statement starts with if keyword and is
followed by the test condition inside
the parenthesis
this test condition is a boolean
expression that results in either true
or false
if the test condition is true the body
of if statement is executed otherwise it
is skipped from the execution
now that you know the syntax of if
statement let's try a working example
suppose there is an election going on
and to cast the vote your a's must be 18
or more if your age is 18 or more we'll
print you are eligible to vote let's see
how we can implement this here i have a
basic c program now i'll create a
variable to store is
int is
now i'll ask the user to input there is
for that i'll use printf statement
and print the message like
enter
here is
then i'll use scanf statement to take
the input
so percent d
comma
ampersand is
then i'll use if condition to check if
a's is greater than or equals to 18.
if this condition is true i'll print
you are
eligible
to vote
now let me run this program
here i'll provide 31 as the is and i'll
press enter as you can see
you are eligible to vote is printed
here the value is 31 so our test
condition is greater than or equals to
18 is true that is why this print
statement
is executed
now let me run this program again
this time instead of 31 i'll enter 15
this time nothing is printed on the
screen this is because the a's is 15 so
the test condition is is greater than or
equals to 18 is false so the body of the
if statement is skipped
our program is working fine but it is
not printing anything when the a's is
less than 18. we might want to print
something like sorry you are not
eligible to vote when age is less than
18 so i'll add another if statement
so i'll say
if
is is less than 18
i'll print
sorry
you are not
eligible
to vote
now i'll run this code
and i'll enter 15 again
here you can see sorry you are not
eligible to vote is printed this is
because ace is 15 so this condition is
is greater than or equals to 18 is false
therefore the statement
is
skipped however this condition is is
less than 18 is true so this statement
is executed
by the way if you are watching this
there is a good chance you want to
improve your skill in c programming
lucky for you we have a mobile app that
provides a well-structured c programming
course with certification at the end and
you can use the app alongside the video
to practice on the built-in compiler our
course is free so download now by
scanning this qr code or click the link
in the video description
i have this code from the last segment
i'll remove this code to get the user
input
and i'll manually assign the value of is
equals to 15
so that it's easier to focus on the
logic of the if statement in this
program i have used two if statement to
perform two different tags
we know that the person is eligible to
vote only if the a's is greater than or
equals to 18 and if the condition is not
met we know that the person is not
eligible to vote
in such cases instead of writing the
second if statement with the condition
we can use else clause
let's first look at the syntax of the if
statement with the else clause
on your screen you can see how it looks
so what happens here is if our test
condition is true
statement inside the body of if
statement is executed and if it is false
statements inside the body of else are
executed
now getting back to our code to check if
the person is eligible to vote or not
in this program i'll use the else clause
instead of this second if statement so
i'll remove the second if statement and
replace it with
else
now let me read this code in plain
english as is greater than or equals to
18 then print you are eligible to vote
else print sorry you are not eligible to
vote now when i run this code
you can see sorry you are not eligible
to vote is printed
this is because ace is 15 which is not
greater than or equals to 18
so we get the output sorry you are not
eligible to vote
okay guys we need your support to keep
this type of content free for all user
youtube really likes engagement on the
video so leave a comment below press
that like button hit subscribe if you
haven't already let's get the engagement
score high up so that more people can
discover and enjoy these courses
the if statement with else clause allow
us to make a choice from two different
options however sometimes we need to
make choice from more than two options
in those case we can use elsif's clause
with test condition
let's see the syntax first
the if statement checks the first
condition inside the if statement if it
is true the body of if is executed and
the statement 2
and the statement 3 are skipped however
if the first test condition is false the
control of the program jumps to the
second test condition
if this condition is true the body of
the else if statement is executed and
other statement are skipped if both the
test condition 1 and the test condition
2 are false
then only the else body is executed
if necessary we can add as many else if
clauses as we want for our program to
work and among all those alternatives
only a single block of code is executed
now we know how the else if clause works
let's get back to our code to check
whether a person can vote or not
here the is variable stores the a's of
the person currently the is of oldest
person is 120 so i want this program to
consider is greater than 120 as invalid
similarly the a's cannot be negative so
i also want to write less than 0 as
invalid
now i want to include this two
additional condition in our program for
that i'll use else if clause now i'll
modify this code
so
if
a's is
greater than 1 and 20
i'll print
invalid is
invalid
is
and
as if
so if a's is
less than 0
that is negative value then will print
is
invalid
is
i'll change this if to elsif
so as you can see i have included one if
condition
two else if condition
and finally one else condition
here i'll change the value from 15 to
130
now i'll run this code
here the a's is greater than 120 so we
get invalid is as our output
now let's change the value of this is
from 130 to minus 4 and let's run this
again
this time we get invalid is again
so this is the condition of else if here
is is less than zero so the output we
got is invalid is
now we can add as many else if condition
as we want here we can see these two
condition are performing the same tags
printing the invalid is
in this case we can combine both the
condition together using logical
operator
here the a's is invalid if it is either
greater than 120 or less than 0 or let
me modify this code so i'll cut this
i'll use logical r and put here
is
less than 0. so i'll change this value
from -4 to -1 and when i run this code
i get invalid is as output
now as long as the a's is an integer
number our program works correctly and
the extra condition make sure the a's is
neither greater than 120 or less than 0
and for any other value of a's the
program runs as intended if we provide
the is equals to 50 so let's do that
i'll provide the value of a is equals to
50 i'll run this
now this person is eligible to vote
before we end this video one quick tip
for you if the body if else block has
only one statement like this then we can
omit the curly braces of the if block
like this
as you can see the syntax of the if
statement is pretty simple the harder
part is the logic behind the test
conditions so you will get better at
creating test condition with practice
also be sure to check our video on the
comparison and logical operators that
are used to create the test condition
the link will be in the video
description below
we have covered a lot in this video it's
time for you to practice what we have
learned here is one programming
challenge for you to solve can you
create a program to check whether a
number is positive negative or zero to
create this program create a variable
named number and assign a double value
to it based on the user input then using
an if statement check if the number
variable is positive negative or zero
if the number is positive print the
number is positive if the number is
negative print the number is negative
and if the number is 0 print the number
is 0. you'll find all the answers to
this question in our github repository
and also if you want to revise the
concept we'll learn today you can find
all programs in the repository since the
video is already long we are not
covering the nested if else statement if
you are interested you can learn about
them from our website programmings.com
i'll put the link in the video
description
now that we are at the end of this video
it's time for programming squeeze
what is the output of following code
comment your answer below and see you on
the next video happy programming
[Music]
تصفح المزيد من مقاطع الفيديو ذات الصلة
W3Schools | C# Full Course | W3Schools C# | C# Tutorial - Full Course for Beginners | C# Tutorial
Svelte Tutorial - 9 - Conditional Rendering
Karel Python - if/else
Percabangan If dan Switch Case
If/Elif/Else Statement Chain | Godot GDScript Tutorial | Ep 06
03 - Conditional A - Python for Everybody Course
5.0 / 5 (0 votes)