#9: If Else Statements in C | C Programming for Beginners

Programiz
8 Dec 202112:50

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

00:00

📝 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.

05:02

🔁 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.

10:06

🔧 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

The 'if statement' is a fundamental control flow mechanism in programming used to execute a block of code based on a condition. In the context of the video, it is used to determine whether a person is eligible to vote based on their age. The script provides an example where the if statement checks if a person's age is 18 or more to print a message about eligibility.

💡else clause

The 'else clause' is a part of the if statement that specifies a block of code to be executed if the condition in the if statement evaluates to false. In the video, it is used to print a message indicating ineligibility to vote when a person's age is less than 18.

💡boolean expression

A 'boolean expression' is an expression that results in either true or false. In the video, the boolean expression is used within the if statement to test whether the age is greater than or equal to 18, serving as the condition for the if statement.

💡decision making

The term 'decision making' refers to the process of selecting from one or more alternatives based on a set of conditions. The video script discusses how the if else statement in C programming is used for decision making, such as determining voting eligibility.

💡scanf statement

The 'scanf statement' is a standard input function in C used to read data from the standard input (usually the keyboard). In the script, it is used to take the user's input for the age, which is then used in the if statement to make a decision.

💡printf statement

The 'printf statement' is a standard output function in C used to display data to the standard output (usually the screen). The video script uses printf to print messages to the user, such as prompts and results of the if statement evaluation.

💡logical operator

A 'logical operator' is a symbol or keyword that represents a logical operation, such as AND, OR, and NOT. In the video, logical operators are used to combine conditions in the if statement, such as checking if the age is both greater than 120 or less than 0.

💡else if clause

The 'else if clause' is used in if statements to check multiple conditions in sequence. If the initial condition is false, the program checks the next condition in the else if clause. The video script demonstrates using else if to handle multiple age-related conditions for voting eligibility.

💡programming challenge

A 'programming challenge' is a task or problem set to test and improve one's programming skills. The video script ends with a programming challenge for the viewers to create a program that checks if a number is positive, negative, or zero using if statements.

💡nested if else statement

A 'nested if else statement' refers to an if or else if statement that is placed inside another if or else if statement. Although not covered in detail in the video, the script mentions that nested if else statements can be used for more complex decision making processes.

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

play00:00

hey guys this is me padma from

play00:01

programmies and welcome back to this

play00:03

series on c programming in this video

play00:05

we'll learn about if else statement in c

play00:08

programming we'll learn to create

play00:10

decision making programs that perform

play00:12

one set of tags under a certain

play00:15

condition and another set of tags under

play00:17

different condition so let's get started

play00:23

in c programming we use the if statement

play00:26

to create programs that can make

play00:28

decision let's start by looking at the

play00:30

syntax of the if statement the if

play00:33

statement starts with if keyword and is

play00:36

followed by the test condition inside

play00:39

the parenthesis

play00:40

this test condition is a boolean

play00:42

expression that results in either true

play00:45

or false

play00:47

if the test condition is true the body

play00:50

of if statement is executed otherwise it

play00:54

is skipped from the execution

play00:57

now that you know the syntax of if

play00:59

statement let's try a working example

play01:02

suppose there is an election going on

play01:04

and to cast the vote your a's must be 18

play01:07

or more if your age is 18 or more we'll

play01:10

print you are eligible to vote let's see

play01:13

how we can implement this here i have a

play01:15

basic c program now i'll create a

play01:17

variable to store is

play01:20

int is

play01:21

now i'll ask the user to input there is

play01:24

for that i'll use printf statement

play01:28

and print the message like

play01:31

enter

play01:32

here is

play01:36

then i'll use scanf statement to take

play01:39

the input

play01:41

so percent d

play01:43

comma

play01:44

ampersand is

play01:51

then i'll use if condition to check if

play01:54

a's is greater than or equals to 18.

play02:00

if this condition is true i'll print

play02:07

you are

play02:08

eligible

play02:09

to vote

play02:14

now let me run this program

play02:17

here i'll provide 31 as the is and i'll

play02:20

press enter as you can see

play02:22

you are eligible to vote is printed

play02:25

here the value is 31 so our test

play02:28

condition is greater than or equals to

play02:31

18 is true that is why this print

play02:34

statement

play02:35

is executed

play02:36

now let me run this program again

play02:39

this time instead of 31 i'll enter 15

play02:43

this time nothing is printed on the

play02:44

screen this is because the a's is 15 so

play02:47

the test condition is is greater than or

play02:50

equals to 18 is false so the body of the

play02:54

if statement is skipped

play02:56

our program is working fine but it is

play02:59

not printing anything when the a's is

play03:01

less than 18. we might want to print

play03:03

something like sorry you are not

play03:05

eligible to vote when age is less than

play03:07

18 so i'll add another if statement

play03:12

so i'll say

play03:14

if

play03:15

is is less than 18

play03:19

i'll print

play03:23

sorry

play03:25

you are not

play03:28

eligible

play03:29

to vote

play03:34

now i'll run this code

play03:37

and i'll enter 15 again

play03:40

here you can see sorry you are not

play03:42

eligible to vote is printed this is

play03:44

because ace is 15 so this condition is

play03:47

is greater than or equals to 18 is false

play03:50

therefore the statement

play03:53

is

play03:53

skipped however this condition is is

play03:56

less than 18 is true so this statement

play03:59

is executed

play04:01

by the way if you are watching this

play04:02

there is a good chance you want to

play04:04

improve your skill in c programming

play04:06

lucky for you we have a mobile app that

play04:08

provides a well-structured c programming

play04:10

course with certification at the end and

play04:13

you can use the app alongside the video

play04:15

to practice on the built-in compiler our

play04:17

course is free so download now by

play04:19

scanning this qr code or click the link

play04:21

in the video description

play04:25

i have this code from the last segment

play04:27

i'll remove this code to get the user

play04:30

input

play04:32

and i'll manually assign the value of is

play04:35

equals to 15

play04:36

so that it's easier to focus on the

play04:39

logic of the if statement in this

play04:42

program i have used two if statement to

play04:45

perform two different tags

play04:47

we know that the person is eligible to

play04:49

vote only if the a's is greater than or

play04:52

equals to 18 and if the condition is not

play04:55

met we know that the person is not

play04:57

eligible to vote

play04:59

in such cases instead of writing the

play05:02

second if statement with the condition

play05:05

we can use else clause

play05:07

let's first look at the syntax of the if

play05:10

statement with the else clause

play05:13

on your screen you can see how it looks

play05:16

so what happens here is if our test

play05:18

condition is true

play05:20

statement inside the body of if

play05:22

statement is executed and if it is false

play05:26

statements inside the body of else are

play05:30

executed

play05:31

now getting back to our code to check if

play05:33

the person is eligible to vote or not

play05:36

in this program i'll use the else clause

play05:39

instead of this second if statement so

play05:41

i'll remove the second if statement and

play05:44

replace it with

play05:45

else

play05:47

now let me read this code in plain

play05:48

english as is greater than or equals to

play05:51

18 then print you are eligible to vote

play05:55

else print sorry you are not eligible to

play05:58

vote now when i run this code

play06:00

you can see sorry you are not eligible

play06:03

to vote is printed

play06:05

this is because ace is 15 which is not

play06:08

greater than or equals to 18

play06:11

so we get the output sorry you are not

play06:13

eligible to vote

play06:15

okay guys we need your support to keep

play06:18

this type of content free for all user

play06:20

youtube really likes engagement on the

play06:22

video so leave a comment below press

play06:24

that like button hit subscribe if you

play06:26

haven't already let's get the engagement

play06:28

score high up so that more people can

play06:30

discover and enjoy these courses

play06:34

the if statement with else clause allow

play06:37

us to make a choice from two different

play06:39

options however sometimes we need to

play06:42

make choice from more than two options

play06:45

in those case we can use elsif's clause

play06:48

with test condition

play06:49

let's see the syntax first

play06:52

the if statement checks the first

play06:54

condition inside the if statement if it

play06:57

is true the body of if is executed and

play07:00

the statement 2

play07:02

and the statement 3 are skipped however

play07:05

if the first test condition is false the

play07:09

control of the program jumps to the

play07:11

second test condition

play07:13

if this condition is true the body of

play07:16

the else if statement is executed and

play07:19

other statement are skipped if both the

play07:22

test condition 1 and the test condition

play07:25

2 are false

play07:27

then only the else body is executed

play07:31

if necessary we can add as many else if

play07:33

clauses as we want for our program to

play07:36

work and among all those alternatives

play07:38

only a single block of code is executed

play07:41

now we know how the else if clause works

play07:44

let's get back to our code to check

play07:46

whether a person can vote or not

play07:49

here the is variable stores the a's of

play07:51

the person currently the is of oldest

play07:54

person is 120 so i want this program to

play07:57

consider is greater than 120 as invalid

play08:01

similarly the a's cannot be negative so

play08:04

i also want to write less than 0 as

play08:06

invalid

play08:07

now i want to include this two

play08:09

additional condition in our program for

play08:12

that i'll use else if clause now i'll

play08:15

modify this code

play08:16

so

play08:17

if

play08:19

a's is

play08:20

greater than 1 and 20

play08:23

i'll print

play08:27

invalid is

play08:29

invalid

play08:30

is

play08:37

and

play08:38

as if

play08:42

so if a's is

play08:44

less than 0

play08:46

that is negative value then will print

play08:50

is

play08:51

invalid

play08:52

is

play08:55

i'll change this if to elsif

play09:00

so as you can see i have included one if

play09:02

condition

play09:04

two else if condition

play09:06

and finally one else condition

play09:09

here i'll change the value from 15 to

play09:12

130

play09:14

now i'll run this code

play09:16

here the a's is greater than 120 so we

play09:19

get invalid is as our output

play09:22

now let's change the value of this is

play09:25

from 130 to minus 4 and let's run this

play09:28

again

play09:29

this time we get invalid is again

play09:33

so this is the condition of else if here

play09:36

is is less than zero so the output we

play09:40

got is invalid is

play09:42

now we can add as many else if condition

play09:45

as we want here we can see these two

play09:48

condition are performing the same tags

play09:50

printing the invalid is

play09:53

in this case we can combine both the

play09:55

condition together using logical

play09:57

operator

play09:58

here the a's is invalid if it is either

play10:01

greater than 120 or less than 0 or let

play10:05

me modify this code so i'll cut this

play10:08

i'll use logical r and put here

play10:11

is

play10:12

less than 0. so i'll change this value

play10:15

from -4 to -1 and when i run this code

play10:20

i get invalid is as output

play10:24

now as long as the a's is an integer

play10:26

number our program works correctly and

play10:29

the extra condition make sure the a's is

play10:31

neither greater than 120 or less than 0

play10:35

and for any other value of a's the

play10:37

program runs as intended if we provide

play10:40

the is equals to 50 so let's do that

play10:43

i'll provide the value of a is equals to

play10:45

50 i'll run this

play10:48

now this person is eligible to vote

play10:51

before we end this video one quick tip

play10:54

for you if the body if else block has

play10:56

only one statement like this then we can

play10:59

omit the curly braces of the if block

play11:02

like this

play11:03

as you can see the syntax of the if

play11:05

statement is pretty simple the harder

play11:08

part is the logic behind the test

play11:10

conditions so you will get better at

play11:13

creating test condition with practice

play11:15

also be sure to check our video on the

play11:18

comparison and logical operators that

play11:20

are used to create the test condition

play11:22

the link will be in the video

play11:24

description below

play11:28

we have covered a lot in this video it's

play11:31

time for you to practice what we have

play11:33

learned here is one programming

play11:34

challenge for you to solve can you

play11:36

create a program to check whether a

play11:38

number is positive negative or zero to

play11:42

create this program create a variable

play11:44

named number and assign a double value

play11:46

to it based on the user input then using

play11:49

an if statement check if the number

play11:51

variable is positive negative or zero

play11:55

if the number is positive print the

play11:57

number is positive if the number is

play11:59

negative print the number is negative

play12:01

and if the number is 0 print the number

play12:04

is 0. you'll find all the answers to

play12:06

this question in our github repository

play12:09

and also if you want to revise the

play12:10

concept we'll learn today you can find

play12:13

all programs in the repository since the

play12:15

video is already long we are not

play12:17

covering the nested if else statement if

play12:20

you are interested you can learn about

play12:22

them from our website programmings.com

play12:25

i'll put the link in the video

play12:26

description

play12:27

now that we are at the end of this video

play12:29

it's time for programming squeeze

play12:31

what is the output of following code

play12:36

comment your answer below and see you on

play12:38

the next video happy programming

play12:44

[Music]

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
C ProgrammingIf-ElseDecision MakingElection ExampleVoting EligibilityConditional LogicProgramming TutorialCode SyntaxLogical OperatorsProgramming Challenge
هل تحتاج إلى تلخيص باللغة الإنجليزية؟