python in if, if-else, switch, break, continue,
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
For example, let's say 1 to 10 is going to run a loop to print the number in the output but if we want to have any number from 1 to 10, such as 5 or 6 you do not want to print in the output So you can do the job with the help of a continuation statement.
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
IF STATEMENTS – PYTHON
Syntax means the way that we use to write the program as we refine the rules of the grammar of Hindi language to write Hindi, just to write the program, it is a reflection of the rules of the programming language of Syntax.if test expression: statement(s)
Example 1:-
age = 20 if age>=18: print ("You Are Eligible To Vote In India")
IF … ELSE STATEMENT:-
If we have only one option in the statement, what to do if the condition is correct but in the if..else statement we have two options if the condition is correct and what to do if it is not correct.if test expression: statement(s) else: statement(s)
Example 1:-
age = eval(input("Enter Your Age?")) if(age>=18): print("You are Eligible To vote in INDIA") else: print("Your Are Not Eligible To vote in INDIA")
Output:-
Enter Your Age? 12
You Are Not Eligible To vote in INDIA
IF…ELIF…ELSE STATEMENT:-
if ... elif ... else statement is also almost like an if..else statement but we get three conditions in an if..elif..else statement. In the if..elif..sese statement we also get elif condition with if and else, due to which we can check the very condition with its help.
Example 1:-
num = 19 if(num>=20): print("Number is Greater than or equal to 20") elif(num<20): print("Number is Less than 20") else: print("Please Enter Another Number")
the switch-case statement in Python
The switch-case statement is a powerful programming feature that allows you to control the flow of your program based on the value of a variable or an expression. You can use it to execute different blocks of code, depending on the variable value during runtime.
public static void switch_demo (string [] args) { int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println (monthString); } Example: - def switch_demo (argument): switcher = { 1: "January", 2: "February", 3: "March", 4: "April", 5: "May", 6: "June", 7: "July", 8: "August", 9: "September", 10: "October", 11: "November", 12: "December" } print switcher.get (argument, "Invalid month")
For example, suppose a loop is going to print the numbers from 1 to 100 in the output, but if we want to have any number from 1 to 100, like 50 or 60, But if we stop printing, we can do this with the help of a break statement.
Because with the help of a break statement, we can create a condition from which we can control the numbers being printed in the output.
for c in range(1,11): if c==5: break print (c)
Output:-
1
2
3
4
Example:-
shapes = ["square", "rectangle", "circle", "triangle", "oval"] for x in shapes: if x == "triangle": break print('Great shape :', x)
Output:-
Great shape: square
Great shape: rectangle
Great shape: circle
CONTINUE STATEMENT IN python
Continue Statement: - Friends, you read about the break statement in the next lesson. In today's lesson, we will read about Continue Statement. Using the Continue Statement, in the event of a particular condition being completed within a loop, except for that condition of the loop, the rest of the loop is printed in the output.For example, let's say 1 to 10 is going to run a loop to print the number in the output but if we want to have any number from 1 to 10, such as 5 or 6 you do not want to print in the output So you can do the job with the help of a continuation statement.
Example 1:-
for c in range(1,11): if c==5: continue print (c)
Output:-
1
2
3
4
6
7
8
9
10
Example 2:-
shapes = ["square", "rectangle", "circle", "triangle", "oval"] for x in shapes: if x == "triangle": continue print('Great shape :', x)
Output:-
Great shape: square
Great shape: rectangle
Great shape: circle
Great shape: oval
Comments
Post a Comment