Chapter 2: Basic Control
Statements in C#
Ph.D Luu Thi Bich Huong
@CMC UNI 2024
Content
2.1. if statement
2.2 switch statement
2.3 for statement
2.4 while statement
2.5 break, continue command
@CMC UNI 2024
2.1 if statement
The IF statement in C# is a conditional structure that allows you to execute a block of code
only when a certain condition is met. This structure plays an important role in programming,
helping you control the flow of program execution effectively.
if (condition)
{
// Code block executed when the condition is true
}
The boolean-expression will return either true or false.
✓If the boolean-expression returns true, the statements inside the body of if ( inside {...} )
will be executed.
✓If the boolean-expression returns false, the statements inside the body of if will be ignored.
@CMC UNI 2024
2.1 if statement
@CMC UNI 2024
2.1 if statement
int number = 2;
if (number<5)
{
Console.WriteLine("{0} is less than 5 ", number);
}
Console.WriteLine("This statement is always executed");
@CMC UNI 2024
if...else (if-then-else) Statement
The if statement in C# may have an optional else statement. The
block of code inside the else statement will be executed if the
expression is evaluated to false.
if (condition)
{
// Code block executed when the condition is true
}
Else
{
// Code block executed when the condition is false
}
@CMC UNI 2024
if...else (if-then-else) Statement
@CMC UNI 2024
2.1 if statement
int number = 12;
if (number<5)
{
Console.WriteLine("{0} is less than 5 ", number);
}
else
{
Console.WriteLine("{0} is greater than 5 or equal to 5 ",
number);
}
Console.WriteLine("This statement is always executed");
@CMC UNI 2024
if...else if (if-then-else if) Statement
When we have only one condition to test, if-then and if-then-else
statement works fine. But what if we have a multiple condition to test
and execute one of the many block of code.
if (condition1)
{
// Code block 1
}
else if (condition2)
{
// Code block 2
}
else
{
// Default code block
}
@CMC UNI 2024
if...else if (if-then-else if) Statement
int number = 12;
if (number<5)
{
Console.WriteLine("{0} is less than 5 ", number);
}
else if (number >5)
{
Console.WriteLine("{0} is greater than 5 ", number);
}
else
{
Console.WriteLine(" {0} equal to 5 ", number);
}
@CMC UNI 2024
Nested if...else Statement
An if...else statement can exist within another if...else statement. Such
statements are called nested if...else statement.
if (boolean expression)
{
if (nested-expression-1)
{
// code to be excuted
}
else{
// code to be excuted
} }
else
{
if (nested-expression-2)
{
// code to be excuted
}
else
{
//code to be excuted
} }
@CMC UNI 2024
Nested if...else Statement
int first = 7, second=-23,third=13;
if (first>second)
{
if(first>third)
{
Console.WriteLine(" {0} is the largest ", first);
}
else
{
Console.WriteLine(" {0} is the largest", third);
}
}
else
{
if (second>third)
{
Console.WriteLine(" {0} is the largest ", second);
}
else
{
Console.WriteLine(" {0} is the largest", third);
}
}
@CMC UNI 2024
2.2 switch statement
Switch-Case: Used to test one value against many different values.
switch (expression)
{
case value1:
// This block of code is executed when expression equals value1
break;
case value2:
// This block of code is executed when expression equals value2
break;
...
default:
// Block of code executed when no value matches
break;
}
@CMC UNI 2024
2.2 switch statement
int number = 3;
switch (number)
{
case 1:
Console.WriteLine("number 1");
break;
case 2:
Console.WriteLine("number 2");
break;
case 3:
Console.WriteLine("number 3");
break;
default:
Console.WriteLine("Unknown number");
break;
}
@CMC UNI 2024
2.2 switch statement
C# switch Statement with grouped cases
int number = 3;
switch (number)
{
case 1:
case 2:
case 3:
Console.WriteLine("The number is between 1-3");
break;
default:
Console.WriteLine("Unknown number");
break;
}
@CMC UNI 2024
2.3 for statement
When you know exactly how many times you want to loop through a
block of code, use the for loop
for (initialization; condition; increment/decrement)
{
conditional code;
}
Ex
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
@CMC UNI 2024
2.3 for statement
for(int i=1; i<5;i++)
{
Console.WriteLine("C# for loop {0}", i);
}
Output:
@CMC UNI 2024
2.3 for statement
Example for loop to compute sum of first n natural numbers
int n=5, sum=0;
for(int i=1; i<=n;i++)
{
sum+=i; // sum= sum +i
}
Console.WriteLine("Sum of first {0} natural numbers =
{1}", n, sum);
@CMC UNI 2024
2.3 for statement
@CMC UNI 2024
2.4. while statement
The while keyword is used to create while loop in C#
while (condition)
{
conditional code;
}
@CMC UNI 2024
2.4 while statement
int i=1;
while (i<=5)
{
Console.WriteLine("C# for loop: Iteration {0}", i);
i++;
}
Output:
@CMC UNI 2024
2.4 while statement
while loop to compute sum of first 5 natural numbers
int i=1, sum=0;
while (i<=5)
{
sum+=i;
i++;
}
Console.WriteLine("sum = {0}", sum);
@CMC UNI 2024
2.4 while statement
while loop to compute sum of first 5 natural numbers
@CMC UNI 2024
do…while statement
▪ do … while loop is similar to a while loop
▪ The do...while loop will execute at least once irrespective to
the test-expression.
do
{
conditional code;
}while (condition)
@CMC UNI 2024
do…while statement
int i=1, n=5, product;
do
{
product=n*i;
Console.WriteLine("{0}*{1}={2}", n,i,product);
i++;
}while (i<=10);
@CMC UNI 2024
2.4 while statement
Nested for loop
A for loop inside another for loop is called nested for loop.
outer - loop
{
body of outer-loop;
inner-loop
{
body of outer-loop;
}
…..
•}
@CMC UNI 2024
2.4 while statement
Example
int outerLoop=0, innerLoop =0;
for(int i=1; i<=5;i++)
{
outerLoop++;
for(int j=1; j<=5; j++)
{
innerLoop++;
}
}
Console.WriteLine("outer Loop runs {0} times", outerLoop);
Console.WriteLine("inner Loop runs {0} times", innerLoop);
@CMC UNI 2024
2.4 while statement
for(int i=1; i<=5;i++)
{
for(int j=1; j<=i; j++)
{
Console.Write(j+" ");
}
Console.WriteLine();
}
Output:
@CMC UNI 2024
2.5 break statement
In C#, we use the break statement
to terminate the loop.
As we know, loops iterate over a
block of code until the test
expression is false. However,
sometimes we may need to
terminate the loop immediately
without checking the test
expression.
@CMC UNI 2024
2.5 break statement
@CMC UNI 2024
2.5 break statement
for(int i=1; i<=4;i++)
{
//terminates the loop
if (i==3)
{
break;
}
Console.WriteLine(i);
}
Console.ReadLine();
@CMC UNI 2024
2.5 continue statement
In C#, we use the continue statement
to skip a current iteration of a loop.
When our program encounters the
continue statement, the program
control moves to the end of the loop
and executes the test condition (update
statement in case of for loop).
@CMC UNI 2024
2.5 continue statement
@CMC UNI 2024
2.5 continue statement
//outer loop
for(int i=1; i<=3;i++)
{
//inner loop
for(int j=1;j<=3;j++)
{
if (j==2)
{
continue;
}
Console.WriteLine("i="+i+" j="+j);
}
}
@CMC UNI 2024
Exercise
Exercise 1 : Write a C# program that asks the user to enter a
number and checks if it is positive, negative, or zero. Based on the
input, display an appropriate message.
Exercise 2: Write a C# program that prompts the user to enter a day
of the week (1 for Monday, 2 for Tuesday, etc.) and displays the
corresponding name of the day. Use a switch statement to determine
the day and output the result.
Exercise 3: Write a C# program that prompts the user to enter a
positive integer and then calculates the factorial of that number using
a for loop. Display the factorial as the output.
@CMC UNI 2024
Exercise
Exercise 4: Write a C# program that uses nested loops to display a
pattern of asterisks. The number of rows and columns in the pattern
should be determined by the user input.
Exercise 5: Write a C# program that prompts the user to guess a secret
number between 1 and 100. The program should generate a random
secret number and provide hints to the user (higher or lower) until they
correctly guess the number. Use a while loop and a break statement to
control the guessing process.
Exercise 6: Write a C# program that prompts the user to enter a series
of numbers. The program should calculate and display the sum of the
positive numbers entered by the user. Use a while loop and a continue
statement to skip negative numbers during the summation process.
@CMC UNI 2024
THANK YOU
@CMC UNI 2024 37