Basic programming course: Lesson #4 Control structures. Part 1. Conditionals.

in #devjr-s20w42 months ago

Copy of @jyoti-thelight.jpg

SOURCE

Control structures. Part 1. Conditionals.

Describe your understanding of conditional structures and mention 2 situations of everyday life where you could apply this concept.

Control statements in C or any other language allow the user to dictate the flow of program control. Simply put, control statements allow users to choose the sequence in which instructions in a program are executed. These make it possible for the program to make certain decisions, repeat various tasks, or move from one code section to another.

Reasons to use control statements

In the C language, control of the program flows from one instructed to another. This type of flow of control from one command to another is called sequential control flow. Now, in any C program, a programmer may want to repeat certain instructions or skip instructions when they write the logic. Declarations in C, also known as control declarations or decision-making, help them in making decisions such as pass or fail, eligible or not, children or adult etc.

Conditional statements are used in C programming language to make certain decisions based on available conditions. These conditional statements are executed sequentially if there is no condition surrounding the statements. Whenever we put a condition on block statements, the flow of execution can change based on the result evaluated by the condition in the program. The process mentioned here is called decision-making in C language.

Types of Control Statements

There are five types of statements in C language

  • If statements
  • Switch Statement
  • Conditional Operator Statement
  • Goto Statement
  • Loop Statements

2 situations of everyday life where you could apply this concept.

Situations:1

In the first situation, we are going to write a program to check the result of a student who has written an exam in a school. In this, we need to tell us whether the mark of the student is pass or fail.

For this we use the " if" statement:

This program does the following:

We define constants for the number of subjects are 5 and the pass mark is 35.
We use an array to store the marks for each subject.
We input marks for each subject, validating that they're between 0 and 100.
We calculate the total marks and keep track if the student fails any subject.
We calculate the average marks.
Finally, we display the results, including total marks, average, and whether the student passed or failed.
The student is considered to have failed if:

They score less than 35 in any individual subject, OR
Their overall average is less than 35
You can compile and run this program. It will prompt the user to enter marks for 5 subjects, then display the results.

Here is the code:
image.png

Lets put marks so 5 subjects to check result:
image.png

Here is the result:
image.png

Situation2

Second Situation When a citizen of a country goes to vote, we have to check whether he is eligible to vote or not.

This program does the following:

  • We define a constant "VOTING AGE" set to 18, which is the minimum age for voting eligibility.

  • We use a do-while loop to allow checking multiple citizens' eligibility.

For this we use the"loop" statement:
  • We prompt the user to enter the citizen's age.
  • We validate the input to ensure it's a realistic age (between 0 and 150).
  • We check if the entered age is greater than or equal to the voting age (18).
  • We print whether the citizen is eligible to vote or not.
  • If they're not eligible, we also print how many years until they become eligible.
  • After each check, we ask the user if they want to check another citizen's eligibility.

The program continues to run until the user chooses not to check any more citizens.

This program provides a simple and interactive way to check voting eligibility for multiple citizens. It also includes input validation to ensure realistic age inputs and provides additional information for those who are not yet eligible to vote.

You can compile and run this program. It will prompt the user to enter a citizen's age and then display whether they are eligible to vote or not. The user can continue checking for multiple citizens until they choose to stop.

image.png

image.png

image.png

image.png

image.png

Create a program that tells the user "Welcome to the room What do you want to do?", if the user writes 1 then it shows a message that says "you have turned on the light", if the user writes 2 then it shows a message that says "you have left the room". Use conditionals.

I'll create a C program that implements the functionality you've described using conditional statements. Here's the program:

We use conditional statements (if, else if, and else) to handle the user's input:

If the choice is 1, we print "You have turned on the light."
If the choice is 2, we print "You have left the room."
If the choice is neither 1 nor 2, we print an error message indicating an invalid choice.
This program provides a simple interaction with the user, allowing them to make a choice and receive appropriate feedback based on their input. The conditionals ensure that the correct message is displayed for each valid choice, and an error message is shown for invalid inputs.

image.png

image.png

image.png

Create a program that asks the user for 4 different ratings, calculate the average and if it is greater than 70 it shows a message saying that the section passed, if not, it shows a message saying that the section can improve.

We define constants "NUM_RATINGS" set to 4 and "PASSING_SCORE" set to 70 for easy modification if needed.

In the main() function:

We declare an array of ratings to store the 4 ratings.
We also declare variables for the sum and average.
We use a "for loop" to ask the user for 4 ratings so that it will check continuously
We add each rating to the sum.
After collecting all ratings, we calculate the average by dividing the sum by the number of ratings.

We ask to display the calculated average.

We use an if-else statement to check if the average is greater than the passing score. This program provides a straightforward implementation of your requirements. It collects the ratings, calculates the average, and provides feedback based on whether the average exceeds 70 or not.

image.png

Now we compile and run this program. It will prompt the user for 4 ratings, display the average, and then indicate whether the section passed or needs improvement based on the calculated average.

image.png

When we hit enter the results show as below:
image.png

I would like to invite
@ifatniza
@roohiasif99
@senehasa

to take part in this contest.

💚THANK YOU 💚

Discord : @jyoti-thelight#6650 Telegram :- https://telegram.org/dl

jo.gif

Sort:  
Loading...

Congratulations, your post has been upvoted by @scilwa, which is a curating account for @R2cornell's Discord Community. We can also be found on our hive community & peakd as well as on my Discord Server

Manually curated by @ abiga554
r2cornell_curation_banner.png

Felicitaciones, su publication ha sido votado por @scilwa. También puedo ser encontrado en nuestra comunidad de colmena y Peakd así como en mi servidor de discordia

Thank you for the great support

Upvoted! Thank you for supporting witness @jswit.

Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.

It seems that you have good knowledge of the subject. All the best.

Your post has been rewarded by the Seven Team.

Support partner witnesses

@seven.wit
@cotina
@xpilar.witness

We are the hope!

Coin Marketplace

STEEM 0.22
TRX 0.21
JST 0.035
BTC 98789.27
ETH 3346.59
USDT 1.00
SBD 3.08