Conditional statement

If statement

If statement is the decision making statement which takes decision to choose the particular option from two or more altermatics.

Example in PHP
<?php
$a=5;
$b=3;
// if-else statement
if($a>$b)
{
	echo "A is greater then B.";
}else{
	echo "B is greater then A.";
}
//if-elseif-else statement
$a=6;
$b=5;
$c=4;
if($a>$b && $a>$c)
{
	echo "A is greatest.";
}elseif($b>$c){
	echo "B is greatest.";
}else{
echo "C is greatest.";
}
?>

Practice Questions

  • Input two number and print the greatest number.
  • Input three number and print the greatest number.
  • Input a student name,roll and marks of three subject and print the student name,roll,total,percentage and grade acording the following criteria.
    • Greater than and equal to 60 % is “A” Grade.
    • Greater than and equal to 50 % and Less than to 60 % is “B” Grade.
    • Greater than and equal to 40 % and Less than to 50 % is “C” Grade.
    • Less than to 40 % is “Fail”.
  • Input two digited number and print the greatest digit of number.

switch statement

switch statement is the multiple conditional statement.which choose a particular option from a group of alternatives and execute it.switch statement is declare with the keyword “switch” and it’s option are define with keyword “case”

Break Keyword :- Break is the keyword which is use to break flow of program

Example in PHP
<?php
$month=1;
switch($month){
	case 1:
		echo "Jan";
		break;
	case 2:
		echo "Feb";
		break;
	/*
	..................
	..................
	statements........
	..................	
	*/
	default:
		echo "Invalid number of months.";
		break;
}
?>

Go to Statement

Go to Statement is the unconditional jumping statement.which jumps the flow program one statement to another statement.where level is switched.

Example in PHP
<?php
for($i=0,$j=0; $i<100,$j<=50; $i++) {
  while($j++) {
    if($j==27) goto manish; 
  }  
}
echo "i = $i";
manish:
echo 'j hit '.$j;// output j hit 27
?>

Practice Questions

  • Input a bit (0-9) and print it in word.
  • Input a vowel and print a country name which start is the inputed vowel.
  • Addition,substraction,multiplication,division,modulous division and exit enter your choice.

© 2014 Powered By