Class 10 Exam  >  Class 10 Notes  >  Artificial Intelligence for Class 10  >  Important Notes: Flow control & Condition

Flow control & Condition Important Notes | Artificial Intelligence for Class 10 PDF Download

Decision Making Statement

In programming languages, decision-making statements determine the program’s execution flow. Python has the following decision-making statements:

  • if statement
  • if..else statements
  • if-elif ladder

If Statement

The if statement is used to test a condition: if the condition is true, a set of statements is executed (called the if-block).

Syntax - 

test expression: 

     statement(s)

# Check if the number is positive, we print an appropriate message 

num = 3 

if num > 0: 

     print(num, “is a positive number.”) 

     print(“this is always printed”) 

     num = -1 

if num > 0: 

     print(num, “is a positive number.”) 

     print(“this is always printed”)

If…else statement

The if/else statement is a control flow statement that allows you to run a block of code only if a set of conditions are satisfied.

Syntax - 

if test expression: 

     Body of if 

else: 

     Body of else

# A program to check if a person can vote 

age = input(“Enter Your Age”) 

if age >= 18: 

     print(“You are eligible to vote”) 

else: 

     print(“You are not eligible to vote”)

# Write Python program to find the greatest number among two numbers

num1 = int(input(“Enter Number”));

num2 = int(input(“Enter Number”));

if num1 >= num2: 

     if num1 == num2: 

          print("Both numbers are equal.") 

     else: 

          print("Fisrt number is greater than the second number.")

else: 

     print("Second number is greater than the First number.")

# Write python program to check the number is even or odd

num = int(input("Enter a number: "))

if (num % 2) == 0:

     print("{0} is Even".format(num))

else:

     print("{0} is Odd".format(num))

if-elif ladder

Elif stands for “else if.” It enables us to check for several expressions at the same time. If the if condition is False, the next elif block’s condition is checked, and so on. The body of else is executed if all of the conditions are False.

Syntax - 

if test expression: 

     Body of if

elif test expression: 

     Body of elif 

else: Body of else

# To check the grade of a student 

Marks = 60

if marks > 75: 

     print("You get an A grade") 

elif marks > 60: 

     print("You get a B grade") 

else: 

     print("You get a C grade")

Nested if statements

An if…elif…else sentence can be nestled inside another if…elif…else statement. In computer programming, this is referred to as nesting.

# Write a program to check weather number is zero, positive or negative

num = float(input("Enter a number: ")) 

if num >= 0:

     if num == 0: 

          print("Zero") 

     else: 

          print("Positive number") 

else: 

     print("Negative number")

For Loop

The for statement allows you to specify how many times a statement or compound statement should be repeated. A for statement’s body is executed one or more times until an optional condition is met.

Syntax - 

for val in sequence: 

     Body of for

# Program to find the sum of all numbers stored in a list

numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

sum = 0 

for val in numbers: 

     sum = sum+val

print("The sum is", sum)

While Statement

The while statement allows you to repeatedly execute a block of statements as long as a condition is true. A while statement is an example of what is called a looping statement. A while statement can have an optional else clause.

Syntax - 

while test_expression: 

     Body of while


# Program to add natural 


n = int(input("Enter n: ")) 

sum = 0

i = 1 

while i <= n: 

     sum = sum + i 

     i = i+1 

print("The sum is", sum)

The document Flow control & Condition Important Notes | Artificial Intelligence for Class 10 is a part of the Class 10 Course Artificial Intelligence for Class 10.
All you need of Class 10 at this link: Class 10
40 videos|35 docs|6 tests

Top Courses for Class 10

FAQs on Flow control & Condition Important Notes - Artificial Intelligence for Class 10

1. What is the purpose of a decision-making statement in programming?
Ans. A decision-making statement in programming allows the program to make decisions based on certain conditions. It determines the flow of the program by executing different code blocks depending on whether a condition is true or false.
2. What is the difference between an if statement and an if-else statement?
Ans. An if statement is used to execute a block of code only if a certain condition is true. On the other hand, an if-else statement allows for the execution of one block of code if the condition is true and another block if the condition is false.
3. How does a nested if statement work in programming?
Ans. A nested if statement is an if statement inside another if statement. It allows for more complex decision-making by checking multiple conditions in a hierarchical manner. The inner if statement is only executed if the outer if statement's condition is true.
4. What is the difference between a for loop and a while loop in programming?
Ans. A for loop is used when the number of iterations is known beforehand, while a while loop is used when the number of iterations is not known and is based on a condition. A for loop typically iterates over a sequence of values, while a while loop continues to execute until the condition becomes false.
5. How does a switch statement differ from an if-else statement in programming?
Ans. A switch statement is used when there are multiple possible cases to be evaluated against a single expression. It provides a cleaner way to handle multiple conditions compared to nested if-else statements. Switch statements are typically used when there are multiple options to choose from based on a single variable.
40 videos|35 docs|6 tests
Download as PDF
Explore Courses for Class 10 exam

Top Courses for Class 10

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Exam

,

Flow control & Condition Important Notes | Artificial Intelligence for Class 10

,

mock tests for examination

,

ppt

,

Semester Notes

,

study material

,

Summary

,

past year papers

,

shortcuts and tricks

,

Flow control & Condition Important Notes | Artificial Intelligence for Class 10

,

Sample Paper

,

Previous Year Questions with Solutions

,

practice quizzes

,

Important questions

,

Flow control & Condition Important Notes | Artificial Intelligence for Class 10

,

Extra Questions

,

Objective type Questions

,

MCQs

,

pdf

,

Free

,

video lectures

,

Viva Questions

;