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

in hive-195150 •  2 months ago  (edited)

steemit-engagement-challenge-cover.jpeg

Greetings Steemit friends

Conditional structures are a way of controlling the outcome of actions in a program depending on particular actions. This structure is used to break and execute small blocks of code while following a particular condition. The results of conditional structures are either true or false depending on the conditions. There are different types of conditional structures in programming.

1. If Statement: This is the commonly used conditional structure. Only one condition is passed, and the code will be executed if the condition is true.

Algoritmo week4
Define number01 as Integer;
//If example
Print "Pick your lucky number: "
Read number01;
if number01 >= 100 Then
Print 'Wow, You did it';
endIf
FinAlgoritmo

Screenshot 2024-10-03 100609-if.jpg

2. If else Statement: This is an added step where a block of code is expected to give two results. That is either true or false after execution. Just as the added to the conditional structures else , give the developer the possibility of determining which block code is true or false.

Algoritmo week4
Define number01 as Integer
//If example
Print "Pick your lucky number: "
Read number01;
// if else example
if number01 >= 100 Then
Print 'Wow, You did it';
else
Print 'Sorry, you missed it.';
endIf
FinAlgoritmo

Screenshot 2024-10-03 100609-if-else.jpg

Screenshot 2024-10-03 100609-if-else-b.jpg

3. if-elseif-else Statement: One more step added to the If else statement. This is to give the programmer the possibility to check to multiply conditions which will return true.

4. Nested if Statements: Yeah, this is not a stand-alone conditional structure, as is all a combination of the above statements. Here you have the possibility of having a conditional structure within a conditional structure. That is, programmers can place an if statement within another if statement to handle complex cases.

Algoritmo week4
Define number01, state01 as Integer;
state01 = 150;
Print "Pick your lucky number: "
Read number01;
// nested conditions example
if number01 >= 100 Then
if number01 == state01 Then
Print 'Wow, You Pick a winning number with a coin.'
Print 'Winning 150 STEEM'
else
Print 'Wow, You Pick a winning number without coins.'
endif
else
Print 'Sorry, you missed it.'
endif
FinAlgoritmo

Screenshot 2024-10-03 133104-nested-1.jpg

Screenshot 2024-10-03 133104-nested-2.jpg

Screenshot 2024-10-03 133104-nested-3.jpg

The conditional structure is about checking a block of code and returning a result of either true or false. Conditional structure makes use of Comparison operators (==, !=, <, >, <=, >=) and logical operators ( and, or, not) to analyze and execute a block of codes.

  • Question2

Here, I will define variables that will hold the values of each of the directions in which the switch is moved. Note a switch can only go two ways, that on or off. So I will make use of the condition structure to check if someone in the room has switched on the light.

Screenshot 2024-10-03 200009-1.jpg

Algoritmo Week402
Define onlight, offlight, switch as Integer;
Define walkin, walkout as Character;
onlight = 1;
offlight = 0;
walkin = 'Welcome to the room, What do you want to do?';
walkout = 'You have left the room.';
Print walkin;
Print 'Turned on the light by typing 1';
Read switch
if (switch == onlight) Then
Print 'Wow You just turn on the light';
Print 'Do you want to walkout,';
Print 'Please turned off the light by typing 0';
Leer switch;
if (switch == offlight) Then
Print 'Wow You just turn off the light';
Print walkout;
Print 'Great Job.';
else
Print 'Wrong switch, You left the light On.'
Print 'Please type 0.';
Read switch;
if (switch == offlight) Then
Print 'Wow Thank You just turn off the light';
Print 'Great Job.';
endIf
endIf
else
Print 'Wrong switch. Please type 1 or 0.';
endIf
FinAlgoritmo

Screenshot 2024-10-03 200009-2.jpg

You can only switch off the light when you are in the room. That is the reason for me having the nested if statement in the if. Here I can ask whoever is in the room to switch off the light if they are walking out of the room.

  • Question3

Here we have a small program to handle an average of four subjects. Let's start by always defining all necessary values, which I will be making use of. What is the formula to handle the average?

Average = total of all subject ratings divide by the number of subjects (4)

Screenshot 2024-10-03 202439last-1.jpg

Screenshot 2024-10-03 202439last-2.jpg

Let's check if all subjects were entered, and check if the ratings are greater than or not equal to 0 before calculating the average.


Algoritmo Week403
// Define variables,
Define totalSum, average as Integer;
Define chemistry, biology, math, french as Integer;
Define countVaribles, numberVariable as Integer;
countVaribles = 0;
// Calculate the average,
numberVariable = 4;
average = totalSum / numberVariable;
Print 'let enter the different ratings';
Print 'let enter ratings for Chemistry';
Read chemistry;
Print 'let enter ratings for Biology';
Read biology;
Print 'let enter ratings for Maths';
Read math;
Print 'let enter ratings for French';
Read french;
//Si chemistry, biology, math, french
if chemistry <> 0 AND biology <> 0 AND maths <> 0 AND french <> 0 Then
For i from 1 to numberVariable do
countVaribles = countVaribles + 1;
EndFor
if countVaribles == numberVariable Then
totalSum = chemistry + biology + math + french;
Print totalSum;
Print numberVariable;
average = totalSum / numberVariable;
Print 'Average of the four ratings is' average;
if average >= 70 Then
Print 'Section Passed'
else
Print 'Section can Improve'
endIf
endIf
else
Print 'All Four ratings were not entered.'
endIf
FinAlgoritmo

To be sure that we have four ratings, I use a for loop to count the number of variables. If it is equal to, which is what is required in the task. Go ahead and sum up the rating input.

At this point, I have the total sum of the ratings and the number of inputs equal to 4. Note, I will do a check one after the other. But our task mentions 4, which makes it easy to check all four variables in one condition. Here making use of both Comparison and logical operators.



Cheers
Thanks for dropping by
@fombae

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
Loading...

image.png

Curated by : @lirvic