0% found this document useful (0 votes)
13 views21 pages

Conditional Statements 1

Chapter 6 discusses control flow statements in programming, focusing on condition statements, if statements, inline if statements, and switch statements. It explains how to evaluate conditions using comparison and logical operators, and provides examples of how these statements can be used to control program flow based on user input. The chapter emphasizes the syntax and functionality of each control flow statement with practical coding examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views21 pages

Conditional Statements 1

Chapter 6 discusses control flow statements in programming, focusing on condition statements, if statements, inline if statements, and switch statements. It explains how to evaluate conditions using comparison and logical operators, and provides examples of how these statements can be used to control program flow based on user input. The chapter emphasizes the syntax and functionality of each control flow statement with practical coding examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 6

Making Choices and


Decisions
Introduction
In this chapter, we are going to cover another
fundamental concept in programming; we’ll learn how to
control the flow of a program using control flow statements.

Specifically, we will learn about the if statement,


the inline if statement, the switch statement.
However, before we go into these control tools, we have to
first look at condition statements.
Condition Statements
Most control flow statements involve evaluating a condition statement. The
program will proceed differently depending on whether the condition is met.

The most common condition statement is the comparison statement. If we


want to compare whether two variables are the same, we use the == sign (double
=). For instance, if you write x == y, you are asking the program to check if the
value of x is equal to the value of y. If they are equal, the condition is met and the
statement evaluates to true. Else, the statement evaluates to false.

In addition to evaluating whether one value is equal to another, there are


other comparison operators that we can use in our condition statements.
Not equal (!=)
Returns true if the left is not equal to the right
5 != 2 is true
6 != 6 is false
Greater than (>)
Returns true if the left is greater than the right
5 > 2 is true
3 > 6 is false
Smaller than (<)
Returns true if the left is smaller than the right
1 < 7 is true
9 < 6 is false
Greater than or equal to (>=)
Returns true if the left is greater than or equal to the right
5 >= 2 is true
5 >= 5 is true
3 >= 6 is false
Smaller than or equal to (<=)
Returns true if the left is smaller than or equal to the right
1 <= 7 is true
7 <= 7 is true
9 <= 6 is false
We also have three logical operators (&&, ||, !) that are useful if we want to
combine multiple conditions.
The AND operator (&&)
Returns true if all conditions are met
5==5 && 2>1 && 3!=7 is true
5==5 && 2<1 && 3!=7 is false as the second condition (2<1) is false

The OR operator (||)


Returns true if at least one condition is met.
5==5 || 2<1 || 3==7 is true as the first condition (5==5) is true
5==6 || 2<1 || 3==7 is false as all conditions are false
Control Flow Statements
Now that we are familiar with condition statements, let us proceed to learn how
we can use these statements to control the flow of a program.
If Statement

The if statement is one of the most commonly


used control flow statements. It allows the program to
evaluate if a certain condition is met, and to perform the
appropriate action based on the result of the evaluation.
The structure of an if statement is as follows (line
numbers are added for reference):
Syntax
1 if (condition 1 is met)
2 {
3 do Task A
4 }
5 else if (condition 2 is met)
6 {
7 do Task B
8 }
9 else if (condition 3 is met)
10 {
11 do Task C
12 }
13 else
14 {
15 do Task E
16 }
Line 1 tests the first condition. If the condition is met, everything
inside the pair of curly braces that follow (lines 2 to 4) will be executed.
The rest of the if statement (from line 5 to 16) will be skipped.

If the first condition is not met, you can use the else if
statements that follow to test more conditions (lines 5 to 12). There
can be multiple else if statements. Finally, you can use the else
statement (lines 13 to 16) to execute some code if none of the
preceding conditions are met. To fully understand how the if
statement works, add the following code to the Main() program in the
VSC template
int userAge;
Console.Write("Please enter your age: ");
userAge = Convert.ToInt32(Console.ReadLine());

if (userAge < 0 || userAge > 100)


{
Console.WriteLine("Invalid Age");
Console.WriteLine("Age must be between 0 and 100");
}
else if (userAge < 18)
Console.WriteLine("Sorry you are underage");

else if (userAge < 21)


Console.WriteLine("You need parental consent");

else
{
Console.WriteLine("Congratulations!");
Console.WriteLine("You may sign up for the event!");
}
Console.Read();
The program first prompts the user for his age and stores the
result in the userAge variable.
Next the statement if (userAge < 0 || userAge > 100)

checks if the value of userAge is smaller than zero or greater than


100. If either of the conditions is true, the program will execute all
statements within the curly braces that follow. In this example, it’ll
print “Invalid Age”, followed by “Age must be between 0 and 100”.

On the other hand, if both conditions are false, the program


will test the next condition - else if (userAge < 18). If userAge
is less than 18 (but more than or equal to 0 since the first
condition is not met), the program will print “Sorry you are
underage”.
You may notice that we did not enclose the statement
Console.WriteLine(“Sorry you are underage”); in curly
braces. This is because curly braces are optional if there is only
one statement to execute. If the user did not enter a value smaller
than 18, but entered a value greater than or equal to 18 but
smaller than 21, the next else if statement will be executed. In
this case, the message “You need parental consent” will be
printed. Finally, if the user entered a value greater than or equal to
21 but smaller than or equal to 100, the program will execute the
code in the else block. In this case, it will print “Congratulations”
followed by “You may sign up for the event!”.
Run the program five times and enter -1, 8, 20, 23 and 121
respectively for each run. You’ll get the following outputs:

Please enter your age: -1


Invalid Age
Age must be between 0 and 100
Please enter your age: 8
Sorry you are underage
Please enter your age: 20
You need parental consent
Please enter your age: 23
Congratulations!
You may sign up for the event!
Please enter your age: 121
Invalid Age
Age must be between 0 and 100
Inline If
An inline if statement is a simpler form of an if statement that is very
convenient if you want to assign a value to a variable depending on the result of a
condition.
The syntax is:
condition ? value if condition is true : value if condition is false;

For instance, the statement


3>2 ? 10 : 5;
returns the value 10 since 3 is greater than 2 (i.e. the
condition 3 > 2 is true). This value can then be assigned to a
variable.
If we write
int myNum = 3>2 ? 10 : 5;
myNum will be assigned the value 10.
Switch Statement

The switch statement is similar to an if statement except


that it does not work with a range of values. A switch statement
requires each case to be based on a single value. Depending on
the value of the variable used for switching, the program will
execute the correct block of code.
The syntax of a switch statement is as follows:

switch (variable used for switching)


{
case firstCase:
do A;
break (or other jump statements);
case secondCase:
do B;
break (or other jump statements);
case default:
do C;
break (or other jump statements);
}
You can have as many cases as you want when using a
switch statement. The default case is optional and is
executed if no other case applies. When a certain case is
satisfied, everything starting from the next line is executed
until a jump statement is reached.

A jump statement is a statement that instructs the


compiler to jump to another line in the program. We’ll look
at jump statements in greater depth later. The most
commonly used jump statement is the break; statement.
Let’s look at an example of how the switch statement works.

1 Console.Write("Enter your grade: ");


2 string userGrade = Console.ReadLine().ToUpper();
3
4 switch (userGrade)
5 {
6 case "A+":
7 case "A":
8 Console.WriteLine("Distinction");
9 break;
10 case "B":
11 Console.WriteLine("B Grade");
12 break;
13 case "C":
14 Console.WriteLine("C Grade");
15 break;
16 default:
17 Console.WriteLine("Fail");
18 break;
19 }
The program first prompts the user for his grade. If grade
is “A+” (Line 6), the program executes the next statement
until it reaches the break; statement. This means it’ll
execute Line 7 to 9. Thus the output is “Distinction”. If grade is
“A” (Line 7), the program executes Line 8 and 9. Similarly, the
output is “Distinction”. If grade is not “A+” or “A”, the program
checks the next case. It keeps checking from top to bottom
until a case is satisfied. If none of the cases applies, the
default case is executed. If you run the code above, you’ll get
the following output for each of the input shown:
Enter your grade: A+
Distinction
Enter your grade: A
Distinction
Enter your grade: B
B Grade
Enter your grade: C
C Grade
Enter your grade: D
Fail
Enter your grade: Hello
Fail

You might also like