Grade 10 Q4 Module 1 2 Conditional Loop
Grade 10 Q4 Module 1 2 Conditional Loop
Quarter 4 – Module 2:
Conditional and Looping Statements
Self-Learning Module
Learner’s Material
I Need to Know
In this module, you will be introduced to different learning activities preparing you
to be skillful before you take the plunge into the world of Programming.
This module is specifically crafted to focus on Programming Language C++
enriched with learning activities that will assess your level in terms of skills and knowledge.
1. The computer has the capability to make decisions based on a given set of
conditions or choices.
2. In if-else statement, when the condition is proven true, statement 1 is executed,
otherwise statement 2 is executed.
3. The looping process will be repeated by the program all over again as long as the
condition remains false.
4. In the absence of else, there is nothing to be executed if the if conditional statement
is evaluated to false.
5. The else part can be included or not in C++ program, which means the program will
still run even without it.
6. The for loop provides specific locations to contain an initialization statement and an
increase/decrease statement.
7. With the use of if-else if-else conditional statement, the computer can decide to
choose one and only one of the given choices and its associated statement will be
executed.
8. The variable initialization specifies the first value where the loop starts.
9. In programming, the character data lowercase b is equivalent to uppercase B.
10. Iteration Structures or Loops are program instructions that repeat some statement
in a specified number of times.
2
Lesson
Conditional Statements and
1 Loops
Lesson Overview
The if Statement
The if statement selects and executes the statement(s) based on a given condition. If the
condition evaluates to True then a given set of statement(s) is executed. However, if the
condition evaluates to False, then the given set of statements is skipped.
Syntax of if statement
if (condition)
statement 1;
3
The if-else Statement
The if-else statement causes one of the two possible statement( s) to execute,
depending upon the outcome of the condition. The if-else statement comprises two parts,
if and else. If the condition is True, the if part is executed. However, if the condition is
False, the else part is executed.
if (condition)
statement 1;
else
statement 2;
Also known as the if-else-if staircase, has an if-else statement within the outermost else
statement. Only those conditions that were first proven true, are the ones to be chosen
and its associated statement(s) will be executed.
if (condition1)
statement 1;
else if (condition2)
statement 2;
else if (condition3)
statement 3;
else
statement4;
4
/*This program determines if the input letter is in uppercase or
lowercase. If a character other than letters is entered as input,
the program will display,”It is not an alphabet”.*/
//Sample if-else if-else Conditional Statement
#include <iostream>
using namespace std;
int main()
{
char ch; //assigned data type to variable ch is character
cout<<"Enter an alphabet:";
cin>>ch;
if( ch>='A' && ch<='Z') //condition1
cout<<"The alphabet is in upper case";/*this statement will be
executed if condition1 is
TRUE*/
else if ( ch>='a' && ch<='z' )//condition2
cout<<"The alphabet is in lower case";/*this statement will be
executed if condition2 is
TRUE*/
else
cout<<"It is not an alphabet";/*this statement will be executed if
condition is FALSE*/
return 0;
}
A nested if-else statement contains one or more if-else statements. The if else can be
nested in three different ways:
The while loop allows programs to repeat a statement or series of statements, over and
over, as long as a certain test condition is true. While Loop is Known as Entry Controlled
Loop because in The while loop first we initialize the value of variable or Starting point of
Execution and then we check the condition and if the condition is true then it will execute
the statements and then after it increments or decrements the value of a variable.
5
Syntax of while loop
initialization;
while (condition)
{
statements;
inc/dec;
{
//This program generates the sequence nos. 12345 using while loop
#include <iostream>
using namespace std;
int main()
{
int n; //variable declaration
n=1;//initialization
while (n<=5)//condition
{
cout <<n; //statement
n++;//incrementation
}
return 0;
}
The for loop allows programs to repeat a statement while condition remains true. In This
loop all the basic operations like initialization, condition checking and incrementing or
decrementing all these are performed in only one line. This is similar to the while loop for
performing its execution but only different in its syntax.
//This program generates the sequence nos. 12345 using for loop
#include <iostream>
using namespace std;
int main()
{
int n; //variable declaration
for (n=1; n<=5; n++) //initialization, condition, incrementation
{
cout << n; //statement
}
return 0;
}
6
The do-while loop
The do-while loop is similar to the while loop, except that the test condition occurs at the
end of the loop. Having the test condition at the end, guarantees that the body of the loop
always executes at least one time. This is Also Called as Exit Controlled Loop.
initialization;
do
{
statements;
inc/dec;
}
while (condition);
I Will Do This
7
I Learned This
I have learned that the computer has the capability to make decisions based on a
given set of conditions and choices. This can be done through the application of conditional
statements in our program. Conditional statements are ___________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________.
Every Conditional Statement has its own syntax which ______________________
______________________________________________________________________
______________________________________________________________________.
I Practice This
Practice coding the given sample programs as shown above (Conditional Statements and
Loops). You may access this link https://www.onlinegdb.com/online_c++_compiler to write
and run your program to see the result/output.
I Apply This
1. Write a program to display the high school level of a student, based on its year-
entry. For example, the year-entry 1 means the student is a freshman, 2 for
sophomore, and so on. Here are the given criteria:
1 Freshman
2 Sophomore
3 Junior
4 Senior
8
2. Write a program that generates the given sequence numbers:
54321
Post Test
#include <iostream>
using namespace std;
int main ( )
{
int n;
for (n=5; n>=1; n--)
{
cout << n ;
}
}
5. From the sample program shown above, the variable n initialize with n>=1.
6. The program evaluates the condition: n=5.
7. Since n has a value of 5, the condition is evaluated to true, therefore all the associated
statements within the body of the loop are executed.
8. After executing the statement, the program loops again and will execute the
decrementation, thus, the value of n is decreased by 1.
9. When the value of n becomes 4, the looping process will be terminated.
10. The output of this program is 1 2 3 4 5.
REFERENCES
http://www.cs.cornell.edu/courses/cs1130/2012sp/1130selfpaced/module2/module2part1/ifloop.html
https://ecomputernotes.com/cpp/control_structure/conditional-statements