C# Tutorial for Beginner : Chapter 4 Decision Making
INTRODUCTION
Decision making structures requires the software engineer to indicate at least one conditions to be assessed or tried by the program, alongside a statement or statements to be executed if the condition is resolved to be true, and alternatively, different statements to be executed if the condition is resolved to be false. Following is the general type of a run-of-the-mill decision making structure found in the greater part of the programming language. It can be said this flowchart below depict how “if” statement is works :
image 1. if-statemen flowchart
I think it is quite clear to understand how “if” statement works. When you set a certain condition like whether a is equal to 0 (a==0) or else is true then piece of code inside if statement will run, otherwise it will not run anything.
There are other type of condition statement other that if. There are if-statement which consists of a logical expression followed by one or more lines of code; if-else statement that can be followed by an optional else statement, which will run when the logical expression is false; nested-if statement that you can use one if or else-if statement inside another if or else-if statement (You can say there is "if" inside of "if"); switch statement that allows a variable to be tested for equality against a list of values (If some value case match variable then run statement); and nested-switch statement that indicated there are switch inside of switch. I will make example each type.
Image 2.Other type of conditional statement
CODE IF-STATEMENT
int a =1; if (a==1) { Console.WriteLine("inside IF"); } Console.WriteLine("Yaay");
The way if-statement works is the same as the flowchart in image 1. The above code gives an example of how if-statement works. First I declare the variable "a" and initialize it with an integer value 1. In the "if (condition)" section check whether "a" value 1 or not. If variable "a" value 1 then "Console.WriteLine (" inside IF "); will run otherwise "Console.WriteLine (" inside IF ");" will not run instead "Console.WriteLine (" Yaay ");" will run.
CODE IF-ELSE-STATEMENT
int b =4; if (4%2 == 0) { Console.WriteLine("even number"); } else { Console.WriteLine("odd number"); } Console.WriteLine("Pika Pika");
image 3. odd and even number flowchart
To comprehend the code above let us take a look flowchart above. Just like it describe. This kind of logical operation with modulo operator by 2 is to check odd and even number. Odd number is any integer that cannot be divided exactly by 2. On the other hand, even number is any integer that can be divided exactly by 2. Back in the code, is variable“a” value can be divided by 2 then it will print “even number” otherwise it will print “odd number”. After that whether it is true or false, “pika pika” will be printed in console.
This time I would like to show you how to use "AND" and "OR" logical expression. In most programming language AND operation symbolize with "&&" and OR with "||". To know the characteristic of this operation, take a look table below.
According in table above, AND operation only give true if two operand is true. On the other hand OR operation only give false if both operands are false. Looking this new knowledge, we can analyze the example code I give. This time conditional is a little bit longer than the previous two because there added if after else. So if "(a || b)" is false then it will check again second condition "(a && b)". But second condition is false too then it will run code in else section and print "CANDY".
CODE IF-ELSE-IF-STATEMENT
boolean a = true; boolean b = false; if (a || b) { Console.WriteLine("BREAD"); } else if (a && b) { Console.WriteLine("PIZZA"); } else { Console.WriteLine("BURGER"); } Console.WriteLine("CANDY");
This time I would like to show you how to use "AND" and "OR" logical expression. In most programming language AND operation symbolize with "&&" and OR with "||". To know the characteristic of this operation, take a look table below.
A | B | AND | OR |
TRUE | TRUE | TRUE | TRUE |
TRUE | FALSE | FALSE | TRUE |
FALSE | TRUE | FALSE | TRUE |
FALSE | FALSE | FALSE | FALSE |
According in table above, AND operation only give true if two operand is true. On the other hand OR operation only give false if both operands are false. Looking this new knowledge, we can analyze the example code I give. This time conditional is a little bit longer than the previous two because there added if after else. So if "(a || b)" is false then it will check again second condition "(a && b)". But second condition is false too then it will run code in else section and print "CANDY".
Comments
Post a Comment