C#
provides control flow statements for decision making.
Program.cs
Decision making statements
if statement
“if” statement execute the section of code when the condition evaluates to
true.
Syntax
if(condition){
// Executes if the condition true
}
Program.cs
using System; class Program { static void Main(string[] args) { Console.WriteLine("Please Enter Your Marks"); int marks = int.Parse(Console.ReadLine()); if (marks < 35) { Console.WriteLine("You failed in the Exam. Better luck next time"); } if(marks >= 35) { Console.WriteLine("Congrats, you passed in the Exam"); } } }
Sample Output1
Please
Enter Your Marks
85
Congrats,
you passed in the Exam
Sample Output2
Please
Enter Your Marks
20
You
failed in the Exam. Better luck next time
if-else statement
Syntax:
if(condition){
}
else{
}
If
the condition true, then if block code executed, otherwise else block code
executed.
Program.cs
using System; class Program { static void Main(string[] args) { Console.WriteLine("Please Enter Your Marks"); int marks = int.Parse(Console.ReadLine()); if (marks < 35) { Console.WriteLine("You failed in the Exam. Better luck next time"); } else { Console.WriteLine("Congrats, you passed in the Exam"); } } }
Sample Output1
Please
Enter Your Marks
85
Congrats,
you passed in the Exam
Sample Output2
Please
Enter Your Marks
20
You
failed in the Exam. Better luck next time
if-else if-else ladder
By
using if-else if-else construct, you can choose number of alternatives.
An
if statement can be followed by an optional else if...else statement, which is
very useful to test various conditions.
using System; class Program { static void Main(string[] args) { Console.WriteLine("Please Enter Your Marks"); int marks = int.Parse(Console.ReadLine()); if (marks < 35) { Console.WriteLine("You are failed"); } else if (marks < 50) { Console.WriteLine("You are passed and got third class"); } else if (marks < 60) { Console.WriteLine("You are passed and got second class"); } else if (marks < 70) { Console.WriteLine("You are passed and got first class"); } else { Console.WriteLine("You are passed and got distinction"); } } }
Sample Output1
Please
Enter Your Marks
20
You
are failed
Sample Output2
Please
Enter Your Marks
40
You
are passed and got third class
Sample Output3
Please
Enter Your Marks
60
You
are passed and got first class
Sample Output4
Please
Enter Your Marks
80
You
are passed and got distinction
No comments:
Post a Comment