Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

Control statements, if else conditional statement

Notes:

I. Sequential statements: [Starts at: 00min:00sec]
II. C# control statements: [Starts at : 07min:02sec]
III. if, if else statements: [Starts at: 14min:58sec]

Abu Jaffer Muhammad Ibn Musa Alkhwarizmi (Father of algorithms) and
Niklaus Wirth (Swiss computer scientist) stated that
"We can solve any problem just by having 4 types of statements"
1. Sequential statements
2. Conditional statements
3. Looping statements
4. Jumping statements

Note:
Control flow of execution is sequential i.e.
Statements are executed from top to down one after the another

I. Sequential statements: [Starts at: 00min:00sec]
Any statement that does not break the flow of execution is called as sequential statement
Ex:
Variable declaration statements
Assignment statements
Expression statements
etc.

Example Code:
using UnityEngine;
public class ControlsDemo : MonoBehaviour
{
void Start ()
{
// Sequential statements
// Do not break the flow of execution of code
Debug.Log ("Hello unity 1");
//Debug.Log ("Hello unity 2"); // wan to skip 2, 3, and 4 statements
//Debug.Log ("Hello unity 3"); // only option is to comment
//Debug.Log ("Hello unity 4");
Debug.Log ("Hello unity 5");
Debug.Log ("Hello unity 6");

// Can not control the execution of code
int a = 10;
Debug.Log ("a is equal to 10");
// we should control the execution
// I should not execute below statement right
Debug.Log ("a is not equal to 10");

}
}

II. C# control statements: [Starts at : 07min:02sec]
Help us to get the control over flow of execution of code

Classification Of Control Statements:

Conditional / selection statements:
allows us to take different flow of execution, depending on the result of given condition
Ex:
if
if else
else if ladder
switch case

Looping / iterative statements :
allows us to execute block of code repeatedly as long as the condition is true.
Ex:
for
while
do while
for each

Jumping/ branching statements:
allows us to perform immediate transfer of the program control anywhere in the code.
Ex:
goto
continue
break
return

III. if, if else statements: [Starts at: 14min:58sec]
The if statement selects a statement for execution based on the result of boolean expression

Syntax:
if(expression) statement1;
[else statement2;]

//OR
if(expression)
{
// true part statements;
}
else
{
// false part statements;
}

Example Code:
using UnityEngine;
public class ConditionalStatements : MonoBehaviour {
void Start () {
int a = 11;
if (a == 10) {
Debug.Log ("a is equal to 10");
} else {
Debug.Log ("a is not equal to 10");
}
}
}