Prog I Chapter IV Conditionals and Loops Student Manual

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Computer Programming in C++

(IT 112/CpE 112)

Chapter IV: Conditionals and Loops

Minerva M. Fiesta

2020
Conditionals and Loops

I. The if Statement
II. The else Statement
III. The while Loop
IV. Using While Loop
V. The for Loop
VI. The do…while Loop
VII. The switch Statement
VIII. Logical Operators
IX. Chapter Quiz
Lesson I: The if Statement

Decision Making

The if statement is used to execute some code if a condition is true.


Syntax:

The condition specifies which expression is to be evaluated. If the condition is


true, the statements in the curly brackets are executed.

 If the condition is false, the statements are simply ignored, and the program continues to run
after the if statements body.
Relational Operators

Use relational operators to evaluate conditions.


For Example:

The if statement evaluates the condition (7>4), finds it to be true, and then
executes the cout statement.
If we change the greater than operator to a less than operator (7<4), the
statement will not be executed and nothing will be printed out.

 A condition specified in an if statement does not require a semicolon

The not equal to operator evaluates the operands, determines whether or not
they are equal. If the operands are not equal, the condition is evaluated to true.
Relational Operators
Operator Description Example
>= Greater than or equal to 7>=4 True
<= Less than or equal to 7<=4 False
== Equal to 7==4 False
!= Not equal to 7!=4 True

For Example:

The not equal to operator evaluates the operands, determines whether or not they are
equal. If the operands are not equal, the condition is evaluated to true.

For Example:

C++isThe
used to create
above computer
condition programs.
evaluates to false and the block of code is not executed

You can use relational operators to compare variables in the if statement.


For Example:
Chapter IV Lesson 1 Evaluation

1. Which choice shows the correct syntax for the if statement?


a. if test
b. if (test) { }
c. IF test
2. Which is the correct operator for the equality testing?
a. *=
b. ==
c. =
d. <<
3. Write a simple program that prints “not equal” on the screen. Let x = 10
and y = 8.
4. Design a program that prints the value of the greatest variable.
Let a = 98; b = 76; Apply the “if statement”.
5. Create a program that will display “Welcome to our Virtual Classroom” if
the value of x is greater than y. Let x = 5 and y = 3.
Chapter IV Lesson 2: The else Statement

An if statement can be followed by an optional else statement, which executes


when condition is false.
Syntax:

An if/else statements, a single statement can be included without enclosing it


into curly braces.

 Including the curly braces anyway is a good practice, as they clarify the code and make it
easier to read.

The code above will test the condition:


- If it evaluates to true, then the code inside the if statement will be
executed.
- If it evaluated to false, then the code inside the else statement will be
executed.

 When the only one statement is used inside the if/else, then the curly braces can be omitted.

For example:
In all previous examples, only one statement was used inside the if/else
statement, but you may include as many statements as you want

Nested if Statements

You can also include or nest, if statements within another if statement.


For Example:
C++ provides the option of nesting an unlimited number of if/else statements.
For Example:

 Remember that all else statements must have corresponding if statements

The if else Statement

In if/else statements, a single statement can be included without enclosing it


into curly braces.

 Including the curly braces { } anyway is a good practice, as they clarify the code and make it
easier to read.
Chapter IV Lesson 2 Evaluation

1. How many statements could be placed between if’s braces?


a. 1
b. 2
c. As many as you want
2. How many nested if statements can an if statement contain?
a. As many as you want
b. Only two
c. None
3. Design a program that will compare two variables, if variable y is greater
then print: “My future”, else print “My past”. Let y = 10 and x = 20.
4. Create a simple program that check if the age variable in greater than 18.
Note: Use any variable name.
5. Create a program that prints “x is greater than 33” if the given value of x
is greater, else it will print “x is not greater than 33””. Let value of
variable x = 22.
Chapter IV Lesson 3: Loops

A loop repeatedly executes a set of statements until a particular condition is


satisfied.
A while loop statement repeatedly executes a target statement as long as a
given condition remains true.
Syntax:

the loop iterates while the condition is true.

 At the point when the condition becomes false, program control is shifted to the line that
immediately follows the loop.

The while Loop

The loop’s body the block of statements within the curly braces.
For Example:
The example above declares a variable equal to 1 (int number = 1).
The while loop checks the condition (num <6), and executes the statements in
its body, which increment the value of number by one each time the loop runs.

 After the 5th iteration, number becomes 6, and the condition is evaluated to false, and the loop
stops running.

The increment value can be changed. If changed, the number of times the loop
run will change as well.

 Without a statement that eventually evaluates the loop condition to false, the loop will continue
indefinitely.
Using the Increment or Decrement
The increment or decrement operators can be used to change the values in the
loop.
For Example:

 num++ is equivalent to num = num + 1.

Using a while loop


A loop can be used to obtain multiple inputs from the user.
Let’s create a program that allows the user to enter a number 5 times, each
time storing the input in a variable.
 The code above asks for the user to input any number 5 times, and each time saves the input
in the number variable.

Now let’s modify our code to calculate the sum of the numbers the user has
entered.

The code above adds the number entered by the user to total variable with
each loop iteration.
Once the loop stops executing, the value of total is printed. This value is the
sum of all the numbers the user entered.

 Note that the variable total has an initial value of 0.


Chapter IV Lesson 3 Evaluation

1. Which is the correct syntax for the while loop?


a. while (variable <=5){cout<<”text”; }
b. WHILE variable<=5 { cout<<”text”;}
c. while bacon <= 5 cout<<”text”;
2. Design a program that will print the value of x on the screen using the
while looping statement with the condition x is greater than or equal to 5
3. Create a program that will display only the even values of a variable on the
screen, let variable x value be lesser than or equal to 20.
4. Create a program that prints the value of x from 6 to 10 using the ++
operator.
5. Design a program to allow a user to enter a value using cin and store it in
‘ Num “ variable 5 times.
6. Create a program that allows the user to enter 5 numbers and print the
sum on the screen. Let initial value of sum equals 5.
Chapter IV Lesson 5: The for loop
A for loop is a repetition control structure that allows you to effectively write a
loop that executes a specific number of times
For Example:

The initialization step is executed first, and does not repeat. Next, the
condition is evaluated, and the body of the loop is executed if the condition is
true.
In the next step, the increment statement updates the loop control variable.
Then the loop’s body repeats itself, only stopping when the condition becomes
false.
For Example:

 The initialization and increment statement may be left out, if not needed, but remember that
the semicolons are mandatory

the example below uses for loop to print numbers from 0 to 9.


in the initialization step, we declared a variable x and set it to equal 0.
X < 10 is the condition.
After each iteration, the x++ increment statement is executed.

 When x increments to 10, the condition evaluates to false, and the loop stops.

It’s possible to change the increment statement.

You can also use decrement in the statement

 When using the for loop, don’t forget the semicolon after the initialization and condition
statements.
Chapter IV Lesson 5 Evaluation

1. Which one is the correct syntax for the for loop?


a. for int x = 1; y<10, ++y { }
b. for (int x =1; x<10; x++ ) { }
c. FOR x = 1, x < 10: x++ { }
d. for (int x = 1,, x < 10; x++;) { }
2. Create a program that prints a’s value on the screen 5 times using the for
looping statement.
3. Design a program that prints EVEN (multiple of two) values from 0 to 20 using
a for loop.
Chapter IV Lesson 6: The do…while loop

The do…while loop


Unlike for and while loops, which test the loop condition at the top of the loop,
the do…while loop checks its condition at the bottom of the loop.
A do while loop is similar to a while loop. The one difference is that the do
while loop is guaranteed to execute at least one time.
Syntax:

 For example, you can take input from the user, then check it. If the input is wrong, you can take
it again.

Here is an example:

 Don’t forget the semicolon after the while statement


while vs do while
if the condition evaluated to false, the statements in the do would still run
once:

 The do while loop executes the statements at least once, and then tests the condition. The
while loop executes the statement after testing the condition.

As with other loops, if the condition in the loop never evaluates to false, the loop will
run forever.
For Example:

This will print 42 forever on the screen

 Always test your loops, so you know that they operate in the manner you expect.
Chapter IV Lesson 6 Evaluation

1. Which is the correct syntax for do while loop?


a. Do while test;
b. do while (test)
c. do{ } while (test);
2. how is a do while different from a while loop?
a. do while loop tests the condition before running the code.
b. do while loop runs your code at least one time.
c. While loop runs the code before testing the condition
3. design a program that prints the x variables from 1 to 10.
4. Create a do while loop program that prints “this is a loop” on the screen
15 times.
Chapter IV Lesson 7: Logical Operators

Logical Operators
Use logical operators to combine conditional statements and return true or
false.
Operator Name of Operator Form
&& AND Operator y&&y
|| OR Operator x||y
! NOT Operator !x

The AND operator works for the following way:


Left Operand Right Operand Result
false false false
false true false
true false false
true true true

 In the AND operator, both operands must be true for the entire expression to be true.

For Example:
In the example above, the logical AND operator was used to combine both
expressions.

 The expression in the if statement evaluates to true only if both expressions are true.

Within a single if statement, logical operators can be used to combine multiple


conditions

 The entire expression evaluates to true only if all of the conditions are true

The OR Operator
The OR (||) operator returns true if one of its operands is true.
Left Operand Right Operand Result
false false false
false true true
true false true
true true true

Example:
 You can combine any number of logical OR statements you want.
 In Addition, multiple OR and AND statements may be chained together.

Logical NOT

The logical NOT (!) operator works with just a single operand, revising its logical
state. Thus, if a condition is true, the NOT operator makes it false, and vice
versa.
right operand result
true false
false true

 Be careful using this, because !false means true.


Chapter IV Lesson 7 Evaluation

1. The result of ‘a && b’ is true if:


a. Both a and b are false
b. Both a and b are true
c. Either a or b is true
2. How many && operators can be used in one if statement?
a. Two
b. As many as you want
c. Only one
3. Create a program that tests both the conditions in the if statement using
the logical operator AND if age is greater than 21 and money is lesser
than 500. Let variable age = 23 and money = 4, then print “WELCOME”
on the screen.
4. Design a program that will print “Welcome” on the screen using the
logical OR condition if age is greater than 21 or money is greater than
500. Let variable age = 23 and money = 4, then print “WELCOME” on the
screen.
5. The result of a || b is true if :
a. Neither a nor b is true.
b. Either a or b is true
c. Both a and b are true

You might also like